Файловый менеджер - Редактировать - /home/easybachat/hisabat365.com/4a7891/laravel.tar
Ðазад
serializable-closure/src/Support/ClosureScope.php 0000644 00000000572 15060132303 0016243 0 ustar 00 <?php namespace Laravel\SerializableClosure\Support; use SplObjectStorage; class ClosureScope extends SplObjectStorage { /** * The number of serializations in current scope. * * @var int */ public $serializations = 0; /** * The number of closures that have to be serialized. * * @var int */ public $toSerialize = 0; } serializable-closure/src/Support/ClosureStream.php 0000644 00000007007 15060132303 0016425 0 ustar 00 <?php namespace Laravel\SerializableClosure\Support; #[\AllowDynamicProperties] class ClosureStream { /** * The stream protocol. */ const STREAM_PROTO = 'laravel-serializable-closure'; /** * Checks if this stream is registered. * * @var bool */ protected static $isRegistered = false; /** * The stream content. * * @var string */ protected $content; /** * The stream content. * * @var int */ protected $length; /** * The stream pointer. * * @var int */ protected $pointer = 0; /** * Opens file or URL. * * @param string $path * @param string $mode * @param string $options * @param string|null $opened_path * @return bool */ public function stream_open($path, $mode, $options, &$opened_path) { $this->content = "<?php\nreturn ".substr($path, strlen(static::STREAM_PROTO.'://')).';'; $this->length = strlen($this->content); return true; } /** * Read from stream. * * @param int $count * @return string */ public function stream_read($count) { $value = substr($this->content, $this->pointer, $count); $this->pointer += $count; return $value; } /** * Tests for end-of-file on a file pointer. * * @return bool */ public function stream_eof() { return $this->pointer >= $this->length; } /** * Change stream options. * * @param int $option * @param int $arg1 * @param int $arg2 * @return bool */ public function stream_set_option($option, $arg1, $arg2) { return false; } /** * Retrieve information about a file resource. * * @return array|bool */ public function stream_stat() { $stat = stat(__FILE__); // @phpstan-ignore-next-line $stat[7] = $stat['size'] = $this->length; return $stat; } /** * Retrieve information about a file. * * @param string $path * @param int $flags * @return array|bool */ public function url_stat($path, $flags) { $stat = stat(__FILE__); // @phpstan-ignore-next-line $stat[7] = $stat['size'] = $this->length; return $stat; } /** * Seeks to specific location in a stream. * * @param int $offset * @param int $whence * @return bool */ public function stream_seek($offset, $whence = SEEK_SET) { $crt = $this->pointer; switch ($whence) { case SEEK_SET: $this->pointer = $offset; break; case SEEK_CUR: $this->pointer += $offset; break; case SEEK_END: $this->pointer = $this->length + $offset; break; } if ($this->pointer < 0 || $this->pointer >= $this->length) { $this->pointer = $crt; return false; } return true; } /** * Retrieve the current position of a stream. * * @return int */ public function stream_tell() { return $this->pointer; } /** * Registers the stream. * * @return void */ public static function register() { if (! static::$isRegistered) { static::$isRegistered = stream_wrapper_register(static::STREAM_PROTO, __CLASS__); } } } serializable-closure/src/Support/SelfReference.php 0000644 00000000602 15060132303 0016337 0 ustar 00 <?php namespace Laravel\SerializableClosure\Support; class SelfReference { /** * The unique hash representing the object. * * @var string */ public $hash; /** * Creates a new self reference instance. * * @param string $hash * @return void */ public function __construct($hash) { $this->hash = $hash; } } serializable-closure/src/Support/ReflectionClosure.php 0000644 00000131002 15060132303 0017255 0 ustar 00 <?php namespace Laravel\SerializableClosure\Support; defined('T_NAME_QUALIFIED') || define('T_NAME_QUALIFIED', -4); defined('T_NAME_FULLY_QUALIFIED') || define('T_NAME_FULLY_QUALIFIED', -5); defined('T_FN') || define('T_FN', -6); defined('T_NULLSAFE_OBJECT_OPERATOR') || define('T_NULLSAFE_OBJECT_OPERATOR', -7); use Closure; use ReflectionFunction; class ReflectionClosure extends ReflectionFunction { protected $code; protected $tokens; protected $hashedName; protected $useVariables; protected $isStaticClosure; protected $isScopeRequired; protected $isBindingRequired; protected $isShortClosure; protected static $files = []; protected static $classes = []; protected static $functions = []; protected static $constants = []; protected static $structures = []; /** * Creates a new reflection closure instance. * * @param \Closure $closure * @param string|null $code * @return void */ public function __construct(Closure $closure, $code = null) { parent::__construct($closure); } /** * Checks if the closure is "static". * * @return bool */ public function isStatic(): bool { if ($this->isStaticClosure === null) { $this->isStaticClosure = strtolower(substr($this->getCode(), 0, 6)) === 'static'; } return $this->isStaticClosure; } /** * Checks if the closure is a "short closure". * * @return bool */ public function isShortClosure() { if ($this->isShortClosure === null) { $code = $this->getCode(); if ($this->isStatic()) { $code = substr($code, 6); } $this->isShortClosure = strtolower(substr(trim($code), 0, 2)) === 'fn'; } return $this->isShortClosure; } /** * Get the closure's code. * * @return string */ public function getCode() { if ($this->code !== null) { return $this->code; } $fileName = $this->getFileName(); $line = $this->getStartLine() - 1; $className = null; if (null !== $className = $this->getClosureScopeClass()) { $className = '\\'.trim($className->getName(), '\\'); } $builtin_types = self::getBuiltinTypes(); $class_keywords = ['self', 'static', 'parent']; $ns = $this->getClosureNamespaceName(); $nsf = $ns == '' ? '' : ($ns[0] == '\\' ? $ns : '\\'.$ns); $_file = var_export($fileName, true); $_dir = var_export(dirname($fileName), true); $_namespace = var_export($ns, true); $_class = var_export(trim($className ?: '', '\\'), true); $_function = $ns.($ns == '' ? '' : '\\').'{closure}'; $_method = ($className == '' ? '' : trim($className, '\\').'::').$_function; $_function = var_export($_function, true); $_method = var_export($_method, true); $_trait = null; $tokens = $this->getTokens(); $state = $lastState = 'start'; $inside_structure = false; $isFirstClassCallable = false; $isShortClosure = false; $inside_structure_mark = 0; $open = 0; $code = ''; $id_start = $id_start_ci = $id_name = $context = ''; $classes = $functions = $constants = null; $use = []; $lineAdd = 0; $isUsingScope = false; $isUsingThisObject = false; for ($i = 0, $l = count($tokens); $i < $l; $i++) { $token = $tokens[$i]; switch ($state) { case 'start': if ($token[0] === T_FUNCTION || $token[0] === T_STATIC) { $code .= $token[1]; $state = $token[0] === T_FUNCTION ? 'function' : 'static'; } elseif ($token[0] === T_FN) { $isShortClosure = true; $code .= $token[1]; $state = 'closure_args'; } elseif ($token[0] === T_PUBLIC || $token[0] === T_PROTECTED || $token[0] === T_PRIVATE) { $code = ''; $isFirstClassCallable = true; } break; case 'static': if ($token[0] === T_WHITESPACE || $token[0] === T_COMMENT || $token[0] === T_FUNCTION) { $code .= $token[1]; if ($token[0] === T_FUNCTION) { $state = 'function'; } } elseif ($token[0] === T_FN) { $isShortClosure = true; $code .= $token[1]; $state = 'closure_args'; } else { $code = ''; $state = 'start'; } break; case 'function': switch ($token[0]) { case T_STRING: if ($isFirstClassCallable) { $state = 'closure_args'; break; } $code = ''; $state = 'named_function'; break; case '(': $code .= '('; $state = 'closure_args'; break; default: $code .= is_array($token) ? $token[1] : $token; } break; case 'named_function': if ($token[0] === T_FUNCTION || $token[0] === T_STATIC) { $code = $token[1]; $state = $token[0] === T_FUNCTION ? 'function' : 'static'; } elseif ($token[0] === T_FN) { $isShortClosure = true; $code .= $token[1]; $state = 'closure_args'; } break; case 'closure_args': switch ($token[0]) { case T_NAME_QUALIFIED: [$id_start, $id_start_ci, $id_name] = $this->parseNameQualified($token[1]); $context = 'args'; $state = 'id_name'; $lastState = 'closure_args'; break; case T_NS_SEPARATOR: case T_STRING: $id_start = $token[1]; $id_start_ci = strtolower($id_start); $id_name = ''; $context = 'args'; $state = 'id_name'; $lastState = 'closure_args'; break; case T_USE: $code .= $token[1]; $state = 'use'; break; case T_DOUBLE_ARROW: $code .= $token[1]; if ($isShortClosure) { $state = 'closure'; } break; case ':': $code .= ':'; $state = 'return'; break; case '{': $code .= '{'; $state = 'closure'; $open++; break; default: $code .= is_array($token) ? $token[1] : $token; } break; case 'use': switch ($token[0]) { case T_VARIABLE: $use[] = substr($token[1], 1); $code .= $token[1]; break; case '{': $code .= '{'; $state = 'closure'; $open++; break; case ':': $code .= ':'; $state = 'return'; break; default: $code .= is_array($token) ? $token[1] : $token; break; } break; case 'return': switch ($token[0]) { case T_WHITESPACE: case T_COMMENT: case T_DOC_COMMENT: $code .= $token[1]; break; case T_NS_SEPARATOR: case T_STRING: $id_start = $token[1]; $id_start_ci = strtolower($id_start); $id_name = ''; $context = 'return_type'; $state = 'id_name'; $lastState = 'return'; break 2; case T_NAME_QUALIFIED: [$id_start, $id_start_ci, $id_name] = $this->parseNameQualified($token[1]); $context = 'return_type'; $state = 'id_name'; $lastState = 'return'; break 2; case T_DOUBLE_ARROW: $code .= $token[1]; if ($isShortClosure) { $state = 'closure'; } break; case '{': $code .= '{'; $state = 'closure'; $open++; break; default: $code .= is_array($token) ? $token[1] : $token; break; } break; case 'closure': switch ($token[0]) { case T_CURLY_OPEN: case T_DOLLAR_OPEN_CURLY_BRACES: case '{': $code .= is_array($token) ? $token[1] : $token; $open++; break; case '}': $code .= '}'; if (--$open === 0 && ! $isShortClosure) { break 3; } elseif ($inside_structure) { $inside_structure = ! ($open === $inside_structure_mark); } break; case '(': case '[': $code .= $token[0]; if ($isShortClosure) { $open++; } break; case ')': case ']': if ($isShortClosure) { if ($open === 0) { break 3; } $open--; } $code .= $token[0]; break; case ',': case ';': if ($isShortClosure && $open === 0) { break 3; } $code .= $token[0]; break; case T_LINE: $code .= $token[2] - $line + $lineAdd; break; case T_FILE: $code .= $_file; break; case T_DIR: $code .= $_dir; break; case T_NS_C: $code .= $_namespace; break; case T_CLASS_C: $code .= $inside_structure ? $token[1] : $_class; break; case T_FUNC_C: $code .= $inside_structure ? $token[1] : $_function; break; case T_METHOD_C: $code .= $inside_structure ? $token[1] : $_method; break; case T_COMMENT: if (substr($token[1], 0, 8) === '#trackme') { $timestamp = time(); $code .= '/**'.PHP_EOL; $code .= '* Date : '.date(DATE_W3C, $timestamp).PHP_EOL; $code .= '* Timestamp : '.$timestamp.PHP_EOL; $code .= '* Line : '.($line + 1).PHP_EOL; $code .= '* File : '.$_file.PHP_EOL.'*/'.PHP_EOL; $lineAdd += 5; } else { $code .= $token[1]; } break; case T_VARIABLE: if ($token[1] == '$this' && ! $inside_structure) { $isUsingThisObject = true; } $code .= $token[1]; break; case T_STATIC: case T_NS_SEPARATOR: case T_STRING: $id_start = $token[1]; $id_start_ci = strtolower($id_start); $id_name = ''; $context = 'root'; $state = 'id_name'; $lastState = 'closure'; break 2; case T_NAME_QUALIFIED: [$id_start, $id_start_ci, $id_name] = $this->parseNameQualified($token[1]); $context = 'root'; $state = 'id_name'; $lastState = 'closure'; break 2; case T_NEW: $code .= $token[1]; $context = 'new'; $state = 'id_start'; $lastState = 'closure'; break 2; case T_USE: $code .= $token[1]; $context = 'use'; $state = 'id_start'; $lastState = 'closure'; break; case T_INSTANCEOF: case T_INSTEADOF: $code .= $token[1]; $context = 'instanceof'; $state = 'id_start'; $lastState = 'closure'; break; case T_OBJECT_OPERATOR: case T_NULLSAFE_OBJECT_OPERATOR: case T_DOUBLE_COLON: $code .= $token[1]; $lastState = 'closure'; $state = 'ignore_next'; break; case T_FUNCTION: $code .= $token[1]; $state = 'closure_args'; if (! $inside_structure) { $inside_structure = true; $inside_structure_mark = $open; } break; case T_TRAIT_C: if ($_trait === null) { $startLine = $this->getStartLine(); $endLine = $this->getEndLine(); $structures = $this->getStructures(); $_trait = ''; foreach ($structures as &$struct) { if ($struct['type'] === 'trait' && $struct['start'] <= $startLine && $struct['end'] >= $endLine ) { $_trait = ($ns == '' ? '' : $ns.'\\').$struct['name']; break; } } $_trait = var_export($_trait, true); } $code .= $_trait; break; default: $code .= is_array($token) ? $token[1] : $token; } break; case 'ignore_next': switch ($token[0]) { case T_WHITESPACE: case T_COMMENT: case T_DOC_COMMENT: $code .= $token[1]; break; case T_CLASS: case T_NEW: case T_STATIC: case T_VARIABLE: case T_STRING: case T_CLASS_C: case T_FILE: case T_DIR: case T_METHOD_C: case T_FUNC_C: case T_FUNCTION: case T_INSTANCEOF: case T_LINE: case T_NS_C: case T_TRAIT_C: case T_USE: $code .= $token[1]; $state = $lastState; break; default: $state = $lastState; $i--; } break; case 'id_start': switch ($token[0]) { case T_WHITESPACE: case T_COMMENT: case T_DOC_COMMENT: $code .= $token[1]; break; case T_NS_SEPARATOR: case T_NAME_FULLY_QUALIFIED: case T_STRING: case T_STATIC: $id_start = $token[1]; $id_start_ci = strtolower($id_start); $id_name = ''; $state = 'id_name'; break 2; case T_NAME_QUALIFIED: [$id_start, $id_start_ci, $id_name] = $this->parseNameQualified($token[1]); $state = 'id_name'; break 2; case T_VARIABLE: $code .= $token[1]; $state = $lastState; break; case T_CLASS: $code .= $token[1]; $state = 'anonymous'; break; default: $i--; //reprocess last $state = 'id_name'; } break; case 'id_name': switch ($token[0]) { case $token[0] === ':' && $context !== 'instanceof': if ($lastState === 'closure' && $context === 'root') { $state = 'closure'; $code .= $id_start.$token; } break; case T_NAME_QUALIFIED: case T_NS_SEPARATOR: case T_STRING: case T_WHITESPACE: case T_COMMENT: case T_DOC_COMMENT: $id_name .= $token[1]; break; case '(': if ($isShortClosure) { $open++; } if ($context === 'new' || false !== strpos($id_name, '\\')) { if ($id_start_ci === 'self' || $id_start_ci === 'static') { if (! $inside_structure) { $isUsingScope = true; } } elseif ($id_start !== '\\' && ! in_array($id_start_ci, $class_keywords)) { if ($classes === null) { $classes = $this->getClasses(); } if (isset($classes[$id_start_ci])) { $id_start = $classes[$id_start_ci]; } if ($id_start[0] !== '\\') { $id_start = $nsf.'\\'.$id_start; } } } else { if ($id_start !== '\\') { if ($functions === null) { $functions = $this->getFunctions(); } if (isset($functions[$id_start_ci])) { $id_start = $functions[$id_start_ci]; } elseif ($nsf !== '\\' && function_exists($nsf.'\\'.$id_start)) { $id_start = $nsf.'\\'.$id_start; // Cache it to functions array $functions[$id_start_ci] = $id_start; } } } $code .= $id_start.$id_name.'('; $state = $lastState; break; case T_VARIABLE: case T_DOUBLE_COLON: if ($id_start !== '\\') { if ($id_start_ci === 'self' || $id_start_ci === 'parent') { if (! $inside_structure) { $isUsingScope = true; } } elseif ($id_start_ci === 'static') { if (! $inside_structure) { $isUsingScope = $token[0] === T_DOUBLE_COLON; } } elseif (! (\PHP_MAJOR_VERSION >= 7 && in_array($id_start_ci, $builtin_types))) { if ($classes === null) { $classes = $this->getClasses(); } if (isset($classes[$id_start_ci])) { $id_start = $classes[$id_start_ci]; } if ($id_start[0] !== '\\') { $id_start = $nsf.'\\'.$id_start; } } } $code .= $id_start.$id_name.$token[1]; $state = $token[0] === T_DOUBLE_COLON ? 'ignore_next' : $lastState; break; default: if ($id_start !== '\\' && ! defined($id_start)) { if ($constants === null) { $constants = $this->getConstants(); } if (isset($constants[$id_start])) { $id_start = $constants[$id_start]; } elseif ($context === 'new') { if (in_array($id_start_ci, $class_keywords)) { if (! $inside_structure) { $isUsingScope = true; } } else { if ($classes === null) { $classes = $this->getClasses(); } if (isset($classes[$id_start_ci])) { $id_start = $classes[$id_start_ci]; } if ($id_start[0] !== '\\') { $id_start = $nsf.'\\'.$id_start; } } } elseif ($context === 'use' || $context === 'instanceof' || $context === 'args' || $context === 'return_type' || $context === 'extends' || $context === 'root' ) { if (in_array($id_start_ci, $class_keywords)) { if (! $inside_structure && ! $id_start_ci === 'static') { $isUsingScope = true; } } elseif (! (\PHP_MAJOR_VERSION >= 7 && in_array($id_start_ci, $builtin_types))) { if ($classes === null) { $classes = $this->getClasses(); } if (isset($classes[$id_start_ci])) { $id_start = $classes[$id_start_ci]; } if ($id_start[0] !== '\\') { $id_start = $nsf.'\\'.$id_start; } } } } $code .= $id_start.$id_name; $state = $lastState; $i--; //reprocess last token } break; case 'anonymous': switch ($token[0]) { case T_NAME_QUALIFIED: [$id_start, $id_start_ci, $id_name] = $this->parseNameQualified($token[1]); $state = 'id_name'; $lastState = 'anonymous'; break 2; case T_NS_SEPARATOR: case T_STRING: $id_start = $token[1]; $id_start_ci = strtolower($id_start); $id_name = ''; $state = 'id_name'; $context = 'extends'; $lastState = 'anonymous'; break; case '{': $state = 'closure'; if (! $inside_structure) { $inside_structure = true; $inside_structure_mark = $open; } $i--; break; default: $code .= is_array($token) ? $token[1] : $token; } break; } } if ($isShortClosure) { $this->useVariables = $this->getStaticVariables(); } else { $this->useVariables = empty($use) ? $use : array_intersect_key($this->getStaticVariables(), array_flip($use)); } $this->isShortClosure = $isShortClosure; $this->isBindingRequired = $isUsingThisObject; $this->isScopeRequired = $isUsingScope; if (PHP_VERSION_ID >= 80100) { $attributesCode = array_map(function ($attribute) { $arguments = $attribute->getArguments(); $name = $attribute->getName(); $arguments = implode(', ', array_map(function ($argument, $key) { $argument = sprintf("'%s'", str_replace("'", "\\'", $argument)); if (is_string($key)) { $argument = sprintf('%s: %s', $key, $argument); } return $argument; }, $arguments, array_keys($arguments))); return "#[$name($arguments)]"; }, $this->getAttributes()); if (! empty($attributesCode)) { $code = implode("\n", array_merge($attributesCode, [$code])); } } $this->code = $code; return $this->code; } /** * Get PHP native built in types. * * @return array */ protected static function getBuiltinTypes() { // PHP 8.1 if (PHP_VERSION_ID >= 80100) { return ['array', 'callable', 'string', 'int', 'bool', 'float', 'iterable', 'void', 'object', 'mixed', 'false', 'null', 'never']; } // PHP 8 if (\PHP_MAJOR_VERSION === 8) { return ['array', 'callable', 'string', 'int', 'bool', 'float', 'iterable', 'void', 'object', 'mixed', 'false', 'null']; } // PHP 7 switch (\PHP_MINOR_VERSION) { case 0: return ['array', 'callable', 'string', 'int', 'bool', 'float']; case 1: return ['array', 'callable', 'string', 'int', 'bool', 'float', 'iterable', 'void']; default: return ['array', 'callable', 'string', 'int', 'bool', 'float', 'iterable', 'void', 'object']; } } /** * Gets the use variables by the closure. * * @return array */ public function getUseVariables() { if ($this->useVariables !== null) { return $this->useVariables; } $tokens = $this->getTokens(); $use = []; $state = 'start'; foreach ($tokens as &$token) { $is_array = is_array($token); switch ($state) { case 'start': if ($is_array && $token[0] === T_USE) { $state = 'use'; } break; case 'use': if ($is_array) { if ($token[0] === T_VARIABLE) { $use[] = substr($token[1], 1); } } elseif ($token == ')') { break 2; } break; } } $this->useVariables = empty($use) ? $use : array_intersect_key($this->getStaticVariables(), array_flip($use)); return $this->useVariables; } /** * Checks if binding is required. * * @return bool */ public function isBindingRequired() { if ($this->isBindingRequired === null) { $this->getCode(); } return $this->isBindingRequired; } /** * Checks if access to the scope is required. * * @return bool */ public function isScopeRequired() { if ($this->isScopeRequired === null) { $this->getCode(); } return $this->isScopeRequired; } /** * The the hash of the current file name. * * @return string */ protected function getHashedFileName() { if ($this->hashedName === null) { $this->hashedName = sha1($this->getFileName()); } return $this->hashedName; } /** * Get the file tokens. * * @return array */ protected function getFileTokens() { $key = $this->getHashedFileName(); if (! isset(static::$files[$key])) { static::$files[$key] = token_get_all(file_get_contents($this->getFileName())); } return static::$files[$key]; } /** * Get the tokens. * * @return array */ protected function getTokens() { if ($this->tokens === null) { $tokens = $this->getFileTokens(); $startLine = $this->getStartLine(); $endLine = $this->getEndLine(); $results = []; $start = false; foreach ($tokens as &$token) { if (! is_array($token)) { if ($start) { $results[] = $token; } continue; } $line = $token[2]; if ($line <= $endLine) { if ($line >= $startLine) { $start = true; $results[] = $token; } continue; } break; } $this->tokens = $results; } return $this->tokens; } /** * Get the classes. * * @return array */ protected function getClasses() { $key = $this->getHashedFileName(); if (! isset(static::$classes[$key])) { $this->fetchItems(); } return static::$classes[$key]; } /** * Get the functions. * * @return array */ protected function getFunctions() { $key = $this->getHashedFileName(); if (! isset(static::$functions[$key])) { $this->fetchItems(); } return static::$functions[$key]; } /** * Gets the constants. * * @return array */ protected function getConstants() { $key = $this->getHashedFileName(); if (! isset(static::$constants[$key])) { $this->fetchItems(); } return static::$constants[$key]; } /** * Get the structures. * * @return array */ protected function getStructures() { $key = $this->getHashedFileName(); if (! isset(static::$structures[$key])) { $this->fetchItems(); } return static::$structures[$key]; } /** * Fetch the items. * * @return void. */ protected function fetchItems() { $key = $this->getHashedFileName(); $classes = []; $functions = []; $constants = []; $structures = []; $tokens = $this->getFileTokens(); $open = 0; $state = 'start'; $lastState = ''; $prefix = ''; $name = ''; $alias = ''; $isFunc = $isConst = false; $startLine = $endLine = 0; $structType = $structName = ''; $structIgnore = false; foreach ($tokens as $token) { switch ($state) { case 'start': switch ($token[0]) { case T_CLASS: case T_INTERFACE: case T_TRAIT: $state = 'before_structure'; $startLine = $token[2]; $structType = $token[0] == T_CLASS ? 'class' : ($token[0] == T_INTERFACE ? 'interface' : 'trait'); break; case T_USE: $state = 'use'; $prefix = $name = $alias = ''; $isFunc = $isConst = false; break; case T_FUNCTION: $state = 'structure'; $structIgnore = true; break; case T_NEW: $state = 'new'; break; case T_OBJECT_OPERATOR: case T_DOUBLE_COLON: $state = 'invoke'; break; } break; case 'use': switch ($token[0]) { case T_FUNCTION: $isFunc = true; break; case T_CONST: $isConst = true; break; case T_NS_SEPARATOR: $name .= $token[1]; break; case T_STRING: $name .= $token[1]; $alias = $token[1]; break; case T_NAME_QUALIFIED: $name .= $token[1]; $pieces = explode('\\', $token[1]); $alias = end($pieces); break; case T_AS: $lastState = 'use'; $state = 'alias'; break; case '{': $prefix = $name; $name = $alias = ''; $state = 'use-group'; break; case ',': case ';': if ($name === '' || $name[0] !== '\\') { $name = '\\'.$name; } if ($alias !== '') { if ($isFunc) { $functions[strtolower($alias)] = $name; } elseif ($isConst) { $constants[$alias] = $name; } else { $classes[strtolower($alias)] = $name; } } $name = $alias = ''; $state = $token === ';' ? 'start' : 'use'; break; } break; case 'use-group': switch ($token[0]) { case T_NS_SEPARATOR: $name .= $token[1]; break; case T_NAME_QUALIFIED: $name .= $token[1]; $pieces = explode('\\', $token[1]); $alias = end($pieces); break; case T_STRING: $name .= $token[1]; $alias = $token[1]; break; case T_AS: $lastState = 'use-group'; $state = 'alias'; break; case ',': case '}': if ($prefix === '' || $prefix[0] !== '\\') { $prefix = '\\'.$prefix; } if ($alias !== '') { if ($isFunc) { $functions[strtolower($alias)] = $prefix.$name; } elseif ($isConst) { $constants[$alias] = $prefix.$name; } else { $classes[strtolower($alias)] = $prefix.$name; } } $name = $alias = ''; $state = $token === '}' ? 'use' : 'use-group'; break; } break; case 'alias': if ($token[0] === T_STRING) { $alias = $token[1]; $state = $lastState; } break; case 'new': switch ($token[0]) { case T_WHITESPACE: case T_COMMENT: case T_DOC_COMMENT: break 2; case T_CLASS: $state = 'structure'; $structIgnore = true; break; default: $state = 'start'; } break; case 'invoke': switch ($token[0]) { case T_WHITESPACE: case T_COMMENT: case T_DOC_COMMENT: break 2; default: $state = 'start'; } break; case 'before_structure': if ($token[0] == T_STRING) { $structName = $token[1]; $state = 'structure'; } break; case 'structure': switch ($token[0]) { case '{': case T_CURLY_OPEN: case T_DOLLAR_OPEN_CURLY_BRACES: $open++; break; case '}': if (--$open == 0) { if (! $structIgnore) { $structures[] = [ 'type' => $structType, 'name' => $structName, 'start' => $startLine, 'end' => $endLine, ]; } $structIgnore = false; $state = 'start'; } break; default: if (is_array($token)) { $endLine = $token[2]; } } break; } } static::$classes[$key] = $classes; static::$functions[$key] = $functions; static::$constants[$key] = $constants; static::$structures[$key] = $structures; } /** * Returns the namespace associated to the closure. * * @return string */ protected function getClosureNamespaceName() { $ns = $this->getNamespaceName(); // First class callables... if ($this->getName() !== '{closure}' && empty($ns) && ! is_null($this->getClosureScopeClass())) { $ns = $this->getClosureScopeClass()->getNamespaceName(); } return $ns; } /** * Parse the given token. * * @param string $token * @return array */ protected function parseNameQualified($token) { $pieces = explode('\\', $token); $id_start = array_shift($pieces); $id_start_ci = strtolower($id_start); $id_name = '\\'.implode('\\', $pieces); return [$id_start, $id_start_ci, $id_name]; } } serializable-closure/src/UnsignedSerializableClosure.php 0000644 00000003316 15060132304 0017621 0 ustar 00 <?php namespace Laravel\SerializableClosure; use Closure; use Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException; class UnsignedSerializableClosure { /** * The closure's serializable. * * @var \Laravel\SerializableClosure\Contracts\Serializable */ protected $serializable; /** * Creates a new serializable closure instance. * * @param \Closure $closure * @return void */ public function __construct(Closure $closure) { if (\PHP_VERSION_ID < 70400) { throw new PhpVersionNotSupportedException(); } $this->serializable = new Serializers\Native($closure); } /** * Resolve the closure with the given arguments. * * @return mixed */ public function __invoke() { if (\PHP_VERSION_ID < 70400) { throw new PhpVersionNotSupportedException(); } return call_user_func_array($this->serializable, func_get_args()); } /** * Gets the closure. * * @return \Closure */ public function getClosure() { if (\PHP_VERSION_ID < 70400) { throw new PhpVersionNotSupportedException(); } return $this->serializable->getClosure(); } /** * Get the serializable representation of the closure. * * @return array */ public function __serialize() { return [ 'serializable' => $this->serializable, ]; } /** * Restore the closure after serialization. * * @param array $data * @return void */ public function __unserialize($data) { $this->serializable = $data['serializable']; } } serializable-closure/src/Signers/Hmac.php 0000644 00000002061 15060132304 0014437 0 ustar 00 <?php namespace Laravel\SerializableClosure\Signers; use Laravel\SerializableClosure\Contracts\Signer; class Hmac implements Signer { /** * The secret key. * * @var string */ protected $secret; /** * Creates a new signer instance. * * @param string $secret * @return void */ public function __construct($secret) { $this->secret = $secret; } /** * Sign the given serializable. * * @param string $serialized * @return array */ public function sign($serialized) { return [ 'serializable' => $serialized, 'hash' => base64_encode(hash_hmac('sha256', $serialized, $this->secret, true)), ]; } /** * Verify the given signature. * * @param array $signature * @return bool */ public function verify($signature) { return hash_equals(base64_encode( hash_hmac('sha256', $signature['serializable'], $this->secret, true) ), $signature['hash']); } } serializable-closure/src/Serializers/Signed.php 0000644 00000004124 15060132304 0015664 0 ustar 00 <?php namespace Laravel\SerializableClosure\Serializers; use Laravel\SerializableClosure\Contracts\Serializable; use Laravel\SerializableClosure\Exceptions\InvalidSignatureException; use Laravel\SerializableClosure\Exceptions\MissingSecretKeyException; class Signed implements Serializable { /** * The signer that will sign and verify the closure's signature. * * @var \Laravel\SerializableClosure\Contracts\Signer|null */ public static $signer; /** * The closure to be serialized/unserialized. * * @var \Closure */ protected $closure; /** * Creates a new serializable closure instance. * * @param \Closure $closure * @return void */ public function __construct($closure) { $this->closure = $closure; } /** * Resolve the closure with the given arguments. * * @return mixed */ public function __invoke() { return call_user_func_array($this->closure, func_get_args()); } /** * Gets the closure. * * @return \Closure */ public function getClosure() { return $this->closure; } /** * Get the serializable representation of the closure. * * @return array */ public function __serialize() { if (! static::$signer) { throw new MissingSecretKeyException(); } return static::$signer->sign( serialize(new Native($this->closure)) ); } /** * Restore the closure after serialization. * * @param array $signature * @return void * * @throws \Laravel\SerializableClosure\Exceptions\InvalidSignatureException */ public function __unserialize($signature) { if (static::$signer && ! static::$signer->verify($signature)) { throw new InvalidSignatureException(); } /** @var \Laravel\SerializableClosure\Contracts\Serializable $serializable */ $serializable = unserialize($signature['serializable']); $this->closure = $serializable->getClosure(); } } serializable-closure/src/Serializers/Native.php 0000644 00000034020 15060132304 0015677 0 ustar 00 <?php namespace Laravel\SerializableClosure\Serializers; use Closure; use DateTimeInterface; use Laravel\SerializableClosure\Contracts\Serializable; use Laravel\SerializableClosure\SerializableClosure; use Laravel\SerializableClosure\Support\ClosureScope; use Laravel\SerializableClosure\Support\ClosureStream; use Laravel\SerializableClosure\Support\ReflectionClosure; use Laravel\SerializableClosure\Support\SelfReference; use Laravel\SerializableClosure\UnsignedSerializableClosure; use ReflectionObject; use UnitEnum; class Native implements Serializable { /** * Transform the use variables before serialization. * * @var \Closure|null */ public static $transformUseVariables; /** * Resolve the use variables after unserialization. * * @var \Closure|null */ public static $resolveUseVariables; /** * The closure to be serialized/unserialized. * * @var \Closure */ protected $closure; /** * The closure's reflection. * * @var \Laravel\SerializableClosure\Support\ReflectionClosure|null */ protected $reflector; /** * The closure's code. * * @var array|null */ protected $code; /** * The closure's reference. * * @var string */ protected $reference; /** * The closure's scope. * * @var \Laravel\SerializableClosure\Support\ClosureScope|null */ protected $scope; /** * The "key" that marks an array as recursive. */ const ARRAY_RECURSIVE_KEY = 'LARAVEL_SERIALIZABLE_RECURSIVE_KEY'; /** * Creates a new serializable closure instance. * * @param \Closure $closure * @return void */ public function __construct(Closure $closure) { $this->closure = $closure; } /** * Resolve the closure with the given arguments. * * @return mixed */ public function __invoke() { return call_user_func_array($this->closure, func_get_args()); } /** * Gets the closure. * * @return \Closure */ public function getClosure() { return $this->closure; } /** * Get the serializable representation of the closure. * * @return array */ public function __serialize() { if ($this->scope === null) { $this->scope = new ClosureScope(); $this->scope->toSerialize++; } $this->scope->serializations++; $scope = $object = null; $reflector = $this->getReflector(); if ($reflector->isBindingRequired()) { $object = $reflector->getClosureThis(); static::wrapClosures($object, $this->scope); } if ($scope = $reflector->getClosureScopeClass()) { $scope = $scope->name; } $this->reference = spl_object_hash($this->closure); $this->scope[$this->closure] = $this; $use = $reflector->getUseVariables(); if (static::$transformUseVariables) { $use = call_user_func(static::$transformUseVariables, $reflector->getUseVariables()); } $code = $reflector->getCode(); $this->mapByReference($use); $data = [ 'use' => $use, 'function' => $code, 'scope' => $scope, 'this' => $object, 'self' => $this->reference, ]; if (! --$this->scope->serializations && ! --$this->scope->toSerialize) { $this->scope = null; } return $data; } /** * Restore the closure after serialization. * * @param array $data * @return void */ public function __unserialize($data) { ClosureStream::register(); $this->code = $data; unset($data); $this->code['objects'] = []; if ($this->code['use']) { $this->scope = new ClosureScope(); if (static::$resolveUseVariables) { $this->code['use'] = call_user_func(static::$resolveUseVariables, $this->code['use']); } $this->mapPointers($this->code['use']); extract($this->code['use'], EXTR_OVERWRITE | EXTR_REFS); $this->scope = null; } $this->closure = include ClosureStream::STREAM_PROTO.'://'.$this->code['function']; if ($this->code['this'] === $this) { $this->code['this'] = null; } $this->closure = $this->closure->bindTo($this->code['this'], $this->code['scope']); if (! empty($this->code['objects'])) { foreach ($this->code['objects'] as $item) { $item['property']->setValue($item['instance'], $item['object']->getClosure()); } } $this->code = $this->code['function']; } /** * Ensures the given closures are serializable. * * @param mixed $data * @param \Laravel\SerializableClosure\Support\ClosureScope $storage * @return void */ public static function wrapClosures(&$data, $storage) { if ($data instanceof Closure) { $data = new static($data); } elseif (is_array($data)) { if (isset($data[self::ARRAY_RECURSIVE_KEY])) { return; } $data[self::ARRAY_RECURSIVE_KEY] = true; foreach ($data as $key => &$value) { if ($key === self::ARRAY_RECURSIVE_KEY) { continue; } static::wrapClosures($value, $storage); } unset($value); unset($data[self::ARRAY_RECURSIVE_KEY]); } elseif ($data instanceof \stdClass) { if (isset($storage[$data])) { $data = $storage[$data]; return; } $data = $storage[$data] = clone $data; foreach ($data as &$value) { static::wrapClosures($value, $storage); } unset($value); } elseif (is_object($data) && ! $data instanceof static && ! $data instanceof UnitEnum) { if (isset($storage[$data])) { $data = $storage[$data]; return; } $instance = $data; $reflection = new ReflectionObject($instance); if (! $reflection->isUserDefined()) { $storage[$instance] = $data; return; } $storage[$instance] = $data = $reflection->newInstanceWithoutConstructor(); do { if (! $reflection->isUserDefined()) { break; } foreach ($reflection->getProperties() as $property) { if ($property->isStatic() || ! $property->getDeclaringClass()->isUserDefined()) { continue; } $property->setAccessible(true); if (PHP_VERSION >= 7.4 && ! $property->isInitialized($instance)) { continue; } $value = $property->getValue($instance); if (is_array($value) || is_object($value)) { static::wrapClosures($value, $storage); } $property->setValue($data, $value); } } while ($reflection = $reflection->getParentClass()); } } /** * Gets the closure's reflector. * * @return \Laravel\SerializableClosure\Support\ReflectionClosure */ public function getReflector() { if ($this->reflector === null) { $this->code = null; $this->reflector = new ReflectionClosure($this->closure); } return $this->reflector; } /** * Internal method used to map closure pointers. * * @param mixed $data * @return void */ protected function mapPointers(&$data) { $scope = $this->scope; if ($data instanceof static) { $data = &$data->closure; } elseif (is_array($data)) { if (isset($data[self::ARRAY_RECURSIVE_KEY])) { return; } $data[self::ARRAY_RECURSIVE_KEY] = true; foreach ($data as $key => &$value) { if ($key === self::ARRAY_RECURSIVE_KEY) { continue; } elseif ($value instanceof static) { $data[$key] = &$value->closure; } elseif ($value instanceof SelfReference && $value->hash === $this->code['self']) { $data[$key] = &$this->closure; } else { $this->mapPointers($value); } } unset($value); unset($data[self::ARRAY_RECURSIVE_KEY]); } elseif ($data instanceof \stdClass) { if (isset($scope[$data])) { return; } $scope[$data] = true; foreach ($data as $key => &$value) { if ($value instanceof SelfReference && $value->hash === $this->code['self']) { $data->{$key} = &$this->closure; } elseif (is_array($value) || is_object($value)) { $this->mapPointers($value); } } unset($value); } elseif (is_object($data) && ! ($data instanceof Closure)) { if (isset($scope[$data])) { return; } $scope[$data] = true; $reflection = new ReflectionObject($data); do { if (! $reflection->isUserDefined()) { break; } foreach ($reflection->getProperties() as $property) { if ($property->isStatic() || ! $property->getDeclaringClass()->isUserDefined()) { continue; } $property->setAccessible(true); if (PHP_VERSION >= 7.4 && ! $property->isInitialized($data)) { continue; } $item = $property->getValue($data); if ($item instanceof SerializableClosure || $item instanceof UnsignedSerializableClosure || ($item instanceof SelfReference && $item->hash === $this->code['self'])) { $this->code['objects'][] = [ 'instance' => $data, 'property' => $property, 'object' => $item instanceof SelfReference ? $this : $item, ]; } elseif (is_array($item) || is_object($item)) { $this->mapPointers($item); $property->setValue($data, $item); } } } while ($reflection = $reflection->getParentClass()); } } /** * Internal method used to map closures by reference. * * @param mixed $data * @return void */ protected function mapByReference(&$data) { if ($data instanceof Closure) { if ($data === $this->closure) { $data = new SelfReference($this->reference); return; } if (isset($this->scope[$data])) { $data = $this->scope[$data]; return; } $instance = new static($data); $instance->scope = $this->scope; $data = $this->scope[$data] = $instance; } elseif (is_array($data)) { if (isset($data[self::ARRAY_RECURSIVE_KEY])) { return; } $data[self::ARRAY_RECURSIVE_KEY] = true; foreach ($data as $key => &$value) { if ($key === self::ARRAY_RECURSIVE_KEY) { continue; } $this->mapByReference($value); } unset($value); unset($data[self::ARRAY_RECURSIVE_KEY]); } elseif ($data instanceof \stdClass) { if (isset($this->scope[$data])) { $data = $this->scope[$data]; return; } $instance = $data; $this->scope[$instance] = $data = clone $data; foreach ($data as &$value) { $this->mapByReference($value); } unset($value); } elseif (is_object($data) && ! $data instanceof SerializableClosure && ! $data instanceof UnsignedSerializableClosure) { if (isset($this->scope[$data])) { $data = $this->scope[$data]; return; } $instance = $data; if ($data instanceof DateTimeInterface) { $this->scope[$instance] = $data; return; } if ($data instanceof UnitEnum) { $this->scope[$instance] = $data; return; } $reflection = new ReflectionObject($data); if (! $reflection->isUserDefined()) { $this->scope[$instance] = $data; return; } $this->scope[$instance] = $data = $reflection->newInstanceWithoutConstructor(); do { if (! $reflection->isUserDefined()) { break; } foreach ($reflection->getProperties() as $property) { if ($property->isStatic() || ! $property->getDeclaringClass()->isUserDefined()) { continue; } $property->setAccessible(true); if (PHP_VERSION >= 7.4 && ! $property->isInitialized($instance)) { continue; } $value = $property->getValue($instance); if (is_array($value) || is_object($value)) { $this->mapByReference($value); } $property->setValue($data, $value); } } while ($reflection = $reflection->getParentClass()); } } } serializable-closure/src/Contracts/Serializable.php 0000644 00000000541 15060132304 0016524 0 ustar 00 <?php namespace Laravel\SerializableClosure\Contracts; interface Serializable { /** * Resolve the closure with the given arguments. * * @return mixed */ public function __invoke(); /** * Gets the closure that got serialized/unserialized. * * @return \Closure */ public function getClosure(); } serializable-closure/src/Contracts/Signer.php 0000644 00000000604 15060132304 0015345 0 ustar 00 <?php namespace Laravel\SerializableClosure\Contracts; interface Signer { /** * Sign the given serializable. * * @param string $serializable * @return array */ public function sign($serializable); /** * Verify the given signature. * * @param array $signature * @return bool */ public function verify($signature); } serializable-closure/src/SerializableClosure.php 0000644 00000006453 15060132304 0016131 0 ustar 00 <?php namespace Laravel\SerializableClosure; use Closure; use Laravel\SerializableClosure\Exceptions\InvalidSignatureException; use Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException; use Laravel\SerializableClosure\Serializers\Signed; use Laravel\SerializableClosure\Signers\Hmac; class SerializableClosure { /** * The closure's serializable. * * @var \Laravel\SerializableClosure\Contracts\Serializable */ protected $serializable; /** * Creates a new serializable closure instance. * * @param \Closure $closure * @return void */ public function __construct(Closure $closure) { if (\PHP_VERSION_ID < 70400) { throw new PhpVersionNotSupportedException(); } $this->serializable = Serializers\Signed::$signer ? new Serializers\Signed($closure) : new Serializers\Native($closure); } /** * Resolve the closure with the given arguments. * * @return mixed */ public function __invoke() { if (\PHP_VERSION_ID < 70400) { throw new PhpVersionNotSupportedException(); } return call_user_func_array($this->serializable, func_get_args()); } /** * Gets the closure. * * @return \Closure */ public function getClosure() { if (\PHP_VERSION_ID < 70400) { throw new PhpVersionNotSupportedException(); } return $this->serializable->getClosure(); } /** * Create a new unsigned serializable closure instance. * * @param Closure $closure * @return \Laravel\SerializableClosure\UnsignedSerializableClosure */ public static function unsigned(Closure $closure) { return new UnsignedSerializableClosure($closure); } /** * Sets the serializable closure secret key. * * @param string|null $secret * @return void */ public static function setSecretKey($secret) { Serializers\Signed::$signer = $secret ? new Hmac($secret) : null; } /** * Sets the serializable closure secret key. * * @param \Closure|null $transformer * @return void */ public static function transformUseVariablesUsing($transformer) { Serializers\Native::$transformUseVariables = $transformer; } /** * Sets the serializable closure secret key. * * @param \Closure|null $resolver * @return void */ public static function resolveUseVariablesUsing($resolver) { Serializers\Native::$resolveUseVariables = $resolver; } /** * Get the serializable representation of the closure. * * @return array */ public function __serialize() { return [ 'serializable' => $this->serializable, ]; } /** * Restore the closure after serialization. * * @param array $data * @return void * * @throws \Laravel\SerializableClosure\Exceptions\InvalidSignatureException */ public function __unserialize($data) { if (Signed::$signer && ! $data['serializable'] instanceof Signed) { throw new InvalidSignatureException(); } $this->serializable = $data['serializable']; } } serializable-closure/src/Exceptions/InvalidSignatureException.php 0000644 00000000651 15060132304 0021430 0 ustar 00 <?php namespace Laravel\SerializableClosure\Exceptions; use Exception; class InvalidSignatureException extends Exception { /** * Create a new exception instance. * * @param string $message * @return void */ public function __construct($message = 'Your serialized closure might have been modified or it\'s unsafe to be unserialized.') { parent::__construct($message); } } serializable-closure/src/Exceptions/PhpVersionNotSupportedException.php 0000644 00000000564 15060132304 0022647 0 ustar 00 <?php namespace Laravel\SerializableClosure\Exceptions; use Exception; class PhpVersionNotSupportedException extends Exception { /** * Create a new exception instance. * * @param string $message * @return void */ public function __construct($message = 'PHP 7.3 is not supported.') { parent::__construct($message); } } serializable-closure/src/Exceptions/MissingSecretKeyException.php 0000644 00000000613 15060132304 0021406 0 ustar 00 <?php namespace Laravel\SerializableClosure\Exceptions; use Exception; class MissingSecretKeyException extends Exception { /** * Create a new exception instance. * * @param string $message * @return void */ public function __construct($message = 'No serializable closure secret key has been specified.') { parent::__construct($message); } } serializable-closure/LICENSE.md 0000644 00000002063 15060132304 0012263 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. serializable-closure/composer.json 0000644 00000002463 15060132304 0013405 0 ustar 00 { "name": "laravel/serializable-closure", "description": "Laravel Serializable Closure provides an easy and secure way to serialize closures in PHP.", "keywords": ["laravel", "Serializable", "closure"], "license": "MIT", "support": { "issues": "https://github.com/laravel/serializable-closure/issues", "source": "https://github.com/laravel/serializable-closure" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" }, { "name": "Nuno Maduro", "email": "nuno@laravel.com" } ], "require": { "php": "^7.3|^8.0" }, "require-dev": { "nesbot/carbon": "^2.61", "pestphp/pest": "^1.21.3", "phpstan/phpstan": "^1.8.2", "symfony/var-dumper": "^5.4.11" }, "autoload": { "psr-4": { "Laravel\\SerializableClosure\\": "src/" } }, "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }, "extra": { "branch-alias": { "dev-master": "1.x-dev" } }, "config": { "sort-packages": true, "allow-plugins": { "pestphp/pest-plugin": true } }, "minimum-stability": "dev", "prefer-stable": true } serializable-closure/README.md 0000644 00000005517 15060132304 0012145 0 ustar 00 # Serializable Closure <a href="https://github.com/laravel/serializable-closure/actions"> <img src="https://github.com/laravel/serializable-closure/workflows/tests/badge.svg" alt="Build Status"> </a> <a href="https://packagist.org/packages/laravel/serializable-closure"> <img src="https://img.shields.io/packagist/dt/laravel/serializable-closure" alt="Total Downloads"> </a> <a href="https://packagist.org/packages/laravel/serializable-closure"> <img src="https://img.shields.io/packagist/v/laravel/serializable-closure" alt="Latest Stable Version"> </a> <a href="https://packagist.org/packages/laravel/serializable-closure"> <img src="https://img.shields.io/packagist/l/laravel/serializable-closure" alt="License"> </a> ## Introduction > This project is a fork of the excellent [opis/closure: 3.x](https://github.com/opis/closure) package. At Laravel, we decided to fork this package as the upcoming version [4.x](https://github.com/opis/closure) is a complete rewrite on top of the [FFI extension](https://www.php.net/manual/en/book.ffi.php). As Laravel is a web framework, and FFI is not enabled by default in web requests, this fork allows us to keep using the `3.x` series while adding support for new PHP versions. Laravel Serializable Closure provides an easy and secure way to **serialize closures in PHP**. ## Official Documentation ### Installation > **Requires [PHP 7.4+](https://php.net/releases/)** First, install Laravel Serializable Closure via the [Composer](https://getcomposer.org/) package manager: ```bash composer require laravel/serializable-closure ``` ### Usage You may serialize a closure this way: ```php use Laravel\SerializableClosure\SerializableClosure; $closure = fn () => 'james'; // Recommended SerializableClosure::setSecretKey('secret'); $serialized = serialize(new SerializableClosure($closure)); $closure = unserialize($serialized)->getClosure(); echo $closure(); // james; ``` ### Caveats * Anonymous classes cannot be created within closures. * Attributes cannot be used within closures. * Serializing closures on REPL environments like Laravel Tinker is not supported. * Serializing closures that reference objects with readonly properties is not supported. ## Contributing Thank you for considering contributing to Serializable Closure! 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/serializable-closure/security/policy) on how to report security vulnerabilities. ## License Serializable Closure is open-sourced software licensed under the [MIT license](LICENSE.md). sail/stubs/minio.stub 0000644 00000001001 15060132304 0010630 0 ustar 00 minio: image: 'minio/minio:latest' ports: - '${FORWARD_MINIO_PORT:-9000}:9000' - '${FORWARD_MINIO_CONSOLE_PORT:-8900}:8900' environment: MINIO_ROOT_USER: 'sail' MINIO_ROOT_PASSWORD: 'password' volumes: - 'sail-minio:/data/minio' networks: - sail command: minio server /data/minio --console-address ":8900" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"] retries: 3 timeout: 5s sail/stubs/typesense.stub 0000644 00000001044 15060132304 0011543 0 ustar 00 typesense: image: 'typesense/typesense:0.25.2' ports: - '${FORWARD_TYPESENSE_PORT:-8108}:8108' environment: TYPESENSE_DATA_DIR: '${TYPESENSE_DATA_DIR:-/typesense-data}' TYPESENSE_API_KEY: '${TYPESENSE_API_KEY:-xyz}' TYPESENSE_ENABLE_CORS: '${TYPESENSE_ENABLE_CORS:-true}' volumes: - 'sail-typesense:/typesense-data' networks: - sail healthcheck: test: ["CMD", "wget", "--no-verbose", "--spider", "http://localhost:8108/health"] retries: 5 timeout: 7s sail/stubs/devcontainer.stub 0000644 00000001221 15060132304 0012202 0 ustar 00 // https://aka.ms/devcontainer.json { "name": "Existing Docker Compose (Extend)", "dockerComposeFile": [ "../docker-compose.yml" ], "service": "laravel.test", "workspaceFolder": "/var/www/html", "customizations": { "vscode": { "extensions": [ // "mikestead.dotenv", // "amiralizadeh9480.laravel-extra-intellisense", // "ryannaddy.laravel-artisan", // "onecentlin.laravel5-snippets", // "onecentlin.laravel-blade" ], "settings": {} } }, "remoteUser": "sail", "postCreateCommand": "chown -R 1000:1000 /var/www/html 2>/dev/null || true" // "forwardPorts": [], // "runServices": [], // "shutdownAction": "none", } sail/stubs/mailpit.stub 0000644 00000000275 15060132304 0011170 0 ustar 00 mailpit: image: 'axllent/mailpit:latest' ports: - '${FORWARD_MAILPIT_PORT:-1025}:1025' - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025' networks: - sail sail/stubs/pgsql.stub 0000644 00000001211 15060132304 0010646 0 ustar 00 pgsql: image: 'postgres:15' ports: - '${FORWARD_DB_PORT:-5432}:5432' environment: PGPASSWORD: '${DB_PASSWORD:-secret}' POSTGRES_DB: '${DB_DATABASE}' POSTGRES_USER: '${DB_USERNAME}' POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}' volumes: - 'sail-pgsql:/var/lib/postgresql/data' - './vendor/laravel/sail/database/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql' networks: - sail healthcheck: test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"] retries: 3 timeout: 5s sail/stubs/mariadb.stub 0000644 00000001243 15060132304 0011124 0 ustar 00 mariadb: image: 'mariadb:10' ports: - '${FORWARD_DB_PORT:-3306}:3306' environment: MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' MYSQL_ROOT_HOST: "%" MYSQL_DATABASE: '${DB_DATABASE}' MYSQL_USER: '${DB_USERNAME}' MYSQL_PASSWORD: '${DB_PASSWORD}' MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' volumes: - 'sail-mariadb:/var/lib/mysql' - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh' networks: - sail healthcheck: test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"] retries: 3 timeout: 5s sail/stubs/redis.stub 0000644 00000000403 15060132304 0010630 0 ustar 00 redis: image: 'redis:alpine' ports: - '${FORWARD_REDIS_PORT:-6379}:6379' volumes: - 'sail-redis:/data' networks: - sail healthcheck: test: ["CMD", "redis-cli", "ping"] retries: 3 timeout: 5s sail/stubs/meilisearch.stub 0000644 00000000654 15060132304 0012017 0 ustar 00 meilisearch: image: 'getmeili/meilisearch:latest' ports: - '${FORWARD_MEILISEARCH_PORT:-7700}:7700' environment: MEILI_NO_ANALYTICS: '${MEILISEARCH_NO_ANALYTICS:-false}' volumes: - 'sail-meilisearch:/meili_data' networks: - sail healthcheck: test: ["CMD", "wget", "--no-verbose", "--spider", "http://localhost:7700/health"] retries: 3 timeout: 5s sail/stubs/soketi.stub 0000644 00000000677 15060132304 0011035 0 ustar 00 soketi: image: 'quay.io/soketi/soketi:latest-16-alpine' environment: SOKETI_DEBUG: '${SOKETI_DEBUG:-1}' SOKETI_METRICS_SERVER_PORT: '9601' SOKETI_DEFAULT_APP_ID: '${PUSHER_APP_ID}' SOKETI_DEFAULT_APP_KEY: '${PUSHER_APP_KEY}' SOKETI_DEFAULT_APP_SECRET: '${PUSHER_APP_SECRET}' ports: - '${PUSHER_PORT:-6001}:6001' - '${PUSHER_METRICS_PORT:-9601}:9601' networks: - sail sail/stubs/docker-compose.stub 0000644 00000001516 15060132304 0012442 0 ustar 00 # For more information: https://laravel.com/docs/sail services: laravel.test: build: context: ./vendor/laravel/sail/runtimes/8.3 dockerfile: Dockerfile args: WWWGROUP: '${WWWGROUP}' image: sail-8.3/app extra_hosts: - 'host.docker.internal:host-gateway' ports: - '${APP_PORT:-80}:80' - '${VITE_PORT:-5173}:${VITE_PORT:-5173}' environment: WWWUSER: '${WWWUSER}' LARAVEL_SAIL: 1 XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}' XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}' IGNITION_LOCAL_SITES_PATH: '${PWD}' volumes: - '.:/var/www/html' networks: - sail networks: sail: driver: bridge sail/stubs/mysql.stub 0000644 00000001247 15060132304 0010676 0 ustar 00 mysql: image: 'mysql/mysql-server:8.0' ports: - '${FORWARD_DB_PORT:-3306}:3306' environment: MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' MYSQL_ROOT_HOST: "%" MYSQL_DATABASE: '${DB_DATABASE}' MYSQL_USER: '${DB_USERNAME}' MYSQL_PASSWORD: '${DB_PASSWORD}' MYSQL_ALLOW_EMPTY_PASSWORD: 1 volumes: - 'sail-mysql:/var/lib/mysql' - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh' networks: - sail healthcheck: test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"] retries: 3 timeout: 5s sail/stubs/memcached.stub 0000644 00000000204 15060132304 0011427 0 ustar 00 memcached: image: 'memcached:alpine' ports: - '${FORWARD_MEMCACHED_PORT:-11211}:11211' networks: - sail sail/stubs/selenium.stub 0000644 00000000271 15060132304 0011346 0 ustar 00 selenium: image: 'selenium/standalone-chrome' extra_hosts: - 'host.docker.internal:host-gateway' volumes: - '/dev/shm:/dev/shm' networks: - sail sail/bin/sail 0000755 00000042115 15060132304 0007117 0 ustar 00 #!/usr/bin/env bash UNAMEOUT="$(uname -s)" # Verify operating system is supported... case "${UNAMEOUT}" in Linux*) MACHINE=linux;; Darwin*) MACHINE=mac;; *) MACHINE="UNKNOWN" esac if [ "$MACHINE" == "UNKNOWN" ]; then echo "Unsupported operating system [$(uname -s)]. Laravel Sail supports macOS, Linux, and Windows (WSL2)." >&2 exit 1 fi # Determine if stdout is a terminal... if test -t 1; then # Determine if colors are supported... ncolors=$(tput colors) if test -n "$ncolors" && test "$ncolors" -ge 8; then BOLD="$(tput bold)" YELLOW="$(tput setaf 3)" GREEN="$(tput setaf 2)" NC="$(tput sgr0)" fi fi # Function that prints the available commands... function display_help { echo "Laravel Sail" echo echo "${YELLOW}Usage:${NC}" >&2 echo " sail COMMAND [options] [arguments]" echo echo "Unknown commands are passed to the docker-compose binary." echo echo "${YELLOW}docker-compose Commands:${NC}" echo " ${GREEN}sail up${NC} Start the application" echo " ${GREEN}sail up -d${NC} Start the application in the background" echo " ${GREEN}sail stop${NC} Stop the application" echo " ${GREEN}sail restart${NC} Restart the application" echo " ${GREEN}sail ps${NC} Display the status of all containers" echo echo "${YELLOW}Artisan Commands:${NC}" echo " ${GREEN}sail artisan ...${NC} Run an Artisan command" echo " ${GREEN}sail artisan queue:work${NC}" echo echo "${YELLOW}PHP Commands:${NC}" echo " ${GREEN}sail php ...${NC} Run a snippet of PHP code" echo " ${GREEN}sail php -v${NC}" echo echo "${YELLOW}Composer Commands:${NC}" echo " ${GREEN}sail composer ...${NC} Run a Composer command" echo " ${GREEN}sail composer require laravel/sanctum${NC}" echo echo "${YELLOW}Node Commands:${NC}" echo " ${GREEN}sail node ...${NC} Run a Node command" echo " ${GREEN}sail node --version${NC}" echo echo "${YELLOW}NPM Commands:${NC}" echo " ${GREEN}sail npm ...${NC} Run a npm command" echo " ${GREEN}sail npx${NC} Run a npx command" echo " ${GREEN}sail npm run prod${NC}" echo echo "${YELLOW}PNPM Commands:${NC}" echo " ${GREEN}sail pnpm ...${NC} Run a pnpm command" echo " ${GREEN}sail pnpx${NC} Run a pnpx command" echo " ${GREEN}sail pnpm run prod${NC}" echo echo "${YELLOW}Yarn Commands:${NC}" echo " ${GREEN}sail yarn ...${NC} Run a Yarn command" echo " ${GREEN}sail yarn run prod${NC}" echo echo "${YELLOW}Bun Commands:${NC}" echo " ${GREEN}sail bun ...${NC} Run a bun command" echo " ${GREEN}sail bunx${NC} Run a bunx command" echo " ${GREEN}sail bun run prod${NC}" echo echo "${YELLOW}Database Commands:${NC}" echo " ${GREEN}sail mysql${NC} Start a MySQL CLI session within the 'mysql' container" echo " ${GREEN}sail mariadb${NC} Start a MySQL CLI session within the 'mariadb' container" echo " ${GREEN}sail psql${NC} Start a PostgreSQL CLI session within the 'pgsql' container" echo " ${GREEN}sail redis${NC} Start a Redis CLI session within the 'redis' container" echo echo "${YELLOW}Debugging:${NC}" echo " ${GREEN}sail debug ...${NC} Run an Artisan command in debug mode" echo " ${GREEN}sail debug queue:work${NC}" echo echo "${YELLOW}Running Tests:${NC}" echo " ${GREEN}sail test${NC} Run the PHPUnit tests via the Artisan test command" echo " ${GREEN}sail phpunit ...${NC} Run PHPUnit" echo " ${GREEN}sail pest ...${NC} Run Pest" echo " ${GREEN}sail pint ...${NC} Run Pint" echo " ${GREEN}sail dusk${NC} Run the Dusk tests (Requires the laravel/dusk package)" echo " ${GREEN}sail dusk:fails${NC} Re-run previously failed Dusk tests (Requires the laravel/dusk package)" echo echo "${YELLOW}Container CLI:${NC}" echo " ${GREEN}sail shell${NC} Start a shell session within the application container" echo " ${GREEN}sail bash${NC} Alias for 'sail shell'" echo " ${GREEN}sail root-shell${NC} Start a root shell session within the application container" echo " ${GREEN}sail root-bash${NC} Alias for 'sail root-shell'" echo " ${GREEN}sail tinker${NC} Start a new Laravel Tinker session" echo echo "${YELLOW}Sharing:${NC}" echo " ${GREEN}sail share${NC} Share the application publicly via a temporary URL" echo " ${GREEN}sail open${NC} Open the site in your browser" echo echo "${YELLOW}Binaries:${NC}" echo " ${GREEN}sail bin ...${NC} Run Composer binary scripts from the vendor/bin directory" echo echo "${YELLOW}Customization:${NC}" echo " ${GREEN}sail artisan sail:publish${NC} Publish the Sail configuration files" echo " ${GREEN}sail build --no-cache${NC} Rebuild all of the Sail containers" exit 1 } # Proxy the "help" command... if [ $# -gt 0 ]; then if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then display_help fi else display_help fi # Source the ".env" file so Laravel's environment variables are available... # shellcheck source=/dev/null if [ -n "$APP_ENV" ] && [ -f ./.env."$APP_ENV" ]; then source ./.env."$APP_ENV"; elif [ -f ./.env ]; then source ./.env; fi # Define environment variables... export APP_PORT=${APP_PORT:-80} export APP_SERVICE=${APP_SERVICE:-"laravel.test"} export DB_PORT=${DB_PORT:-3306} export WWWUSER=${WWWUSER:-$UID} export WWWGROUP=${WWWGROUP:-$(id -g)} export SAIL_FILES=${SAIL_FILES:-""} export SAIL_SHARE_DASHBOARD=${SAIL_SHARE_DASHBOARD:-4040} export SAIL_SHARE_SERVER_HOST=${SAIL_SHARE_SERVER_HOST:-"laravel-sail.site"} export SAIL_SHARE_SERVER_PORT=${SAIL_SHARE_SERVER_PORT:-8080} export SAIL_SHARE_SUBDOMAIN=${SAIL_SHARE_SUBDOMAIN:-""} export SAIL_SHARE_DOMAIN=${SAIL_SHARE_DOMAIN:-"$SAIL_SHARE_SERVER_HOST"} export SAIL_SHARE_SERVER=${SAIL_SHARE_SERVER:-""} # Function that outputs Sail is not running... function sail_is_not_running { echo "${BOLD}Sail is not running.${NC}" >&2 echo "" >&2 echo "${BOLD}You may Sail using the following commands:${NC} './vendor/bin/sail up' or './vendor/bin/sail up -d'" >&2 exit 1 } # Define Docker Compose command prefix... if docker compose &> /dev/null; then DOCKER_COMPOSE=(docker compose) else DOCKER_COMPOSE=(docker-compose) fi if [ -n "$SAIL_FILES" ]; then # Convert SAIL_FILES to an array... IFS=':' read -ra SAIL_FILES <<< "$SAIL_FILES" for FILE in "${SAIL_FILES[@]}"; do if [ -f "$FILE" ]; then DOCKER_COMPOSE+=(-f "$FILE") else echo "${BOLD}Unable to find Docker Compose file: '${FILE}'${NC}" >&2 exit 1 fi done fi EXEC="yes" if [ -z "$SAIL_SKIP_CHECKS" ]; then # Ensure that Docker is running... if ! docker info > /dev/null 2>&1; then echo "${BOLD}Docker is not running.${NC}" >&2 exit 1 fi # Determine if Sail is currently up... if "${DOCKER_COMPOSE[@]}" ps "$APP_SERVICE" 2>&1 | grep 'Exit\|exited'; then echo "${BOLD}Shutting down old Sail processes...${NC}" >&2 "${DOCKER_COMPOSE[@]}" down > /dev/null 2>&1 EXEC="no" elif [ -z "$("${DOCKER_COMPOSE[@]}" ps -q)" ]; then EXEC="no" fi fi ARGS=() # Proxy PHP commands to the "php" binary on the application container... if [ "$1" == "php" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" "php" "$@") else sail_is_not_running fi # Proxy vendor binary commands on the application container... elif [ "$1" == "bin" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" ./vendor/bin/"$@") else sail_is_not_running fi # Proxy docker-compose commands to the docker-compose binary on the application container... elif [ "$1" == "docker-compose" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" "${DOCKER_COMPOSE[@]}") else sail_is_not_running fi # Proxy Composer commands to the "composer" binary on the application container... elif [ "$1" == "composer" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" "composer" "$@") else sail_is_not_running fi # Proxy Artisan commands to the "artisan" binary on the application container... elif [ "$1" == "artisan" ] || [ "$1" == "art" ] || [ "$1" == "a" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" php artisan "$@") else sail_is_not_running fi # Proxy the "debug" command to the "php artisan" binary on the application container with xdebug enabled... elif [ "$1" == "debug" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail -e XDEBUG_TRIGGER=1) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" php artisan "$@") else sail_is_not_running fi # Proxy the "test" command to the "php artisan test" Artisan command... elif [ "$1" == "test" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" php artisan test "$@") else sail_is_not_running fi # Proxy the "phpunit" command to "php vendor/bin/phpunit"... elif [ "$1" == "phpunit" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" php vendor/bin/phpunit "$@") else sail_is_not_running fi # Proxy the "pest" command to "php vendor/bin/pest"... elif [ "$1" == "pest" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" php vendor/bin/pest "$@") else sail_is_not_running fi # Proxy the "pint" command to "php vendor/bin/pint"... elif [ "$1" == "pint" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" php vendor/bin/pint "$@") else sail_is_not_running fi # Proxy the "dusk" command to the "php artisan dusk" Artisan command... elif [ "$1" == "dusk" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=(-e "APP_URL=http://${APP_SERVICE}") ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub") ARGS+=("$APP_SERVICE" php artisan dusk "$@") else sail_is_not_running fi # Proxy the "dusk:fails" command to the "php artisan dusk:fails" Artisan command... elif [ "$1" == "dusk:fails" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=(-e "APP_URL=http://${APP_SERVICE}") ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub") ARGS+=("$APP_SERVICE" php artisan dusk:fails "$@") else sail_is_not_running fi # Initiate a Laravel Tinker session within the application container... elif [ "$1" == "tinker" ] ; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" php artisan tinker) else sail_is_not_running fi # Proxy Node commands to the "node" binary on the application container... elif [ "$1" == "node" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" node "$@") else sail_is_not_running fi # Proxy NPM commands to the "npm" binary on the application container... elif [ "$1" == "npm" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" npm "$@") else sail_is_not_running fi # Proxy NPX commands to the "npx" binary on the application container... elif [ "$1" == "npx" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" npx "$@") else sail_is_not_running fi # Proxy PNPM commands to the "pnpm" binary on the application container... elif [ "$1" == "pnpm" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" pnpm "$@") else sail_is_not_running fi # Proxy PNPX commands to the "pnpx" binary on the application container... elif [ "$1" == "pnpx" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" pnpx "$@") else sail_is_not_running fi # Proxy Yarn commands to the "yarn" binary on the application container... elif [ "$1" == "yarn" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" yarn "$@") else sail_is_not_running fi # Proxy Bun commands to the "bun" binary on the application container... elif [ "$1" == "bun" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" bun "$@") else sail_is_not_running fi # Proxy Bun X commands to the "bunx" binary on the application container... elif [ "$1" == "bunx" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" bunx "$@") else sail_is_not_running fi # Initiate a MySQL CLI terminal session within the "mysql" container... elif [ "$1" == "mysql" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec) [ ! -t 0 ] && ARGS+=(-T) ARGS+=(mysql bash -c) ARGS+=("MYSQL_PWD=\${MYSQL_PASSWORD} mysql -u \${MYSQL_USER} \${MYSQL_DATABASE}") else sail_is_not_running fi # Initiate a MySQL CLI terminal session within the "mariadb" container... elif [ "$1" == "mariadb" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec) [ ! -t 0 ] && ARGS+=(-T) ARGS+=(mariadb bash -c) ARGS+=("MYSQL_PWD=\${MYSQL_PASSWORD} mariadb -u \${MYSQL_USER} \${MYSQL_DATABASE}") else sail_is_not_running fi # Initiate a PostgreSQL CLI terminal session within the "pgsql" container... elif [ "$1" == "psql" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec) [ ! -t 0 ] && ARGS+=(-T) ARGS+=(pgsql bash -c) ARGS+=("PGPASSWORD=\${PGPASSWORD} psql -U \${POSTGRES_USER} \${POSTGRES_DB}") else sail_is_not_running fi # Initiate a Bash shell within the application container... elif [ "$1" == "shell" ] || [ "$1" == "bash" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u sail) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" bash "$@") else sail_is_not_running fi # Initiate a root user Bash shell within the application container... elif [ "$1" == "root-shell" ] || [ "$1" == "root-bash" ]; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec -u root) [ ! -t 0 ] && ARGS+=(-T) ARGS+=("$APP_SERVICE" bash "$@") else sail_is_not_running fi # Initiate a Redis CLI terminal session within the "redis" container... elif [ "$1" == "redis" ] ; then shift 1 if [ "$EXEC" == "yes" ]; then ARGS+=(exec) [ ! -t 0 ] && ARGS+=(-T) ARGS+=(redis redis-cli) else sail_is_not_running fi # Share the site... elif [ "$1" == "share" ]; then shift 1 if [ "$EXEC" == "yes" ]; then docker run --init --rm -p "$SAIL_SHARE_DASHBOARD":4040 -t beyondcodegmbh/expose-server:latest share http://host.docker.internal:"$APP_PORT" \ --server-host="$SAIL_SHARE_SERVER_HOST" \ --server-port="$SAIL_SHARE_SERVER_PORT" \ --auth="$SAIL_SHARE_TOKEN" \ --server="$SAIL_SHARE_SERVER" \ --subdomain="$SAIL_SHARE_SUBDOMAIN" \ --domain="$SAIL_SHARE_DOMAIN" \ "$@" exit else sail_is_not_running fi # Open the site... elif [ "$1" == "open" ]; then shift 1 if [ "$EXEC" == "yes" ]; then if [[ -n "$APP_PORT" && "$APP_PORT" != "80" ]]; then FULL_URL="${APP_URL}:${APP_PORT}" else FULL_URL="$APP_URL" fi open "$FULL_URL" exit else sail_is_not_running fi # Pass unknown commands to the "docker-compose" binary... else ARGS+=("$@") fi # Run Docker Compose with the defined arguments... "${DOCKER_COMPOSE[@]}" "${ARGS[@]}" sail/src/SailServiceProvider.php 0000644 00000003357 15060132304 0012722 0 ustar 00 <?php namespace Laravel\Sail; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; use Laravel\Sail\Console\AddCommand; use Laravel\Sail\Console\InstallCommand; use Laravel\Sail\Console\PublishCommand; class SailServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { $this->registerCommands(); $this->configurePublishing(); } /** * Register the console commands for the package. * * @return void */ protected function registerCommands() { if ($this->app->runningInConsole()) { $this->commands([ InstallCommand::class, AddCommand::class, PublishCommand::class, ]); } } /** * Configure publishing for the package. * * @return void */ protected function configurePublishing() { if ($this->app->runningInConsole()) { $this->publishes([ __DIR__ . '/../runtimes' => $this->app->basePath('docker'), ], ['sail', 'sail-docker']); $this->publishes([ __DIR__ . '/../bin/sail' => $this->app->basePath('sail'), ], ['sail', 'sail-bin']); $this->publishes([ __DIR__ . '/../database' => $this->app->basePath('docker'), ], ['sail', 'sail-database']); } } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ InstallCommand::class, PublishCommand::class, ]; } } sail/src/Console/Concerns/InteractsWithDockerComposeServices.php 0000644 00000025366 15060132304 0021130 0 ustar 00 <?php namespace Laravel\Sail\Console\Concerns; use Symfony\Component\Process\Process; use Symfony\Component\Yaml\Yaml; trait InteractsWithDockerComposeServices { /** * The available services that may be installed. * * @var array<string> */ protected $services = [ 'mysql', 'pgsql', 'mariadb', 'redis', 'memcached', 'meilisearch', 'typesense', 'minio', 'mailpit', 'selenium', 'soketi', ]; /** * The default services used when the user chooses non-interactive mode. * * @var string[] */ protected $defaultServices = ['mysql', 'redis', 'selenium', 'mailpit']; /** * Gather the desired Sail services using an interactive prompt. * * @return array */ protected function gatherServicesInteractively() { if (function_exists('\Laravel\Prompts\multiselect')) { return \Laravel\Prompts\multiselect( label: 'Which services would you like to install?', options: $this->services, default: ['mysql'], ); } return $this->choice('Which services would you like to install?', $this->services, 0, null, true); } /** * Build the Docker Compose file. * * @param array $services * @return void */ protected function buildDockerCompose(array $services) { $composePath = base_path('docker-compose.yml'); $compose = file_exists($composePath) ? Yaml::parseFile($composePath) : Yaml::parse(file_get_contents(__DIR__ . '/../../../stubs/docker-compose.stub')); // Prepare the installation of the "mariadb-client" package if the MariaDB service is used... if (in_array('mariadb', $services)) { $compose['services']['laravel.test']['build']['args']['MYSQL_CLIENT'] = 'mariadb-client'; } // Adds the new services as dependencies of the laravel.test service... if (! array_key_exists('laravel.test', $compose['services'])) { $this->warn('Couldn\'t find the laravel.test service. Make sure you add ['.implode(',', $services).'] to the depends_on config.'); } else { $compose['services']['laravel.test']['depends_on'] = collect($compose['services']['laravel.test']['depends_on'] ?? []) ->merge($services) ->unique() ->values() ->all(); } // Add the services to the docker-compose.yml... collect($services) ->filter(function ($service) use ($compose) { return ! array_key_exists($service, $compose['services'] ?? []); })->each(function ($service) use (&$compose) { $compose['services'][$service] = Yaml::parseFile(__DIR__ . "/../../../stubs/{$service}.stub")[$service]; }); // Merge volumes... collect($services) ->filter(function ($service) { return in_array($service, ['mysql', 'pgsql', 'mariadb', 'redis', 'meilisearch', 'typesense', 'minio']); })->filter(function ($service) use ($compose) { return ! array_key_exists($service, $compose['volumes'] ?? []); })->each(function ($service) use (&$compose) { $compose['volumes']["sail-{$service}"] = ['driver' => 'local']; }); // If the list of volumes is empty, we can remove it... if (empty($compose['volumes'])) { unset($compose['volumes']); } // Replace Selenium with ARM base container on Apple Silicon... if (in_array('selenium', $services) && in_array(php_uname('m'), ['arm64', 'aarch64'])) { $compose['services']['selenium']['image'] = 'seleniarm/standalone-chromium'; } file_put_contents($this->laravel->basePath('docker-compose.yml'), Yaml::dump($compose, Yaml::DUMP_OBJECT_AS_MAP)); } /** * Replace the Host environment variables in the app's .env file. * * @param array $services * @return void */ protected function replaceEnvVariables(array $services) { $environment = file_get_contents($this->laravel->basePath('.env')); if (in_array('mysql', $services) || in_array('mariadb', $services) || in_array('pgsql', $services)) { $defaults = [ '# DB_HOST=127.0.0.1', '# DB_PORT=3306', '# DB_DATABASE=laravel', '# DB_USERNAME=root', '# DB_PASSWORD=', ]; foreach ($defaults as $default) { $environment = str_replace($default, substr($default, 2), $environment); } } if (in_array('mysql', $services)) { $environment = preg_replace('/DB_CONNECTION=.*/', 'DB_CONNECTION=mysql', $environment); $environment = str_replace('DB_HOST=127.0.0.1', "DB_HOST=mysql", $environment); }elseif (in_array('pgsql', $services)) { $environment = preg_replace('/DB_CONNECTION=.*/', 'DB_CONNECTION=pgsql', $environment); $environment = str_replace('DB_HOST=127.0.0.1', "DB_HOST=pgsql", $environment); $environment = str_replace('DB_PORT=3306', "DB_PORT=5432", $environment); } elseif (in_array('mariadb', $services)) { if ($this->laravel->config->has('database.connections.mariadb')) { $environment = preg_replace('/DB_CONNECTION=.*/', 'DB_CONNECTION=mariadb', $environment); } $environment = str_replace('DB_HOST=127.0.0.1', "DB_HOST=mariadb", $environment); } $environment = str_replace('DB_USERNAME=root', "DB_USERNAME=sail", $environment); $environment = preg_replace("/DB_PASSWORD=(.*)/", "DB_PASSWORD=password", $environment); if (in_array('memcached', $services)) { $environment = str_replace('MEMCACHED_HOST=127.0.0.1', 'MEMCACHED_HOST=memcached', $environment); } if (in_array('redis', $services)) { $environment = str_replace('REDIS_HOST=127.0.0.1', 'REDIS_HOST=redis', $environment); } if (in_array('meilisearch', $services)) { $environment .= "\nSCOUT_DRIVER=meilisearch"; $environment .= "\nMEILISEARCH_HOST=http://meilisearch:7700\n"; $environment .= "\nMEILISEARCH_NO_ANALYTICS=false\n"; } if (in_array('typesense', $services)) { $environment .= "\nSCOUT_DRIVER=typesense"; $environment .= "\nTYPESENSE_HOST=typesense"; $environment .= "\nTYPESENSE_PORT=8108"; $environment .= "\nTYPESENSE_PROTOCOL=http"; $environment .= "\nTYPESENSE_API_KEY=xyz\n"; } if (in_array('soketi', $services)) { $environment = preg_replace("/^BROADCAST_DRIVER=(.*)/m", "BROADCAST_DRIVER=pusher", $environment); $environment = preg_replace("/^PUSHER_APP_ID=(.*)/m", "PUSHER_APP_ID=app-id", $environment); $environment = preg_replace("/^PUSHER_APP_KEY=(.*)/m", "PUSHER_APP_KEY=app-key", $environment); $environment = preg_replace("/^PUSHER_APP_SECRET=(.*)/m", "PUSHER_APP_SECRET=app-secret", $environment); $environment = preg_replace("/^PUSHER_HOST=(.*)/m", "PUSHER_HOST=soketi", $environment); $environment = preg_replace("/^PUSHER_PORT=(.*)/m", "PUSHER_PORT=6001", $environment); $environment = preg_replace("/^PUSHER_SCHEME=(.*)/m", "PUSHER_SCHEME=http", $environment); $environment = preg_replace("/^VITE_PUSHER_HOST=(.*)/m", "VITE_PUSHER_HOST=localhost", $environment); } if (in_array('mailpit', $services)) { $environment = preg_replace("/^MAIL_MAILER=(.*)/m", "MAIL_MAILER=smtp", $environment); $environment = preg_replace("/^MAIL_HOST=(.*)/m", "MAIL_HOST=mailpit", $environment); $environment = preg_replace("/^MAIL_PORT=(.*)/m", "MAIL_PORT=1025", $environment); } file_put_contents($this->laravel->basePath('.env'), $environment); } /** * Configure PHPUnit to use the dedicated testing database. * * @return void */ protected function configurePhpUnit() { if (! file_exists($path = $this->laravel->basePath('phpunit.xml'))) { $path = $this->laravel->basePath('phpunit.xml.dist'); if (! file_exists($path)) { return; } } $phpunit = file_get_contents($path); $phpunit = preg_replace('/^.*DB_CONNECTION.*\n/m', '', $phpunit); $phpunit = str_replace('<!-- <env name="DB_DATABASE" value=":memory:"/> -->', '<env name="DB_DATABASE" value="testing"/>', $phpunit); file_put_contents($this->laravel->basePath('phpunit.xml'), $phpunit); } /** * Install the devcontainer.json configuration file. * * @return void */ protected function installDevContainer() { if (! is_dir($this->laravel->basePath('.devcontainer'))) { mkdir($this->laravel->basePath('.devcontainer'), 0755, true); } file_put_contents( $this->laravel->basePath('.devcontainer/devcontainer.json'), file_get_contents(__DIR__.'/../../../stubs/devcontainer.stub') ); $environment = file_get_contents($this->laravel->basePath('.env')); $environment .= "\nWWWGROUP=1000"; $environment .= "\nWWWUSER=1000\n"; file_put_contents($this->laravel->basePath('.env'), $environment); } /** * Prepare the installation by pulling and building any necessary images. * * @param array $services * @return void */ protected function prepareInstallation($services) { // Ensure docker is installed... if ($this->runCommands(['docker info > /dev/null 2>&1']) !== 0) { return; } if (count($services) > 0) { $this->runCommands([ './vendor/bin/sail pull '.implode(' ', $services), ]); } $this->runCommands([ './vendor/bin/sail build', ]); } /** * Run the given commands. * * @param array $commands * @return int */ protected function runCommands($commands) { $process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null); if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { try { $process->setTty(true); } catch (\RuntimeException $e) { $this->output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL); } } return $process->run(function ($type, $line) { $this->output->write(' '.$line); }); } } sail/src/Console/AddCommand.php 0000644 00000003145 15060132304 0012362 0 ustar 00 <?php namespace Laravel\Sail\Console; use Illuminate\Console\Command; use Laravel\Sail\Console\Concerns\InteractsWithDockerComposeServices; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'sail:add')] class AddCommand extends Command { use InteractsWithDockerComposeServices; /** * The name and signature of the console command. * * @var string */ protected $signature = 'sail:add {services? : The services that should be added} '; /** * The console command description. * * @var string */ protected $description = 'Add a service to an existing Sail installation'; /** * Execute the console command. * * @return int|null */ public function handle() { if ($this->argument('services')) { $services = $this->argument('services') == 'none' ? [] : explode(',', $this->argument('services')); } elseif ($this->option('no-interaction')) { $services = $this->defaultServices; } else { $services = $this->gatherServicesInteractively(); } if ($invalidServices = array_diff($services, $this->services)) { $this->components->error('Invalid services ['.implode(',', $invalidServices).'].'); return 1; } $this->buildDockerCompose($services); $this->replaceEnvVariables($services); $this->configurePhpUnit(); $this->prepareInstallation($services); $this->output->writeln(''); $this->components->info('Additional Sail services installed successfully.'); } } sail/src/Console/InstallCommand.php 0000644 00000004534 15060132304 0013303 0 ustar 00 <?php namespace Laravel\Sail\Console; use Illuminate\Console\Command; use RuntimeException; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Process\Process; #[AsCommand(name: 'sail:install')] class InstallCommand extends Command { use Concerns\InteractsWithDockerComposeServices; /** * The name and signature of the console command. * * @var string */ protected $signature = 'sail:install {--with= : The services that should be included in the installation} {--devcontainer : Create a .devcontainer configuration directory}'; /** * The console command description. * * @var string */ protected $description = 'Install Laravel Sail\'s default Docker Compose file'; /** * Execute the console command. * * @return int|null */ public function handle() { if ($this->option('with')) { $services = $this->option('with') == 'none' ? [] : explode(',', $this->option('with')); } elseif ($this->option('no-interaction')) { $services = $this->defaultServices; } else { $services = $this->gatherServicesInteractively(); } if ($invalidServices = array_diff($services, $this->services)) { $this->components->error('Invalid services ['.implode(',', $invalidServices).'].'); return 1; } $this->buildDockerCompose($services); $this->replaceEnvVariables($services); $this->configurePhpUnit(); if ($this->option('devcontainer')) { $this->installDevContainer(); } $this->prepareInstallation($services); $this->output->writeln(''); $this->components->info('Sail scaffolding installed successfully. You may run your Docker containers using Sail\'s "up" command.'); $this->output->writeln('<fg=gray>➜</> <options=bold>./vendor/bin/sail up</>'); if (in_array('mysql', $services) || in_array('mariadb', $services) || in_array('pgsql', $services)) { $this->components->warn('A database service was installed. Run "artisan migrate" to prepare your database:'); $this->output->writeln('<fg=gray>➜</> <options=bold>./vendor/bin/sail artisan migrate</>'); } $this->output->writeln(''); } } sail/src/Console/PublishCommand.php 0000644 00000003115 15060132304 0013275 0 ustar 00 <?php namespace Laravel\Sail\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'sail:publish')] class PublishCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'sail:publish'; /** * The console command description. * * @var string */ protected $description = 'Publish the Laravel Sail Docker files'; /** * Execute the console command. * * @return void */ public function handle() { $this->call('vendor:publish', ['--tag' => 'sail-docker']); $this->call('vendor:publish', ['--tag' => 'sail-database']); file_put_contents( $this->laravel->basePath('docker-compose.yml'), str_replace( [ './vendor/laravel/sail/runtimes/8.3', './vendor/laravel/sail/runtimes/8.2', './vendor/laravel/sail/runtimes/8.1', './vendor/laravel/sail/runtimes/8.0', './vendor/laravel/sail/database/mysql', './vendor/laravel/sail/database/pgsql' ], [ './docker/8.3', './docker/8.2', './docker/8.1', './docker/8.0', './docker/mysql', './docker/pgsql' ], file_get_contents($this->laravel->basePath('docker-compose.yml')) ) ); } } sail/LICENSE.md 0000644 00000002117 15060132304 0007073 0 ustar 00 The MIT License (MIT) Copyright (c) Taylor Otwell Copyright (c) Chris Fidao 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. sail/database/pgsql/create-testing-database.sql 0000644 00000000154 15060132304 0015561 0 ustar 00 SELECT 'CREATE DATABASE testing' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'testing')\gexec sail/database/mysql/create-testing-database.sh 0000755 00000000304 15060132304 0015413 0 ustar 00 #!/usr/bin/env bash mysql --user=root --password="$MYSQL_ROOT_PASSWORD" <<-EOSQL CREATE DATABASE IF NOT EXISTS testing; GRANT ALL PRIVILEGES ON \`testing%\`.* TO '$MYSQL_USER'@'%'; EOSQL sail/runtimes/8.2/Dockerfile 0000644 00000006215 15060132304 0011641 0 ustar 00 FROM ubuntu:22.04 LABEL maintainer="Taylor Otwell" ARG WWWGROUP ARG NODE_VERSION=20 ARG POSTGRES_VERSION=15 WORKDIR /var/www/html ENV DEBIAN_FRONTEND noninteractive ENV TZ=UTC ENV SUPERVISOR_PHP_COMMAND="/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80" ENV SUPERVISOR_PHP_USER="sail" RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apt-get update \ && mkdir -p /etc/apt/keyrings \ && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils librsvg2-bin fswatch ffmpeg nano \ && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /etc/apt/keyrings/ppa_ondrej_php.gpg > /dev/null \ && echo "deb [signed-by=/etc/apt/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \ && apt-get update \ && apt-get install -y php8.2-cli php8.2-dev \ php8.2-pgsql php8.2-sqlite3 php8.2-gd php8.2-imagick \ php8.2-curl \ php8.2-imap php8.2-mysql php8.2-mbstring \ php8.2-xml php8.2-zip php8.2-bcmath php8.2-soap \ php8.2-intl php8.2-readline \ php8.2-ldap \ php8.2-msgpack php8.2-igbinary php8.2-redis php8.2-swoole \ php8.2-memcached php8.2-pcov php8.2-xdebug \ && curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_VERSION.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \ && apt-get update \ && apt-get install -y nodejs \ && npm install -g npm \ && npm install -g pnpm \ && npm install -g bun \ && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /etc/apt/keyrings/yarn.gpg >/dev/null \ && echo "deb [signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \ && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/keyrings/pgdg.gpg >/dev/null \ && echo "deb [signed-by=/etc/apt/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list \ && apt-get update \ && apt-get install -y yarn \ && apt-get install -y mysql-client \ && apt-get install -y postgresql-client-$POSTGRES_VERSION \ && apt-get -y autoremove \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.2 RUN groupadd --force -g $WWWGROUP sail RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail COPY start-container /usr/local/bin/start-container COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf COPY php.ini /etc/php/8.2/cli/conf.d/99-sail.ini RUN chmod +x /usr/local/bin/start-container EXPOSE 8000 ENTRYPOINT ["start-container"] sail/runtimes/8.2/php.ini 0000644 00000000141 15060132304 0011127 0 ustar 00 [PHP] post_max_size = 100M upload_max_filesize = 100M variables_order = EGPCS pcov.directory = . sail/runtimes/8.2/start-container 0000644 00000001056 15060132304 0012705 0 ustar 00 #!/usr/bin/env bash if [ "$SUPERVISOR_PHP_USER" != "root" ] && [ "$SUPERVISOR_PHP_USER" != "sail" ]; then echo "You should set SUPERVISOR_PHP_USER to either 'sail' or 'root'." exit 1 fi if [ ! -z "$WWWUSER" ]; then usermod -u $WWWUSER sail fi if [ ! -d /.composer ]; then mkdir /.composer fi chmod -R ugo+rw /.composer if [ $# -gt 0 ]; then if [ "$SUPERVISOR_PHP_USER" = "root" ]; then exec "$@" else exec gosu $WWWUSER "$@" fi else exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf fi sail/runtimes/8.2/supervisord.conf 0000644 00000000521 15060132304 0013075 0 ustar 00 [supervisord] nodaemon=true user=root logfile=/var/log/supervisor/supervisord.log pidfile=/var/run/supervisord.pid [program:php] command=%(ENV_SUPERVISOR_PHP_COMMAND)s user=%(ENV_SUPERVISOR_PHP_USER)s environment=LARAVEL_SAIL="1" stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 sail/runtimes/8.3/Dockerfile 0000644 00000006256 15060132304 0011647 0 ustar 00 FROM ubuntu:22.04 LABEL maintainer="Taylor Otwell" ARG WWWGROUP ARG NODE_VERSION=20 ARG MYSQL_CLIENT="mysql-client" ARG POSTGRES_VERSION=15 WORKDIR /var/www/html ENV DEBIAN_FRONTEND noninteractive ENV TZ=UTC ENV SUPERVISOR_PHP_COMMAND="/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80" ENV SUPERVISOR_PHP_USER="sail" RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apt-get update \ && mkdir -p /etc/apt/keyrings \ && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils librsvg2-bin fswatch ffmpeg nano \ && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /etc/apt/keyrings/ppa_ondrej_php.gpg > /dev/null \ && echo "deb [signed-by=/etc/apt/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \ && apt-get update \ && apt-get install -y php8.3-cli php8.3-dev \ php8.3-pgsql php8.3-sqlite3 php8.3-gd \ php8.3-curl \ php8.3-imap php8.3-mysql php8.3-mbstring \ php8.3-xml php8.3-zip php8.3-bcmath php8.3-soap \ php8.3-intl php8.3-readline \ php8.3-ldap \ php8.3-msgpack php8.3-igbinary php8.3-redis php8.3-swoole \ php8.3-memcached php8.3-pcov php8.3-imagick php8.3-xdebug \ && curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_VERSION.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \ && apt-get update \ && apt-get install -y nodejs \ && npm install -g npm \ && npm install -g pnpm \ && npm install -g bun \ && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /etc/apt/keyrings/yarn.gpg >/dev/null \ && echo "deb [signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \ && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/keyrings/pgdg.gpg >/dev/null \ && echo "deb [signed-by=/etc/apt/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list \ && apt-get update \ && apt-get install -y yarn \ && apt-get install -y $MYSQL_CLIENT \ && apt-get install -y postgresql-client-$POSTGRES_VERSION \ && apt-get -y autoremove \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.3 RUN groupadd --force -g $WWWGROUP sail RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail COPY start-container /usr/local/bin/start-container COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf COPY php.ini /etc/php/8.3/cli/conf.d/99-sail.ini RUN chmod +x /usr/local/bin/start-container EXPOSE 8000 ENTRYPOINT ["start-container"] sail/runtimes/8.3/php.ini 0000644 00000000141 15060132304 0011130 0 ustar 00 [PHP] post_max_size = 100M upload_max_filesize = 100M variables_order = EGPCS pcov.directory = . sail/runtimes/8.3/start-container 0000644 00000001056 15060132304 0012706 0 ustar 00 #!/usr/bin/env bash if [ "$SUPERVISOR_PHP_USER" != "root" ] && [ "$SUPERVISOR_PHP_USER" != "sail" ]; then echo "You should set SUPERVISOR_PHP_USER to either 'sail' or 'root'." exit 1 fi if [ ! -z "$WWWUSER" ]; then usermod -u $WWWUSER sail fi if [ ! -d /.composer ]; then mkdir /.composer fi chmod -R ugo+rw /.composer if [ $# -gt 0 ]; then if [ "$SUPERVISOR_PHP_USER" = "root" ]; then exec "$@" else exec gosu $WWWUSER "$@" fi else exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf fi sail/runtimes/8.3/supervisord.conf 0000644 00000000521 15060132304 0013076 0 ustar 00 [supervisord] nodaemon=true user=root logfile=/var/log/supervisor/supervisord.log pidfile=/var/run/supervisord.pid [program:php] command=%(ENV_SUPERVISOR_PHP_COMMAND)s user=%(ENV_SUPERVISOR_PHP_USER)s environment=LARAVEL_SAIL="1" stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 sail/runtimes/8.0/Dockerfile 0000644 00000006253 15060132304 0011641 0 ustar 00 FROM ubuntu:20.04 LABEL maintainer="Taylor Otwell" ARG WWWGROUP ARG NODE_VERSION=20 ARG POSTGRES_VERSION=13 WORKDIR /var/www/html ENV DEBIAN_FRONTEND noninteractive ENV TZ=UTC ENV SUPERVISOR_PHP_COMMAND="/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80" ENV SUPERVISOR_PHP_USER="sail" RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apt-get update \ && mkdir -p /etc/apt/keyrings \ && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils librsvg2-bin fswatch ffmpeg nano \ && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /usr/share/keyrings/ppa_ondrej_php.gpg > /dev/null \ && echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \ && apt-get update \ && apt-get install -y php8.0-cli php8.0-dev \ php8.0-pgsql php8.0-sqlite3 php8.0-gd php8.0-imagick \ php8.0-curl php8.0-memcached \ php8.0-imap php8.0-mysql php8.0-mbstring \ php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap \ php8.0-intl php8.0-readline php8.0-pcov \ php8.0-msgpack php8.0-igbinary php8.0-ldap \ php8.0-redis php8.0-swoole php8.0-xdebug \ && curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_VERSION.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \ && apt-get update \ && apt-get install -y nodejs \ && npm install -g npm \ && npm install -g bun \ && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null \ && echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \ && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/pgdg.gpg >/dev/null \ && echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt focal-pgdg main" > /etc/apt/sources.list.d/pgdg.list \ && apt-get update \ && apt-get install -y yarn \ && apt-get install -y mysql-client \ && apt-get install -y postgresql-client-$POSTGRES_VERSION \ && apt-get -y autoremove \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN update-alternatives --set php /usr/bin/php8.0 RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.0 RUN groupadd --force -g $WWWGROUP sail RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail COPY start-container /usr/local/bin/start-container COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini RUN chmod +x /usr/local/bin/start-container EXPOSE 8000 ENTRYPOINT ["start-container"] sail/runtimes/8.0/php.ini 0000644 00000000141 15060132304 0011125 0 ustar 00 [PHP] post_max_size = 100M upload_max_filesize = 100M variables_order = EGPCS pcov.directory = . sail/runtimes/8.0/start-container 0000644 00000001056 15060132304 0012703 0 ustar 00 #!/usr/bin/env bash if [ "$SUPERVISOR_PHP_USER" != "root" ] && [ "$SUPERVISOR_PHP_USER" != "sail" ]; then echo "You should set SUPERVISOR_PHP_USER to either 'sail' or 'root'." exit 1 fi if [ ! -z "$WWWUSER" ]; then usermod -u $WWWUSER sail fi if [ ! -d /.composer ]; then mkdir /.composer fi chmod -R ugo+rw /.composer if [ $# -gt 0 ]; then if [ "$SUPERVISOR_PHP_USER" = "root" ]; then exec "$@" else exec gosu $WWWUSER "$@" fi else exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf fi sail/runtimes/8.0/supervisord.conf 0000644 00000000521 15060132304 0013073 0 ustar 00 [supervisord] nodaemon=true user=root logfile=/var/log/supervisor/supervisord.log pidfile=/var/run/supervisord.pid [program:php] command=%(ENV_SUPERVISOR_PHP_COMMAND)s user=%(ENV_SUPERVISOR_PHP_USER)s environment=LARAVEL_SAIL="1" stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 sail/runtimes/8.1/Dockerfile 0000644 00000006173 15060132304 0011643 0 ustar 00 FROM ubuntu:22.04 LABEL maintainer="Taylor Otwell" ARG WWWGROUP ARG NODE_VERSION=20 ARG POSTGRES_VERSION=15 WORKDIR /var/www/html ENV DEBIAN_FRONTEND noninteractive ENV TZ=UTC ENV SUPERVISOR_PHP_COMMAND="/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80" ENV SUPERVISOR_PHP_USER="sail" RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apt-get update \ && mkdir -p /etc/apt/keyrings \ && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils librsvg2-bin fswatch ffmpeg nano \ && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /usr/share/keyrings/ppa_ondrej_php.gpg > /dev/null \ && echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \ && apt-get update \ && apt-get install -y php8.1-cli php8.1-dev \ php8.1-pgsql php8.1-sqlite3 php8.1-gd php8.1-imagick \ php8.1-curl \ php8.1-imap php8.1-mysql php8.1-mbstring \ php8.1-xml php8.1-zip php8.1-bcmath php8.1-soap \ php8.1-intl php8.1-readline \ php8.1-ldap \ php8.1-msgpack php8.1-igbinary php8.1-redis php8.1-swoole \ php8.1-memcached php8.1-pcov php8.1-xdebug \ && curl -sLS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_VERSION.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \ && apt-get update \ && apt-get install -y nodejs \ && npm install -g npm \ && npm install -g bun \ && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarn.gpg >/dev/null \ && echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \ && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/pgdg.gpg >/dev/null \ && echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list \ && apt-get update \ && apt-get install -y yarn \ && apt-get install -y mysql-client \ && apt-get install -y postgresql-client-$POSTGRES_VERSION \ && apt-get -y autoremove \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.1 RUN groupadd --force -g $WWWGROUP sail RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail COPY start-container /usr/local/bin/start-container COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf COPY php.ini /etc/php/8.1/cli/conf.d/99-sail.ini RUN chmod +x /usr/local/bin/start-container EXPOSE 8000 ENTRYPOINT ["start-container"] sail/runtimes/8.1/php.ini 0000644 00000000141 15060132304 0011126 0 ustar 00 [PHP] post_max_size = 100M upload_max_filesize = 100M variables_order = EGPCS pcov.directory = . sail/runtimes/8.1/start-container 0000644 00000001056 15060132304 0012704 0 ustar 00 #!/usr/bin/env bash if [ "$SUPERVISOR_PHP_USER" != "root" ] && [ "$SUPERVISOR_PHP_USER" != "sail" ]; then echo "You should set SUPERVISOR_PHP_USER to either 'sail' or 'root'." exit 1 fi if [ ! -z "$WWWUSER" ]; then usermod -u $WWWUSER sail fi if [ ! -d /.composer ]; then mkdir /.composer fi chmod -R ugo+rw /.composer if [ $# -gt 0 ]; then if [ "$SUPERVISOR_PHP_USER" = "root" ]; then exec "$@" else exec gosu $WWWUSER "$@" fi else exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf fi sail/runtimes/8.1/supervisord.conf 0000644 00000000521 15060132304 0013074 0 ustar 00 [supervisord] nodaemon=true user=root logfile=/var/log/supervisor/supervisord.log pidfile=/var/run/supervisord.pid [program:php] command=%(ENV_SUPERVISOR_PHP_COMMAND)s user=%(ENV_SUPERVISOR_PHP_USER)s environment=LARAVEL_SAIL="1" stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 sail/composer.json 0000644 00000002300 15060132304 0010203 0 ustar 00 { "name": "laravel/sail", "description": "Docker files for running a basic Laravel application.", "keywords": ["laravel", "docker"], "license": "MIT", "support": { "issues": "https://github.com/laravel/sail/issues", "source": "https://github.com/laravel/sail" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.0", "illuminate/console": "^9.52.16|^10.0|^11.0", "illuminate/contracts": "^9.52.16|^10.0|^11.0", "illuminate/support": "^9.52.16|^10.0|^11.0", "symfony/console": "^6.0|^7.0", "symfony/yaml": "^6.0|^7.0" }, "bin": [ "bin/sail" ], "autoload": { "psr-4": { "Laravel\\Sail\\": "src/" } }, "extra": { "laravel": { "providers": [ "Laravel\\Sail\\SailServiceProvider" ] } }, "config": { "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true, "require-dev": { "orchestra/testbench": "^7.0|^8.0|^9.0", "phpstan/phpstan": "^1.10" } } sail/README.md 0000644 00000003722 15060132304 0006751 0 ustar 00 <p align="center"><img src="https://github.com/laravel/sail/raw/HEAD/art/logo.svg" alt="Logo Laravel Sail"></p> <p align="center"> <a href="https://packagist.org/packages/laravel/sail"> <img src="https://img.shields.io/packagist/dt/laravel/sail" alt="Total Downloads"> </a> <a href="https://packagist.org/packages/laravel/sail"> <img src="https://img.shields.io/packagist/v/laravel/sail" alt="Latest Stable Version"> </a> <a href="https://packagist.org/packages/laravel/sail"> <img src="https://img.shields.io/packagist/l/laravel/sail" alt="License"> </a> </p> ## Introduction Sail provides a Docker powered local development experience for Laravel that is compatible with macOS, Windows (WSL2), and Linux. Other than Docker, no software or libraries are required to be installed on your local computer before using Sail. Sail's simple CLI means you can start building your Laravel application without any previous Docker experience. #### Inspiration Laravel Sail is inspired by and derived from [Vessel](https://github.com/shipping-docker/vessel) by [Chris Fidao](https://github.com/fideloper). If you're looking for a thorough introduction to Docker, check out Chris' course: [Shipping Docker](https://serversforhackers.com/shipping-docker). ## Official Documentation Documentation for Sail can be found on the [Laravel website](https://laravel.com/docs/sail). ## Contributing Thank you for considering contributing to Sail! You can read the contribution guide [here](.github/CONTRIBUTING.md). ## 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/sail/security/policy) on how to report security vulnerabilities. ## License Laravel Sail is open-sourced software licensed under the [MIT license](LICENSE.md). framework/config-stubs/app.php 0000644 00000010274 15060132304 0012433 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Application Name |-------------------------------------------------------------------------- | | This value is the name of your application, which will be used when the | framework needs to place the application's name in a notification or | other UI elements where an application name needs to be displayed. | */ 'name' => env('APP_NAME', 'Laravel'), /* |-------------------------------------------------------------------------- | Application Environment |-------------------------------------------------------------------------- | | This value determines the "environment" your application is currently | running in. This may determine how you prefer to configure various | services the application utilizes. Set this in your ".env" file. | */ 'env' => env('APP_ENV', 'production'), /* |-------------------------------------------------------------------------- | Application Debug Mode |-------------------------------------------------------------------------- | | When your application is in debug mode, detailed error messages with | stack traces will be shown on every error that occurs within your | application. If disabled, a simple generic error page is shown. | */ 'debug' => (bool) env('APP_DEBUG', false), /* |-------------------------------------------------------------------------- | Application URL |-------------------------------------------------------------------------- | | This URL is used by the console to properly generate URLs when using | the Artisan command line tool. You should set this to the root of | the application so that it's available within Artisan commands. | */ 'url' => env('APP_URL', 'http://localhost'), /* |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | | Here you may specify the default timezone for your application, which | will be used by the PHP date and date-time functions. The timezone | is set to "UTC" by default as it is suitable for most use cases. | */ 'timezone' => env('APP_TIMEZONE', 'UTC'), /* |-------------------------------------------------------------------------- | Application Locale Configuration |-------------------------------------------------------------------------- | | The application locale determines the default locale that will be used | by Laravel's translation / localization methods. This option can be | set to any locale for which you plan to have translation strings. | */ 'locale' => env('APP_LOCALE', 'en'), 'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'), 'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'), /* |-------------------------------------------------------------------------- | Encryption Key |-------------------------------------------------------------------------- | | This key is utilized by Laravel's encryption services and should be set | to a random, 32 character string to ensure that all encrypted values | are secure. You should do this prior to deploying the application. | */ 'cipher' => 'AES-256-CBC', 'key' => env('APP_KEY'), 'previous_keys' => [ ...array_filter( explode(',', env('APP_PREVIOUS_KEYS', '')) ), ], /* |-------------------------------------------------------------------------- | Maintenance Mode Driver |-------------------------------------------------------------------------- | | These configuration options determine the driver used to determine and | manage Laravel's "maintenance mode" status. The "cache" driver will | allow maintenance mode to be controlled across multiple machines. | | Supported drivers: "file", "cache" | */ 'maintenance' => [ 'driver' => env('APP_MAINTENANCE_DRIVER', 'file'), 'store' => env('APP_MAINTENANCE_STORE', 'database'), ], ]; framework/src/Illuminate/Conditionable/HigherOrderWhenProxy.php 0000644 00000004311 15060132304 0020775 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; } } framework/src/Illuminate/Conditionable/LICENSE.md 0000644 00000002063 15060132304 0015604 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. framework/src/Illuminate/Conditionable/Traits/Conditionable.php 0000644 00000004320 15060132304 0020727 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; } } framework/src/Illuminate/Conditionable/composer.json 0000644 00000001363 15060132304 0016724 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": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Translation/TranslationServiceProvider.php 0000755 00000002656 15060132304 0022002 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']; } } framework/src/Illuminate/Translation/PotentiallyTranslatedString.php 0000644 00000004027 15060132304 0022154 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|float|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; } } framework/src/Illuminate/Translation/LICENSE.md 0000644 00000002063 15060132304 0015330 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. framework/src/Illuminate/Translation/Translator.php 0000755 00000035501 15060132304 0016574 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 = []; /** * The callback that is responsible for handling missing translation keys. * * @var callable|null */ protected $missingTranslationKeyCallback; /** * Indicates whether missing translation keys should be handled. * * @var bool */ protected $handleMissingTranslationKeys = true; /** * 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 $languageLineLocale) { if (! is_null($line = $this->getLine( $namespace, $group, $languageLineLocale, $item, $replace ))) { return $line; } } $key = $this->handleMissingTranslationKey( $key, $replace, $locale, $fallback ); } // 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|float|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 ($value instanceof Closure) { $line = preg_replace_callback( '/<'.$key.'>(.*?)<\/'.$key.'>/', fn ($args) => $value($args[1]), $line ); continue; } 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]); } /** * Handle a missing translation key. * * @param string $key * @param array $replace * @param string|null $locale * @param bool $fallback * @return string */ protected function handleMissingTranslationKey($key, $replace, $locale, $fallback) { if (! $this->handleMissingTranslationKeys || ! isset($this->missingTranslationKeyCallback)) { return $key; } // Prevent infinite loops... $this->handleMissingTranslationKeys = false; $key = call_user_func( $this->missingTranslationKeyCallback, $key, $replace, $locale, $fallback ) ?? $key; $this->handleMissingTranslationKeys = true; return $key; } /** * Register a callback that is responsible for handling missing translation keys. * * @param callable|null $callback * @return static */ public function handleMissingKeysUsing(?callable $callback) { $this->missingTranslationKeyCallback = $callback; return $this; } /** * 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; } } framework/src/Illuminate/Translation/FileLoader.php 0000755 00000012052 15060132304 0016445 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; } } framework/src/Illuminate/Translation/CreatesPotentiallyTranslatedStrings.php 0000644 00000003150 15060132304 0023642 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()); } }; } } framework/src/Illuminate/Translation/composer.json 0000755 00000001677 15060132304 0016463 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.2", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/filesystem": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Translation\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Translation/MessageSelector.php 0000755 00000026627 15060132304 0017541 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; } } } framework/src/Illuminate/Translation/ArrayLoader.php 0000644 00000003117 15060132304 0016643 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 []; } } framework/src/Illuminate/Translation/lang/en/pagination.php 0000644 00000001026 15060132304 0020107 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 »', ]; framework/src/Illuminate/Translation/lang/en/auth.php 0000644 00000001242 15060132304 0016717 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.', ]; framework/src/Illuminate/Translation/lang/en/passwords.php 0000644 00000001350 15060132304 0020003 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 | outcome such as failure due to an invalid password / reset token. | */ '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.", ]; framework/src/Illuminate/Translation/lang/en/validation.php 0000644 00000026271 15060132304 0020121 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.', 'contains' => 'The :attribute field is missing a required value.', '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.', 'extensions' => 'The :attribute field must have one of the following extensions: :values.', '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.', ], 'hex_color' => 'The :attribute field must be a valid hexadecimal color.', '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.', 'list' => 'The :attribute field must be a list.', '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.', 'present_if' => 'The :attribute field must be present when :other is :value.', 'present_unless' => 'The :attribute field must be present unless :other is :value.', 'present_with' => 'The :attribute field must be present when :values is present.', 'present_with_all' => 'The :attribute field must be present when :values are 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_if_declined' => 'The :attribute field is required when :other is declined.', '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' => [], ]; framework/src/Illuminate/Container/BoundMethod.php 0000644 00000015707 15060132304 0016302 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, '@'); } } framework/src/Illuminate/Container/ContextualBindingBuilder.php 0000644 00000004450 15060132304 0021013 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)); } } framework/src/Illuminate/Container/LICENSE.md 0000644 00000002063 15060132304 0014754 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. framework/src/Illuminate/Container/RewindableGenerator.php 0000644 00000002151 15060132304 0020002 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; } } framework/src/Illuminate/Container/EntryNotFoundException.php 0000644 00000000306 15060132304 0020514 0 ustar 00 <?php namespace Illuminate\Container; use Exception; use Psr\Container\NotFoundExceptionInterface; class EntryNotFoundException extends Exception implements NotFoundExceptionInterface { // } framework/src/Illuminate/Container/Util.php 0000644 00000003225 15060132304 0014777 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; } } framework/src/Illuminate/Container/Container.php 0000755 00000120121 15060132304 0016002 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 (is_callable($callback) && ! ($reflector = new ReflectionFunction($callback(...)))->isAnonymous()) { return $reflector->getClosureScopeClass()->name ?? false; } return false; } /** * 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; } } framework/src/Illuminate/Container/composer.json 0000755 00000001610 15060132304 0016072 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.2", "illuminate/contracts": "^11.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": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Support/ValidatedInput.php 0000644 00000030052 15060132304 0016527 0 ustar 00 <?php namespace Illuminate\Support; use ArrayIterator; use Illuminate\Contracts\Support\ValidatedData; use Illuminate\Support\Facades\Date; use stdClass; use Symfony\Component\VarDumper\VarDumper; 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 string|array $key * @return bool */ public function exists($key) { return $this->has($key); } /** * 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->all(), $key)) { return false; } } return true; } /** * Determine if the validated input contains any of the given keys. * * @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 validated input 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 validated input 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 validated input 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 validated input contains a non-empty value for any of the given input items. * * @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 validated input 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 given input item is an empty string. * * @param string $key * @return bool */ protected function isEmptyString($key) { $value = $this->input($key); return ! is_bool($value) && ! is_array($value) && trim((string) $value) === ''; } /** * Determine if the validated input is missing one or more keys. * * @param mixed $keys * @return bool */ public function missing($keys) { return ! $this->has($keys); } /** * Apply the callback if the validated input 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; } /** * Get the keys for all of the input. * * @return array */ public function keys() { return array_keys($this->input()); } /** * Retrieve an input item from the validated input. * * @param string|null $key * @param mixed $default * @return mixed */ public function input($key = null, $default = null) { return data_get( $this->all(), $key, $default ); } /** * Retrieve input from the validated input 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 validated input 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 validated input 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 validated input 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)); } /** * 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->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 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; } /** * 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->all(), $items)); } /** * Get the input 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 the raw, underlying input array. * * @return array */ public function all() { return $this->input; } /** * Dump the validated input 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; } /** * 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 item is set. * * @return bool */ public function __isset($name) { return $this->exists($name); } /** * Remove an input item. * * @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 $this->exists($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); } } framework/src/Illuminate/Support/Sleep.php 0000644 00000025135 15060132304 0014670 0 ustar 00 <?php namespace Illuminate\Support; 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 = []; /** * Keep Carbon's "now" in sync when sleeping. * * @var bool */ protected static $syncWithCarbon = false; /** * 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|float|numeric-string $timestamp * @return static */ public static function until($timestamp) { if (is_numeric($timestamp)) { $timestamp = Carbon::createFromTimestamp($timestamp, date_default_timezone_get()); } 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; if (static::$syncWithCarbon) { Carbon::setTestNow(Carbon::now()->add($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 * @param bool $syncWithCarbon * @return void */ public static function fake($value = true, $syncWithCarbon = false) { static::$fake = $value; static::$sequence = []; static::$fakeSleepCallbacks = []; static::$syncWithCarbon = $syncWithCarbon; } /** * 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, (int) $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; } /** * Indicate that Carbon's "now" should be kept in sync when sleeping. * * @return void */ public static function syncWithCarbon($value = true) { static::$syncWithCarbon = $value; } } framework/src/Illuminate/Support/Onceable.php 0000644 00000004066 15060132304 0015330 0 ustar 00 <?php namespace Illuminate\Support; use Closure; use Laravel\SerializableClosure\Support\ReflectionClosure; class Onceable { /** * Create a new onceable instance. * * @param string $hash * @param object|null $object * @param callable $callable * @return void */ public function __construct( public string $hash, public object|null $object, public $callable ) { // } /** * Tries to create a new onceable instance from the given trace. * * @param array<int, array<string, mixed>> $trace * @return static|null */ public static function tryFromTrace(array $trace, callable $callable) { if (! is_null($hash = static::hashFromTrace($trace, $callable))) { $object = static::objectFromTrace($trace); return new static($hash, $object, $callable); } } /** * Computes the object of the onceable from the given trace, if any. * * @param array<int, array<string, mixed>> $trace * @return object|null */ protected static function objectFromTrace(array $trace) { return $trace[1]['object'] ?? null; } /** * Computes the hash of the onceable from the given trace. * * @param array<int, array<string, mixed>> $trace * @return string|null */ protected static function hashFromTrace(array $trace, callable $callable) { if (str_contains($trace[0]['file'] ?? '', 'eval()\'d code')) { return null; } $uses = array_map( fn (mixed $argument) => is_object($argument) ? spl_object_hash($argument) : $argument, $callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureUsedVariables() : [], ); return md5(sprintf( '%s@%s%s:%s (%s)', $trace[0]['file'], isset($trace[1]['class']) ? ($trace[1]['class'].'@') : '', $trace[1]['function'], $trace[0]['line'], serialize($uses), )); } } framework/src/Illuminate/Support/Fluent.php 0000755 00000011771 15060132304 0015061 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 using "dot" notation. * * @template TGetDefault * * @param TKey $key * @param TGetDefault|(\Closure(): TGetDefault) $default * @return TValue|TGetDefault */ public function get($key, $default = null) { return data_get($this->attributes, $key, $default); } /** * Get an attribute from the fluent instance. * * @param string $key * @param mixed $default * @return mixed */ public function value($key, $default = null) { if (array_key_exists($key, $this->attributes)) { return $this->attributes[$key]; } return value($default); } /** * Get the value of the given key as a new Fluent instance. * * @param string $key * @param mixed $default * @return static */ public function scope($key, $default = null) { return new static( (array) $this->get($key, $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 fluent instance to a Collection. * * @param string|null $key * @return \Illuminate\Support\Collection */ public function collect($key = null) { return new Collection($this->get($key)); } /** * 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->value($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->value($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); } } framework/src/Illuminate/Support/Stringable.php 0000644 00000103015 15060132304 0015704 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\Dumpable; use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Tappable; use JsonSerializable; use Stringable as BaseStringable; class Stringable implements JsonSerializable, ArrayAccess, BaseStringable { use Conditionable, Dumpable, 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|null $encoding * @return static */ 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); } /** * Remove all non-numeric characters from a string. * * @return static */ public function numbers() { return new static(Str::numbers($this->value)); } /** * 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 array|string $pattern * @param \Closure|string[]|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[]|string|null $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 proper case. * * @return static */ public function title() { return new static(Str::title($this->value)); } /** * Convert the given string to proper case for each word. * * @return static */ public function headline() { return new static(Str::headline($this->value)); } /** * Convert the given string to APA-style title case. * * @return static */ public function apa() { return new static(Str::apa($this->value)); } /** * Transliterate a string to its closest ASCII representation. * * @param string|null $unknown * @param bool|null $strict * @return static */ public function transliterate($unknown = '?', $strict = false) { return new static(Str::transliterate($this->value, $unknown, $strict)); } /** * 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(Str::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(Str::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(Str::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)); } /** * Unwrap the string with the given strings. * * @param string $before * @param string|null $after * @return static */ public function unwrap($before, $after = null) { return new static(Str::unwrap($this->value, $before, $after)); } /** * Convert the string into a `HtmlString` instance. * * @return \Illuminate\Support\HtmlString */ public function toHtmlString() { return new HtmlString($this->value); } /** * Convert the string to Base64 encoding. * * @return static */ public function toBase64() { return new static(base64_encode($this->value)); } /** * Decode the Base64 encoded string. * * @param bool $strict * @return static */ public function fromBase64($strict = false) { return new static(base64_decode($this->value, $strict)); } /** * Dump the string. * * @param mixed ...$args * @return $this */ public function dump(...$args) { dump($this->value, ...$args); return $this; } /** * 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. * * @param int $base * @return int */ public function toInteger($base = 10) { return intval($this->value, $base); } /** * 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; } } framework/src/Illuminate/Support/Benchmark.php 0000644 00000003666 15060132304 0015517 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); } } framework/src/Illuminate/Support/DateFactory.php 0000644 00000017410 15060132304 0016022 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()); } } framework/src/Illuminate/Support/LICENSE.md 0000644 00000002063 15060132304 0014506 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. framework/src/Illuminate/Support/NamespacedItemResolver.php 0000755 00000006544 15060132304 0020227 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 = []; } } framework/src/Illuminate/Support/Once.php 0000644 00000003572 15060132304 0014505 0 ustar 00 <?php namespace Illuminate\Support; use WeakMap; class Once { /** * The current globally used instance. * * @var static|null */ protected static ?self $instance = null; /** * Indicates if the once instance is enabled. * * @var bool */ protected static bool $enabled = true; /** * Create a new once instance. * * @param \WeakMap<object, array<string, mixed>> $values * @return void */ protected function __construct(protected WeakMap $values) { // } /** * Create a new once instance. * * @return static */ public static function instance() { return static::$instance ??= new static(new WeakMap); } /** * Get the value of the given onceable. * * @param Onceable $onceable * @return mixed */ public function value(Onceable $onceable) { if (! static::$enabled) { return call_user_func($onceable->callable); } $object = $onceable->object ?: $this; $hash = $onceable->hash; if (isset($this->values[$object][$hash])) { return $this->values[$object][$hash]; } if (! isset($this->values[$object])) { $this->values[$object] = []; } return $this->values[$object][$hash] = call_user_func($onceable->callable); } /** * Re-enable the once instance if it was disabled. * * @return void */ public static function enable() { static::$enabled = true; } /** * Disable the once instance. * * @return void */ public static function disable() { static::$enabled = false; } /** * Flush the once instance. * * @return void */ public static function flush() { static::$instance = null; } } framework/src/Illuminate/Support/DefaultProviders.php 0000644 00000006114 15060132304 0017076 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\Auth\Passwords\PasswordResetServiceProvider::class, \Illuminate\Pipeline\PipelineServiceProvider::class, \Illuminate\Queue\QueueServiceProvider::class, \Illuminate\Redis\RedisServiceProvider::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 $replacements * @return static */ public function replace(array $replacements) { $current = collect($this->providers); foreach ($replacements as $from => $to) { $key = $current->search($from); $current = is_int($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; } } framework/src/Illuminate/Support/Composer.php 0000644 00000015554 15060132304 0015413 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|null $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|null $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; } } framework/src/Illuminate/Support/Env.php 0000644 00000005726 15060132304 0014354 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; }); } } framework/src/Illuminate/Support/ViewErrorBag.php 0000644 00000005137 15060132304 0016156 0 ustar 00 <?php namespace Illuminate\Support; use Countable; use Illuminate\Contracts\Support\MessageBag as MessageBagContract; use Stringable; /** * @mixin \Illuminate\Contracts\Support\MessageBag */ class ViewErrorBag implements Countable, Stringable { /** * 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'); } } framework/src/Illuminate/Support/Traits/ForwardsCalls.php 0000644 00000003521 15060132304 0017627 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 )); } } framework/src/Illuminate/Support/Traits/Tappable.php 0000644 00000000543 15060132304 0016612 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); } } framework/src/Illuminate/Support/Traits/Localizable.php 0000644 00000001165 15060132304 0017304 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); } } } framework/src/Illuminate/Support/Traits/Dumpable.php 0000644 00000000761 15060132304 0016615 0 ustar 00 <?php namespace Illuminate\Support\Traits; trait Dumpable { /** * Dump the given arguments and terminate execution. * * @param mixed ...$args * @return never */ public function dd(...$args) { $this->dump(...$args); dd(); } /** * Dump the given arguments. * * @param mixed ...$args * @return $this */ public function dump(...$args) { dump($this, ...$args); return $this; } } framework/src/Illuminate/Support/Traits/CapsuleManagerTrait.php 0000644 00000002627 15060132304 0020762 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; } } framework/src/Illuminate/Support/Traits/ReflectsClosures.php 0000644 00000004736 15060132304 0020361 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(); } } framework/src/Illuminate/Support/Facades/Request.php 0000755 00000025233 15060132304 0016600 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 \Illuminate\Http\Request dump(mixed $keys = []) * @method static never dd(mixed ...$args) * @method static void macro(string $name, object|callable $macro, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/URL.php 0000755 00000007406 15060132304 0015614 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 query(string $path, array $query = [], 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Auth.php 0000755 00000011775 15060132304 0016057 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 \Illuminate\Contracts\Auth\Guard 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|false loginUsingId(mixed $id, bool $remember = false) * @method static \Illuminate\Contracts\Auth\Authenticatable|false 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) * @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, object|callable $macro = null) * @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); } } framework/src/Illuminate/Support/Facades/Mail.php 0000755 00000012063 15060132304 0016027 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 \Illuminate\Mail\SentMessage|null sendNow(\Illuminate\Contracts\Mail\Mailable|string|array $mailable, 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Password.php 0000755 00000003755 15060132304 0016757 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'; } } framework/src/Illuminate/Support/Facades/View.php 0000755 00000012672 15060132304 0016065 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Lang.php 0000755 00000004156 15060132304 0016032 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|float|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 \Illuminate\Translation\Translator handleMissingKeysUsing(callable|null $callback) * @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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Artisan.php 0000755 00000003565 15060132304 0016555 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\Console\Scheduling\Schedule resolveConsoleSchedule() * @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) * @method static \Illuminate\Foundation\Console\Kernel addCommands(array $commands) * @method static \Illuminate\Foundation\Console\Kernel addCommandPaths(array $paths) * @method static \Illuminate\Foundation\Console\Kernel addCommandRoutePaths(array $paths) * * @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; } } framework/src/Illuminate/Support/Facades/ParallelTesting.php 0000644 00000002344 15060132304 0020235 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; } } framework/src/Illuminate/Support/Facades/Context.php 0000644 00000005134 15060132304 0016567 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static bool has(string $key) * @method static bool hasHidden(string $key) * @method static array all() * @method static array allHidden() * @method static mixed get(string $key, mixed $default = null) * @method static mixed getHidden(string $key, mixed $default = null) * @method static mixed pull(string $key, mixed $default = null) * @method static mixed pullHidden(string $key, mixed $default = null) * @method static array only(array $keys) * @method static array onlyHidden(array $keys) * @method static \Illuminate\Log\Context\Repository add(string|array $key, mixed $value = null) * @method static \Illuminate\Log\Context\Repository addHidden(string|array $key, mixed $value = null) * @method static \Illuminate\Log\Context\Repository forget(string|array $key) * @method static \Illuminate\Log\Context\Repository forgetHidden(string|array $key) * @method static \Illuminate\Log\Context\Repository addIf(string $key, mixed $value) * @method static \Illuminate\Log\Context\Repository addHiddenIf(string $key, mixed $value) * @method static \Illuminate\Log\Context\Repository push(string $key, mixed ...$values) * @method static \Illuminate\Log\Context\Repository pushHidden(string $key, mixed ...$values) * @method static bool isEmpty() * @method static \Illuminate\Log\Context\Repository dehydrating(callable $callback) * @method static \Illuminate\Log\Context\Repository hydrated(callable $callback) * @method static \Illuminate\Log\Context\Repository handleUnserializeExceptionsUsing(callable|null $callback) * @method static \Illuminate\Log\Context\Repository flush() * @method static \Illuminate\Log\Context\Repository|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Log\Context\Repository|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static void macro(string $name, object|callable $macro, object|callable $macro = null) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * @method static \Illuminate\Database\Eloquent\Model restoreModel(\Illuminate\Contracts\Database\ModelIdentifier $value) * * @see \Illuminate\Log\Context\Repository */ class Context extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return \Illuminate\Log\Context\Repository::class; } } framework/src/Illuminate/Support/Facades/Validator.php 0000755 00000003050 15060132304 0017066 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'; } } framework/src/Illuminate/Support/Facades/Storage.php 0000644 00000017035 15060132304 0016552 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 string path(string $path) * @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 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 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 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 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Config.php 0000755 00000002451 15060132304 0016352 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 string string(string $key, \Closure|string|null $default = null) * @method static int integer(string $key, \Closure|int|null $default = null) * @method static float float(string $key, \Closure|float|null $default = null) * @method static bool boolean(string $key, \Closure|bool|null $default = null) * @method static array array(string $key, \Closure|array|null $default = null) * @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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/App.php 0000755 00000021630 15060132304 0015665 0 ustar 00 <?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Foundation\Configuration\ApplicationBuilder configure(string|null $basePath = null) * @method static string inferBasePath() * @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 string getBootstrapProvidersPath() * @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 registered(callable $callback) * @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 void handleRequest(\Illuminate\Http\Request $request) * @method static int handleCommand(\Symfony\Component\Console\Input\InputInterface $input) * @method static bool shouldMergeFrameworkConfiguration() * @method static \Illuminate\Foundation\Application dontMergeFrameworkConfiguration() * @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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Cookie.php 0000755 00000004660 15060132304 0016362 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Session.php 0000755 00000007146 15060132304 0016576 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 array except(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 bool hasAny(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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Schema.php 0000755 00000006211 15060132304 0016343 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 bool createDatabase(string $name) * @method static bool dropDatabaseIfExists(string $name) * @method static bool hasTable(string $table) * @method static bool hasView(string $view) * @method static array getTables() * @method static array getTableListing() * @method static array getViews() * @method static array getTypes() * @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 array getIndexes(string $table) * @method static array getIndexListing(string $table) * @method static bool hasIndex(string $table, string|array $index, string|null $type = null) * @method static array getForeignKeys(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 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) * @method static void macro(string $name, object|callable $macro, object|callable $macro = null) * @method static void mixin(object $mixin, bool $replace = true) * @method static bool hasMacro(string $name) * @method static void flushMacros() * * @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'; } } framework/src/Illuminate/Support/Facades/Exceptions.php 0000644 00000006446 15060132304 0017273 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Support\Arr; use Illuminate\Support\Testing\Fakes\ExceptionHandlerFake; /** * @method static void register() * @method static \Illuminate\Foundation\Exceptions\ReportableHandler reportable(callable $reportUsing) * @method static \Illuminate\Foundation\Exceptions\Handler renderable(callable $renderUsing) * @method static \Illuminate\Foundation\Exceptions\Handler map(\Closure|string $from, \Closure|string|null $to = null) * @method static \Illuminate\Foundation\Exceptions\Handler dontReport(array|string $exceptions) * @method static \Illuminate\Foundation\Exceptions\Handler ignore(array|string $exceptions) * @method static \Illuminate\Foundation\Exceptions\Handler dontFlash(array|string $attributes) * @method static \Illuminate\Foundation\Exceptions\Handler level(string $type, void $level) * @method static void report(\Throwable $e) * @method static bool shouldReport(\Throwable $e) * @method static \Illuminate\Foundation\Exceptions\Handler throttleUsing(callable $throttleUsing) * @method static \Illuminate\Foundation\Exceptions\Handler stopIgnoring(array|string $exceptions) * @method static \Illuminate\Foundation\Exceptions\Handler buildContextUsing(\Closure $contextCallback) * @method static \Symfony\Component\HttpFoundation\Response render(\Illuminate\Http\Request $request, \Throwable $e) * @method static \Illuminate\Foundation\Exceptions\Handler respondUsing(callable $callback) * @method static \Illuminate\Foundation\Exceptions\Handler shouldRenderJsonWhen(callable $callback) * @method static \Illuminate\Foundation\Exceptions\Handler dontReportDuplicates() * @method static \Illuminate\Contracts\Debug\ExceptionHandler handler() * @method static void assertNothingReported() * @method static void assertReported(\Closure|string $exception) * @method static void assertReportedCount(int $count) * @method static void assertNotReported(\Closure|string $exception) * @method static void renderForConsole(\Symfony\Component\Console\Output\OutputInterface $output, \Throwable $e) * @method static \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake throwFirstReported() * @method static \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake setHandler(\Illuminate\Contracts\Debug\ExceptionHandler $handler) * * @see \Illuminate\Foundation\Exceptions\Handler * @see \Illuminate\Contracts\Debug\ExceptionHandler * @see \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake */ class Exceptions extends Facade { /** * Replace the bound instance with a fake. * * @param array<int, class-string<\Throwable>>|class-string<\Throwable> $exceptions * @return \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake */ public static function fake(array|string $exceptions = []) { $exceptionHandler = static::isFake() ? static::getFacadeRoot()->handler() : static::getFacadeRoot(); return tap(new ExceptionHandlerFake($exceptionHandler, Arr::wrap($exceptions)), function ($fake) { static::swap($fake); }); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return ExceptionHandler::class; } } framework/src/Illuminate/Support/Facades/Schedule.php 0000644 00000002766 15060132304 0016707 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Console\Scheduling\Schedule as ConsoleSchedule; /** * @method static \Illuminate\Console\Scheduling\CallbackEvent call(string|callable $callback, array $parameters = []) * @method static \Illuminate\Console\Scheduling\Event command(string $command, array $parameters = []) * @method static \Illuminate\Console\Scheduling\CallbackEvent job(object|string $job, string|null $queue = null, string|null $connection = null) * @method static \Illuminate\Console\Scheduling\Event exec(string $command, array $parameters = []) * @method static string compileArrayInput(string|int $key, array $value) * @method static bool serverShouldRun(\Illuminate\Console\Scheduling\Event $event, \DateTimeInterface $time) * @method static \Illuminate\Support\Collection dueEvents(\Illuminate\Contracts\Foundation\Application $app) * @method static \Illuminate\Console\Scheduling\Event[] events() * @method static \Illuminate\Console\Scheduling\Schedule useCache(string $store) * @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\Console\Scheduling\Schedule */ class Schedule extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return ConsoleSchedule::class; } } framework/src/Illuminate/Support/Facades/Redis.php 0000755 00000004025 15060132304 0016212 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Route.php 0000755 00000020116 15060132304 0016241 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 \Illuminate\Routing\Router substituteImplicitBindingsUsing(callable $callback) * @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, object|callable $macro = null) * @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 missing(\Closure $missing) * @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'; } } framework/src/Illuminate/Support/Facades/DB.php 0000644 00000017472 15060132304 0015440 0 ustar 00 <?php namespace Illuminate\Support\Facades; use Illuminate\Database\Console\Migrations\FreshCommand; use Illuminate\Database\Console\Migrations\RefreshCommand; use Illuminate\Database\Console\Migrations\ResetCommand; use Illuminate\Database\Console\WipeCommand; /** * @method static \Illuminate\Database\Connection connection(string|null $name = null) * @method static \Illuminate\Database\ConnectionInterface connectUsing(string $name, array $config, bool $force = false) * @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, object|callable $macro = null) * @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 beforeStartingTransaction(\Closure $callback) * @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 \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 string getServerVersion() * @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 { /** * Indicate if destructive Artisan commands should be prohibited. * * Prohibits: db:wipe, migrate:fresh, migrate:refresh, and migrate:reset * * @param bool $prohibit * @return void */ public static function prohibitDestructiveCommands(bool $prohibit = true) { FreshCommand::prohibit($prohibit); RefreshCommand::prohibit($prohibit); ResetCommand::prohibit($prohibit); WipeCommand::prohibit($prohibit); } /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'db'; } } framework/src/Illuminate/Support/Facades/Redirect.php 0000755 00000004521 15060132304 0016706 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Broadcast.php 0000644 00000005077 15060132304 0017053 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\AnonymousEvent on(\Illuminate\Broadcasting\Channel|array|string $channels) * @method static \Illuminate\Broadcasting\AnonymousEvent private(string $channel) * @method static \Illuminate\Broadcasting\AnonymousEvent presence(string $channel) * @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; } } framework/src/Illuminate/Support/Facades/Crypt.php 0000755 00000001552 15060132304 0016247 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() * @method static array getAllKeys() * @method static array getPreviousKeys() * @method static \Illuminate\Encryption\Encrypter previousKeys(array $keys) * * @see \Illuminate\Encryption\Encrypter */ class Crypt extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'encrypter'; } } framework/src/Illuminate/Support/Facades/Facade.php 0000755 00000021547 15060132304 0016317 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\Number; 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|null */ 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, 'Context' => Context::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, 'Number' => Number::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, 'Schedule' => Schedule::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|null */ public static function getFacadeApplication() { return static::$app; } /** * Set the application instance. * * @param \Illuminate\Contracts\Foundation\Application|null $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); } } framework/src/Illuminate/Support/Facades/Hash.php 0000755 00000002550 15060132304 0016030 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'; } } framework/src/Illuminate/Support/Facades/Vite.php 0000644 00000003613 15060132304 0016052 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 \Illuminate\Foundation\Vite createAssetPathsUsing(callable|null $resolver) * @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, object|callable $macro = null) * @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; } } framework/src/Illuminate/Support/Facades/RateLimiter.php 0000644 00000002361 15060132304 0017363 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 int increment(string $key, int $decaySeconds = 60, int $amount = 1) * @method static int decrement(string $key, int $decaySeconds = 60, int $amount = 1) * @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; } } framework/src/Illuminate/Support/Facades/Cache.php 0000755 00000007467 15060132304 0016164 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, array $config = []) * @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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Bus.php 0000644 00000011344 15060132304 0015674 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 \Illuminate\Support\Testing\Fakes\ChainedBatchTruthTest chainedBatch(\Closure $callback) * @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; } } framework/src/Illuminate/Support/Facades/Pipeline.php 0000644 00000002352 15060132304 0016707 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) * @method static \Illuminate\Pipeline\Pipeline|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * @method static \Illuminate\Pipeline\Pipeline|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null) * * @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'; } } framework/src/Illuminate/Support/Facades/Blade.php 0000755 00000005617 15060132304 0016163 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'; } } framework/src/Illuminate/Support/Facades/Notification.php 0000644 00000007644 15060132304 0017601 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 \Illuminate\Support\Testing\Fakes\NotificationFake serializeAndRestore(bool $serializeAndRestore = true) * @method static array sentNotifications() * @method static void macro(string $name, object|callable $macro, object|callable $macro = null) * @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 on the given channels. * * @param array $channels * @return \Illuminate\Notifications\AnonymousNotifiable */ public static function routes(array $channels) { $notifiable = new AnonymousNotifiable; foreach ($channels as $channel => $route) { $notifiable->route($channel, $route); } return $notifiable; } /** * 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; } } framework/src/Illuminate/Support/Facades/Date.php 0000644 00000012534 15060132304 0016022 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); } } framework/src/Illuminate/Support/Facades/Queue.php 0000755 00000011157 15060132304 0016234 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 getJobTries(mixed $job) * @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 assertCount(int $expectedCount) * @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'; } } framework/src/Illuminate/Support/Facades/Http.php 0000644 00000022131 15060132304 0016056 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 \Illuminate\Http\Client\Factory globalOptions(\Closure|array $options) * @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\Http\Client\PendingRequest createPendingRequest() * @method static \Illuminate\Contracts\Events\Dispatcher|null getDispatcher() * @method static array getGlobalMiddleware() * @method static void macro(string $name, object|callable $macro, object|callable $macro = null) * @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(\Psr\Http\Message\StreamInterface|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(array|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) * @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)); }); } } framework/src/Illuminate/Support/Facades/Response.php 0000755 00000005421 15060132304 0016743 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\StreamedJsonResponse streamJson(array $data, int $status = 200, array $headers = [], int $encodingOptions = 15) * @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, object|callable $macro = null) * @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; } } framework/src/Illuminate/Support/Facades/Event.php 0000755 00000011422 15060132304 0016224 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, object|callable $macro = null) * @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) * @method static array dispatchedEvents() * * @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'; } } framework/src/Illuminate/Support/Facades/Log.php 0000755 00000005440 15060132304 0015667 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 withoutContext() * @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|\Stringable $message, array $context = []) * @method static void alert(string|\Stringable $message, array $context = []) * @method static void critical(string|\Stringable $message, array $context = []) * @method static void error(string|\Stringable $message, array $context = []) * @method static void warning(string|\Stringable $message, array $context = []) * @method static void notice(string|\Stringable $message, array $context = []) * @method static void info(string|\Stringable $message, array $context = []) * @method static void debug(string|\Stringable $message, array $context = []) * @method static void log(mixed $level, string|\Stringable $message, array $context = []) * @method static \Illuminate\Log\LogManager setApplication(\Illuminate\Contracts\Foundation\Application $app) * @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 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'; } } framework/src/Illuminate/Support/Facades/Process.php 0000644 00000010115 15060132304 0016554 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|null $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, object|callable $macro = null) * @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|null $callback = null) { return tap(static::getFacadeRoot(), function ($fake) use ($callback) { static::swap($fake->fake($callback)); }); } } framework/src/Illuminate/Support/Facades/File.php 0000755 00000007531 15060132304 0016030 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, bool $lock = false) * @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 bool|null 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, object|callable $macro = null) * @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'; } } framework/src/Illuminate/Support/Facades/Gate.php 0000644 00000005357 15060132304 0016032 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(iterable|string $ability, array|mixed $arguments = []) * @method static bool denies(iterable|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; } } framework/src/Illuminate/Support/AggregateServiceProvider.php 0000644 00000001743 15060132304 0020541 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; } } framework/src/Illuminate/Support/Timebox.php 0000644 00000003240 15060132304 0015220 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 * * @throws \Throwable */ 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); } } framework/src/Illuminate/Support/Number.php 0000644 00000020137 15060132304 0015045 0 ustar 00 <?php namespace Illuminate\Support; use Illuminate\Support\Traits\Macroable; use NumberFormatter; use RuntimeException; class Number { use Macroable; /** * The current default locale. * * @var string */ protected static $locale = 'en'; /** * Format the given number according to the current locale. * * @param int|float $number * @param int|null $precision * @param int|null $maxPrecision * @param string|null $locale * @return string|false */ public static function format(int|float $number, ?int $precision = null, ?int $maxPrecision = null, ?string $locale = null) { static::ensureIntlExtensionIsInstalled(); $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::DECIMAL); if (! is_null($maxPrecision)) { $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision); } elseif (! is_null($precision)) { $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); } return $formatter->format($number); } /** * Spell out the given number in the given locale. * * @param int|float $number * @param string|null $locale * @param int|null $after * @param int|null $until * @return string */ public static function spell(int|float $number, ?string $locale = null, ?int $after = null, ?int $until = null) { static::ensureIntlExtensionIsInstalled(); if (! is_null($after) && $number <= $after) { return static::format($number, locale: $locale); } if (! is_null($until) && $number >= $until) { return static::format($number, locale: $locale); } $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::SPELLOUT); return $formatter->format($number); } /** * Convert the given number to ordinal form. * * @param int|float $number * @param string|null $locale * @return string */ public static function ordinal(int|float $number, ?string $locale = null) { static::ensureIntlExtensionIsInstalled(); $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::ORDINAL); return $formatter->format($number); } /** * Convert the given number to its percentage equivalent. * * @param int|float $number * @param int $precision * @param int|null $maxPrecision * @param string|null $locale * @return string|false */ public static function percentage(int|float $number, int $precision = 0, ?int $maxPrecision = null, ?string $locale = null) { static::ensureIntlExtensionIsInstalled(); $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::PERCENT); if (! is_null($maxPrecision)) { $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $maxPrecision); } else { $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); } return $formatter->format($number / 100); } /** * Convert the given number to its currency equivalent. * * @param int|float $number * @param string $in * @param string|null $locale * @return string|false */ public static function currency(int|float $number, string $in = 'USD', ?string $locale = null) { static::ensureIntlExtensionIsInstalled(); $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::CURRENCY); return $formatter->formatCurrency($number, $in); } /** * Convert the given number to its file size equivalent. * * @param int|float $bytes * @param int $precision * @param int|null $maxPrecision * @return string */ public static function fileSize(int|float $bytes, int $precision = 0, ?int $maxPrecision = null) { $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; for ($i = 0; ($bytes / 1024) > 0.9 && ($i < count($units) - 1); $i++) { $bytes /= 1024; } return sprintf('%s %s', static::format($bytes, $precision, $maxPrecision), $units[$i]); } /** * Convert the number to its human-readable equivalent. * * @param int|float $number * @param int $precision * @param int|null $maxPrecision * @return bool|string */ public static function abbreviate(int|float $number, int $precision = 0, ?int $maxPrecision = null) { return static::forHumans($number, $precision, $maxPrecision, abbreviate: true); } /** * Convert the number to its human-readable equivalent. * * @param int|float $number * @param int $precision * @param int|null $maxPrecision * @param bool $abbreviate * @return bool|string */ public static function forHumans(int|float $number, int $precision = 0, ?int $maxPrecision = null, bool $abbreviate = false) { return static::summarize($number, $precision, $maxPrecision, $abbreviate ? [ 3 => 'K', 6 => 'M', 9 => 'B', 12 => 'T', 15 => 'Q', ] : [ 3 => ' thousand', 6 => ' million', 9 => ' billion', 12 => ' trillion', 15 => ' quadrillion', ]); } /** * Convert the number to its human-readable equivalent. * * @param int|float $number * @param int $precision * @param int|null $maxPrecision * @param array $units * @return string|false */ protected static function summarize(int|float $number, int $precision = 0, ?int $maxPrecision = null, array $units = []) { if (empty($units)) { $units = [ 3 => 'K', 6 => 'M', 9 => 'B', 12 => 'T', 15 => 'Q', ]; } switch (true) { case floatval($number) === 0.0: return $precision > 0 ? static::format(0, $precision, $maxPrecision) : '0'; case $number < 0: return sprintf('-%s', static::summarize(abs($number), $precision, $maxPrecision, $units)); case $number >= 1e15: return sprintf('%s'.end($units), static::summarize($number / 1e15, $precision, $maxPrecision, $units)); } $numberExponent = floor(log10($number)); $displayExponent = $numberExponent - ($numberExponent % 3); $number /= pow(10, $displayExponent); return trim(sprintf('%s%s', static::format($number, $precision, $maxPrecision), $units[$displayExponent] ?? '')); } /** * Clamp the given number between the given minimum and maximum. * * @param int|float $number * @param int|float $min * @param int|float $max * @return int|float */ public static function clamp(int|float $number, int|float $min, int|float $max) { return min(max($number, $min), $max); } /** * Execute the given callback using the given locale. * * @param string $locale * @param callable $callback * @return mixed */ public static function withLocale(string $locale, callable $callback) { $previousLocale = static::$locale; static::useLocale($locale); return tap($callback(), fn () => static::useLocale($previousLocale)); } /** * Set the default locale. * * @param string $locale * @return void */ public static function useLocale(string $locale) { static::$locale = $locale; } /** * Ensure the "intl" PHP extension is installed. * * @return void */ protected static function ensureIntlExtensionIsInstalled() { if (! extension_loaded('intl')) { $method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; throw new RuntimeException('The "intl" PHP extension is required to use the ['.$method.'] method.'); } } } framework/src/Illuminate/Support/Carbon.php 0000644 00000001602 15060132304 0015015 0 ustar 00 <?php namespace Illuminate\Support; use Carbon\Carbon as BaseCarbon; use Carbon\CarbonImmutable as BaseCarbonImmutable; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Dumpable; use Ramsey\Uuid\Uuid; use Symfony\Component\Uid\Ulid; class Carbon extends BaseCarbon { use Conditionable, Dumpable; /** * {@inheritdoc} */ public static function setTestNow(mixed $testNow = null): void { BaseCarbon::setTestNow($testNow); BaseCarbonImmutable::setTestNow($testNow); } /** * Create a Carbon instance from a given ordered UUID or ULID. */ public static function createFromId(Uuid|Ulid|string $id): static { if (is_string($id)) { $id = Ulid::isValid($id) ? Ulid::fromString($id) : Uuid::fromString($id); } return static::createFromInterface($id->getDateTime()); } } framework/src/Illuminate/Support/MultipleInstanceManager.php 0000644 00000010427 15060132304 0020371 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 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 * @throws \RuntimeException */ 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); } } framework/src/Illuminate/Support/composer.json 0000644 00000003462 15060132304 0015630 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.2", "ext-ctype": "*", "ext-filter": "*", "ext-mbstring": "*", "doctrine/inflector": "^2.0", "illuminate/collections": "^11.0", "illuminate/conditionable": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "nesbot/carbon": "^2.72.2|^3.0", "voku/portable-ascii": "^2.0" }, "conflict": { "tightenco/collect": "<5.5.33" }, "replace": { "spatie/once": "*" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" }, "files": [ "helpers.php" ] }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "illuminate/filesystem": "Required to use the composer class (^11.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 (^7.0).", "symfony/uid": "Required to use Str::ulid() (^7.0).", "symfony/var-dumper": "Required to use the dd function (^7.0).", "vlucas/phpdotenv": "Required to use the Env class and env helper (^5.4.1)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Support/InteractsWithTime.php 0000644 00000004133 15060132304 0017222 0 ustar 00 <?php namespace Illuminate\Support; use Carbon\CarbonInterval; 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(); } /** * Given a start time, format the total run time for human readability. * * @param float $startTime * @param float $endTime * @return string */ protected function runTimeForHumans($startTime, $endTime = null) { $endTime ??= microtime(true); $runTime = ($endTime - $startTime) * 1000; return $runTime > 1000 ? CarbonInterval::milliseconds($runTime)->cascade()->forHumans(short: true) : number_format($runTime, 2).'ms'; } } framework/src/Illuminate/Support/helpers.php 0000644 00000027212 15060132304 0015260 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\Fluent; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Once; use Illuminate\Support\Onceable; 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; } if ($value instanceof Stringable) { return trim((string) $value) === ''; } 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|int|float|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('fluent')) { /** * Create an Fluent object from the given value. * * @param object|array $value * @return \Illuminate\Support\Fluent */ function fluent($value) { return new Fluent($value); } } if (! function_exists('literal')) { /** * Return a new literal or anonymous object using named arguments. * * @return \stdClass */ function literal(...$arguments) { if (count($arguments) === 1 && array_is_list($arguments)) { return $arguments[0]; } return (object) $arguments; } } 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('once')) { /** * Ensures a callable is only called once, and returns the result on subsequent calls. * * @template TReturnType * * @param callable(): TReturnType $callback * @return TReturnType */ function once(callable $callback) { $onceable = Onceable::tryFromTrace( debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2), $callback, ); return $onceable ? Once::instance()->value($onceable) : call_user_func($callback); } } 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 \Throwable */ 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 (Throwable $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); } } framework/src/Illuminate/Support/Str.php 0000644 00000154047 15060132304 0014375 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); } /** * Unwrap the string with the given strings. * * @param string $value * @param string $before * @param string|null $after * @return string */ public static function unwrap($value, $before, $after = null) { if (static::startsWith($value, $before)) { $value = static::substr($value, static::length($before)); } if (static::endsWith($value, $after ??= $before)) { $value = static::substr($value, 0, -static::length($after)); } return $value; } /** * 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-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}$/D', $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]); } /** * Remove all non-numeric characters from a string. * * @param string $value * @return string */ public static function numbers($value) { return preg_replace('/[^0-9]/', '', $value); } /** * 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 = ' ') { if (function_exists('mb_str_pad')) { return mb_str_pad($value, $length, $pad, STR_PAD_BOTH); } $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 = ' ') { if (function_exists('mb_str_pad')) { return mb_str_pad($value, $length, $pad, STR_PAD_LEFT); } $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 = ' ') { if (function_exists('mb_str_pad')) { return mb_str_pad($value, $length, $pad, STR_PAD_RIGHT); } $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) { if (static::contains($callback, "@anonymous\0")) { if (static::substrCount($callback, '@') > 1) { return [ static::beforeLast($callback, '@'), static::afterLast($callback, '@'), ]; } return [$callback, $default]; } 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 array|string $pattern * @param \Closure|string[]|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 proper 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 proper 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))); } /** * Convert the given string to APA-style title case. * * See: https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case * * @param string $value * @return string */ public static function apa($value) { if (trim($value) === '') { return $value; } $minorWords = [ 'and', 'as', 'but', 'for', 'if', 'nor', 'or', 'so', 'yet', 'a', 'an', 'the', 'at', 'by', 'for', 'in', 'of', 'off', 'on', 'per', 'to', 'up', 'via', 'et', 'ou', 'un', 'une', 'la', 'le', 'les', 'de', 'du', 'des', 'par', 'à', ]; $endPunctuation = ['.', '!', '?', ':', '—', ',']; $words = preg_split('/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY); for ($i = 0; $i < count($words); $i++) { $lowercaseWord = mb_strtolower($words[$i]); if (str_contains($lowercaseWord, '-')) { $hyphenatedWords = explode('-', $lowercaseWord); $hyphenatedWords = array_map(function ($part) use ($minorWords) { return (in_array($part, $minorWords) && mb_strlen($part) <= 3) ? $part : mb_strtoupper(mb_substr($part, 0, 1)).mb_substr($part, 1); }, $hyphenatedWords); $words[$i] = implode('-', $hyphenatedWords); } else { if (in_array($lowercaseWord, $minorWords) && mb_strlen($lowercaseWord) <= 3 && ! ($i === 0 || in_array(mb_substr($words[$i - 1], -1), $endPunctuation))) { $words[$i] = $lowercaseWord; } else { $words[$i] = mb_strtoupper(mb_substr($lowercaseWord, 0, 1)).mb_substr($lowercaseWord, 1); } } } return implode(' ', $words); } /** * 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 whitespace from both ends of a string. * * @param string $value * @param string|null $charlist * @return string */ public static function trim($value, $charlist = null) { if ($charlist === null) { return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+|[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? trim($value); } return trim($value, $charlist); } /** * Remove all whitespace from the beginning of a string. * * @param string $value * @param string|null $charlist * @return string */ public static function ltrim($value, $charlist = null) { if ($charlist === null) { return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}]+~u', '', $value) ?? ltrim($value); } return ltrim($value, $charlist); } /** * Remove all whitespace from the end of a string. * * @param string $value * @param string|null $charlist * @return string */ public static function rtrim($value, $charlist = null) { if ($charlist === null) { return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}]+$~u', '', $value) ?? rtrim($value); } return rtrim($value, $charlist); } /** * 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', ' ', static::trim($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); } /** * Convert the given string to Base64 encoding. * * @param string $string * @return string */ public static function toBase64($string): string { return base64_encode($string); } /** * Decode the given Base64 encoded string. * * @param string $string * @param bool $strict * @return string|false */ public static function fromBase64($string, $strict = false) { return base64_decode($string, $strict); } /** * 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. * * @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 = []; } } framework/src/Illuminate/Support/Pluralizer.php 0000755 00000005534 15060132304 0015755 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; } } framework/src/Illuminate/Support/ConfigurationUrlParser.php 0000644 00000010403 15060132304 0020257 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; } } framework/src/Illuminate/Support/HigherOrderTapProxy.php 0000644 00000001231 15060132304 0017520 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; } } framework/src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php 0000644 00000007066 15060132304 0022201 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(); } /** * Rollback the last database transaction for the connection. * * @return void */ public function rollBack() { // } } framework/src/Illuminate/Support/Testing/Fakes/BatchFake.php 0000644 00000007502 15060132304 0020074 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; } } framework/src/Illuminate/Support/Testing/Fakes/PendingMailFake.php 0000644 00000002275 15060132304 0021244 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)); } /** * Send a new mailable message instance synchronously. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return void */ public function sendNow(Mailable $mailable) { $this->mailer->sendNow($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)); } } framework/src/Illuminate/Support/Testing/Fakes/PendingChainFake.php 0000644 00000002632 15060132304 0021401 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); } } framework/src/Illuminate/Support/Testing/Fakes/BusFake.php 0000644 00000061104 15060132304 0017602 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; use Illuminate\Bus\BatchRepository; use Illuminate\Bus\ChainedBatch; 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; use RuntimeException; 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 ($command instanceof ChainedBatchTruthTest) { $instance = $command; $command = ChainedBatch::class; $callback = fn ($job) => $instance($job->toPendingBatch()); } 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->assertDispatchedWithChainOfObjects($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->assertDispatchedWithChainOfObjects($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 = $expectedChain; PHPUnit::assertTrue( $this->dispatched($command, $callback)->filter(function ($job) use ($chain) { if (count($chain) !== count($job->chained)) { return false; } foreach ($job->chained as $index => $serializedChainedJob) { if ($chain[$index] instanceof ChainedBatchTruthTest) { $chainedBatch = unserialize($serializedChainedJob); if (! $chainedBatch instanceof ChainedBatch || ! $chain[$index]($chainedBatch->toPendingBatch())) { return false; } } elseif ($chain[$index] instanceof Closure) { [$expectedType, $callback] = [$this->firstClosureParameterType($chain[$index]), $chain[$index]]; $chainedJob = unserialize($serializedChainedJob); if (! $chainedJob instanceof $expectedType) { throw new RuntimeException('The chained job was expected to be of type '.$expectedType.', '.$chainedJob::class.' chained.'); } if (! $callback($chainedJob)) { return false; } } elseif (is_string($chain[$index])) { if ($chain[$index] != get_class(unserialize($serializedChainedJob))) { return false; } } elseif (serialize($chain[$index]) != $serializedChainedJob) { return false; } } return true; })->isNotEmpty(), 'The expected chain was not dispatched.' ); } /** * Create a new assertion about a chained batch. * * @param \Closure $callback * @return \Illuminate\Support\Testing\Fakes\ChainedBatchTruthTest */ public function chainedBatch(Closure $callback) { return new ChainedBatchTruthTest($callback); } /** * 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); $jobs = ChainedBatch::prepareNestedBatches($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; } } framework/src/Illuminate/Support/Testing/Fakes/MailFake.php 0000644 00000033407 15060132304 0017740 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 mixed|void */ public function send($view, array $data = [], $callback = null) { return $this->sendMail($view, $view instanceof ShouldQueue); } /** * Send a new message synchronously using a view. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $mailable * @param array $data * @param \Closure|string|null $callback * @return void */ public function sendNow($mailable, array $data = [], $callback = null) { return $this->sendMail($mailable, shouldQueue: false); } /** * Send a new message using a view. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $view * @param bool $shouldQueue * @return mixed|void */ protected function sendMail($view, $shouldQueue = false) { if (! $view instanceof Mailable) { return; } $view->mailer($this->currentMailer); if ($shouldQueue) { return $this->queue($view); } $this->currentMailer = null; $this->mailables[] = $view; } /** * Queue a new 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); } } framework/src/Illuminate/Support/Testing/Fakes/ExceptionHandlerFake.php 0000644 00000015135 15060132304 0022310 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Foundation\Testing\Concerns\WithoutExceptionHandlingHandler; use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\ReflectsClosures; use Illuminate\Testing\Assert; use PHPUnit\Framework\Assert as PHPUnit; use PHPUnit\Framework\ExpectationFailedException; use Throwable; /** * @mixin \Illuminate\Foundation\Exceptions\Handler */ class ExceptionHandlerFake implements ExceptionHandler, Fake { use ForwardsCalls, ReflectsClosures; /** * All of the exceptions that have been reported. * * @var array<int, \Throwable> */ protected $reported = []; /** * If the fake should throw exceptions when they are reported. * * @var bool */ protected $throwOnReport = false; /** * Create a new exception handler fake. * * @param \Illuminate\Contracts\Debug\ExceptionHandler $handler * @param array<int, class-string<\Throwable>> $exceptions * @return void */ public function __construct( protected ExceptionHandler $handler, protected array $exceptions = [], ) { // } /** * Get the underlying handler implementation. * * @return \Illuminate\Contracts\Debug\ExceptionHandler */ public function handler() { return $this->handler; } /** * Assert if an exception of the given type has been reported. * * @param \Closure|string $exception * @return void */ public function assertReported(Closure|string $exception) { $message = sprintf( 'The expected [%s] exception was not reported.', is_string($exception) ? $exception : $this->firstClosureParameterType($exception) ); if (is_string($exception)) { Assert::assertTrue( in_array($exception, array_map('get_class', $this->reported), true), $message, ); return; } Assert::assertTrue( collect($this->reported)->contains( fn (Throwable $e) => $this->firstClosureParameterType($exception) === get_class($e) && $exception($e) === true, ), $message, ); } /** * Assert the number of exceptions that have been reported. * * @param int $count * @return void */ public function assertReportedCount(int $count) { $total = collect($this->reported)->count(); PHPUnit::assertSame( $count, $total, "The total number of exceptions reported was {$total} instead of {$count}." ); } /** * Assert if an exception of the given type has not been reported. * * @param \Closure|string $exception * @return void */ public function assertNotReported(Closure|string $exception) { try { $this->assertReported($exception); } catch (ExpectationFailedException $e) { return; } throw new ExpectationFailedException(sprintf( 'The expected [%s] exception was not reported.', is_string($exception) ? $exception : $this->firstClosureParameterType($exception) )); } /** * Assert nothing has been reported. * * @return void */ public function assertNothingReported() { Assert::assertEmpty( $this->reported, sprintf( 'The following exceptions were reported: %s.', implode(', ', array_map('get_class', $this->reported)), ), ); } /** * Report or log an exception. * * @param \Throwable $e * @return void */ public function report($e) { if (! $this->isFakedException($e)) { $this->handler->report($e); return; } if (! $this->shouldReport($e)) { return; } $this->reported[] = $e; if ($this->throwOnReport) { throw $e; } } /** * Determine if the given exception is faked. * * @param \Throwable $e * @return bool */ protected function isFakedException(Throwable $e) { return count($this->exceptions) === 0 || in_array(get_class($e), $this->exceptions, true); } /** * Determine if the exception should be reported. * * @param \Throwable $e * @return bool */ public function shouldReport($e) { return $this->runningWithoutExceptionHandling() || $this->handler->shouldReport($e); } /** * Determine if the handler is running without exception handling. * * @return bool */ protected function runningWithoutExceptionHandling() { return $this->handler instanceof WithoutExceptionHandlingHandler; } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Throwable $e * @return \Symfony\Component\HttpFoundation\Response */ public function render($request, $e) { return $this->handler->render($request, $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) { $this->handler->renderForConsole($output, $e); } /** * Throw exceptions when they are reported. * * @return $this */ public function throwOnReport() { $this->throwOnReport = true; return $this; } /** * Throw the first reported exception. * * @return $this * * @throws \Throwable */ public function throwFirstReported() { foreach ($this->reported as $e) { throw $e; } return $this; } /** * Set the "original" handler that should be used by the fake. * * @param \Illuminate\Contracts\Debug\ExceptionHandler $handler * @return $this */ public function setHandler(ExceptionHandler $handler) { $this->handler = $handler; return $this; } /** * Handle dynamic method calls to the mailer. * * @param string $method * @param array<string, mixed> $parameters * @return mixed */ public function __call(string $method, array $parameters) { return $this->forwardCallTo($this->handler, $method, $parameters); } } framework/src/Illuminate/Support/Testing/Fakes/Fake.php 0000644 00000000116 15060132304 0017124 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; interface Fake { // } framework/src/Illuminate/Support/Testing/Fakes/ChainedBatchTruthTest.php 0000644 00000001276 15060132304 0022452 0 ustar 00 <?php namespace Illuminate\Support\Testing\Fakes; use Closure; class ChainedBatchTruthTest { /** * The underlying truth test. * * @var \Closure */ protected $callback; /** * Create a new truth test instance. * * @param \Closure $callback * @return void */ public function __construct(Closure $callback) { $this->callback = $callback; } /** * Invoke the truth test with the given pending batch. * * @param \Illuminate\Bus\PendingBatch $pendingBatch * @return bool */ public function __invoke($pendingBatch) { return call_user_func($this->callback, $pendingBatch); } } framework/src/Illuminate/Support/Testing/Fakes/QueueFake.php 0000644 00000035512 15060132304 0020141 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 the total count of jobs that were pushed. * * @param int $expectedCount * @return void */ public function assertCount($expectedCount) { $actualCount = collect($this->jobs)->flatten(1)->count(); PHPUnit::assertSame( $expectedCount, $actualCount, "Expected {$expectedCount} jobs to be pushed, but found {$actualCount} instead." ); } /** * 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 )); } } framework/src/Illuminate/Support/Testing/Fakes/PendingBatchFake.php 0000644 00000002041 15060132304 0021372 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); } } framework/src/Illuminate/Support/Testing/Fakes/NotificationFake.php 0000644 00000026560 15060132304 0021506 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\Queue\ShouldQueue; 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; /** * Indicates if notifications should be serialized and restored when pushed to the queue. * * @var bool */ protected $serializeAndRestore = false; /** * 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' => $this->serializeAndRestore && $notification instanceof ShouldQueue ? $this->serializeAndRestoreNotification($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; } /** * Specify if notification 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 notification to simulate the queueing process. * * @param mixed $notification * @return mixed */ protected function serializeAndRestoreNotification($notification) { return unserialize(serialize($notification)); } /** * Get the notifications that have been sent. * * @return array */ public function sentNotifications() { return $this->notifications; } } framework/src/Illuminate/Support/Testing/Fakes/EventFake.php 0000644 00000025777 15060132304 0020152 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); } /** * Get the events that have been dispatched. * * @return array */ public function dispatchedEvents() { return $this->events; } /** * 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); } } framework/src/Illuminate/Support/Lottery.php 0000644 00000013237 15060132304 0015262 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; } } framework/src/Illuminate/Support/ProcessUtils.php 0000644 00000004002 15060132304 0016245 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]; } } framework/src/Illuminate/Support/ServiceProvider.php 0000755 00000031362 15060132304 0016735 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 = []; /** * The migration paths available for publishing. * * @var array */ protected static $publishableMigrationPaths = []; /** * 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 migration paths to be published by the publish command. * * @param array $paths * @param mixed $groups * @return void */ protected function publishesMigrations(array $paths, $groups = null) { $this->publishes($paths, $groups); if ($this->app->config->get('database.migrations.update_date_on_publish', false)) { static::$publishableMigrationPaths = array_unique(array_merge(static::$publishableMigrationPaths, array_keys($paths))); } } /** * 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 migration paths available for publishing. * * @return array */ public static function publishableMigrationPaths() { return static::$publishableMigrationPaths; } /** * 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; } /** * Add the given provider to the application's provider bootstrap file. * * @param string $provider * @param string $path * @return bool */ public static function addProviderToBootstrapFile(string $provider, ?string $path = null) { $path ??= app()->getBootstrapProvidersPath(); if (! file_exists($path)) { return false; } if (function_exists('opcache_invalidate')) { opcache_invalidate($path, true); } $providers = collect(require $path) ->merge([$provider]) ->unique() ->sort() ->values() ->map(fn ($p) => ' '.$p.'::class,') ->implode(PHP_EOL); $content = '<?php return [ '.$providers.' ];'; file_put_contents($path, $content.PHP_EOL); return true; } } framework/src/Illuminate/Support/Optional.php 0000644 00000005214 15060132304 0015401 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); } } } framework/src/Illuminate/Support/Reflector.php 0000644 00000010777 15060132304 0015553 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; } } framework/src/Illuminate/Support/Manager.php 0000755 00000010662 15060132304 0015174 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); } } framework/src/Illuminate/Support/Js.php 0000644 00000006737 15060132304 0014203 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; use Stringable; class Js implements Htmlable, Stringable { /** * 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(); } } framework/src/Illuminate/Support/Exceptions/MathException.php 0000644 00000000200 15060132304 0020473 0 ustar 00 <?php namespace Illuminate\Support\Exceptions; use RuntimeException; class MathException extends RuntimeException { // } framework/src/Illuminate/Support/MessageBag.php 0000755 00000024172 15060132304 0015621 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; use Stringable; class MessageBag implements Jsonable, JsonSerializable, MessageBagContract, MessageProvider, Stringable { /** * 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(); } } framework/src/Illuminate/Support/HtmlString.php 0000644 00000002075 15060132304 0015711 0 ustar 00 <?php namespace Illuminate\Support; use Illuminate\Contracts\Support\Htmlable; use Stringable; class HtmlString implements Htmlable, Stringable { /** * 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(); } } framework/src/Illuminate/Config/LICENSE.md 0000644 00000002063 15060132304 0014237 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. framework/src/Illuminate/Config/composer.json 0000755 00000001466 15060132304 0015366 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.2", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Config\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Config/Repository.php 0000644 00000014461 15060132304 0015530 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; use InvalidArgumentException; 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; } /** * Get the specified string configuration value. * * @param string $key * @param (\Closure():(string|null))|string|null $default * @return string */ public function string(string $key, $default = null): string { $value = $this->get($key, $default); if (! is_string($value)) { throw new InvalidArgumentException( sprintf('Configuration value for key [%s] must be a string, %s given.', $key, gettype($value)) ); } return $value; } /** * Get the specified integer configuration value. * * @param string $key * @param (\Closure():(int|null))|int|null $default * @return int */ public function integer(string $key, $default = null): int { $value = $this->get($key, $default); if (! is_int($value)) { throw new InvalidArgumentException( sprintf('Configuration value for key [%s] must be an integer, %s given.', $key, gettype($value)) ); } return $value; } /** * Get the specified float configuration value. * * @param string $key * @param (\Closure():(float|null))|float|null $default * @return float */ public function float(string $key, $default = null): float { $value = $this->get($key, $default); if (! is_float($value)) { throw new InvalidArgumentException( sprintf('Configuration value for key [%s] must be a float, %s given.', $key, gettype($value)) ); } return $value; } /** * Get the specified boolean configuration value. * * @param string $key * @param (\Closure():(bool|null))|bool|null $default * @return bool */ public function boolean(string $key, $default = null): bool { $value = $this->get($key, $default); if (! is_bool($value)) { throw new InvalidArgumentException( sprintf('Configuration value for key [%s] must be a boolean, %s given.', $key, gettype($value)) ); } return $value; } /** * Get the specified array configuration value. * * @param string $key * @param (\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null $default * @return array<array-key, mixed> */ public function array(string $key, $default = null): array { $value = $this->get($key, $default); if (! is_array($value)) { throw new InvalidArgumentException( sprintf('Configuration value for key [%s] must be an array, %s given.', $key, gettype($value)) ); } return $value; } /** * 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); } } framework/src/Illuminate/Broadcasting/UniqueBroadcastEvent.php 0000644 00000002776 15060132304 0020645 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); } } framework/src/Illuminate/Broadcasting/BroadcastController.php 0000644 00000002225 15060132304 0020505 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; } } framework/src/Illuminate/Broadcasting/BroadcastEvent.php 0000644 00000007453 15060132304 0017453 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; } } framework/src/Illuminate/Broadcasting/PendingBroadcast.php 0000644 00000002736 15060132304 0017755 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); } } framework/src/Illuminate/Broadcasting/EncryptedPrivateChannel.php 0000644 00000000467 15060132304 0021326 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); } } framework/src/Illuminate/Broadcasting/LICENSE.md 0000644 00000002063 15060132304 0015432 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. framework/src/Illuminate/Broadcasting/Channel.php 0000644 00000001327 15060132304 0016111 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Contracts\Broadcasting\HasBroadcastChannel; use Stringable; class Channel implements Stringable { /** * 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; } } framework/src/Illuminate/Broadcasting/BroadcastManager.php 0000644 00000032330 15060132304 0017734 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 sending an anonymous broadcast to the given channels. */ public function on(Channel|string|array $channels): AnonymousEvent { return new AnonymousEvent($channels); } /** * Begin sending an anonymous broadcast to the given private channels. */ public function private(string $channel): AnonymousEvent { return $this->on(new PrivateChannel($channel)); } /** * Begin sending an anonymous broadcast to the given presence channels. */ public function presence(string $channel): AnonymousEvent { return $this->on(new PresenceChannel($channel)); } /** * 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 createReverbDriver(array $config) { return $this->createPusherDriver($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) { $guzzleClient = new GuzzleClient( array_merge( [ 'connect_timeout' => 10, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, 'timeout' => 30, ], $config['client_options'] ?? [], ), ); $pusher = new Pusher( $config['key'], $config['secret'], $config['app_id'], $config['options'] ?? [], $guzzleClient, ); 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); } } framework/src/Illuminate/Broadcasting/PresenceChannel.php 0000644 00000000446 15060132304 0017577 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); } } framework/src/Illuminate/Broadcasting/BroadcastServiceProvider.php 0000644 00000002160 15060132304 0021473 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, ]; } } framework/src/Illuminate/Broadcasting/InteractsWithSockets.php 0000644 00000001235 15060132304 0020663 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; } } framework/src/Illuminate/Broadcasting/composer.json 0000644 00000002430 15060132304 0016546 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.2", "psr/log": "^1.0|^2.0|^3.0", "illuminate/bus": "^11.0", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/queue": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Broadcasting\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.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" } framework/src/Illuminate/Broadcasting/PrivateChannel.php 0000644 00000000762 15060132304 0017446 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); } } framework/src/Illuminate/Broadcasting/BroadcastException.php 0000644 00000000177 15060132304 0020324 0 ustar 00 <?php namespace Illuminate\Broadcasting; use RuntimeException; class BroadcastException extends RuntimeException { // } framework/src/Illuminate/Broadcasting/InteractsWithBroadcasting.php 0000644 00000001524 15060132304 0021651 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; } } framework/src/Illuminate/Broadcasting/AnonymousEvent.php 0000644 00000006437 15060132304 0017542 0 ustar 00 <?php namespace Illuminate\Broadcasting; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Support\Arr; class AnonymousEvent implements ShouldBroadcast { use Dispatchable, InteractsWithBroadcasting, InteractsWithSockets; /** * The connection the event should be broadcast on. */ protected ?string $connection = null; /** * The name the event should be broadcast as. */ protected ?string $name = null; /** * The payload the event should be broadcast with. */ protected array $payload = []; /** * Should the broadcast include the current user. */ protected bool $includeCurrentUser = true; /** * Indicates if the event should be broadcast synchronously. */ protected bool $shouldBroadcastNow = false; /** * Create a new anonymous broadcastable event instance. * * @return void */ public function __construct(protected Channel|array|string $channels) { $this->channels = Arr::wrap($channels); } /** * Set the connection the event should be broadcast on. */ public function via(string $connection): static { $this->connection = $connection; return $this; } /** * Set the name the event should be broadcast as. */ public function as(string $name): static { $this->name = $name; return $this; } /** * Set the payload the event should be broadcast with. */ public function with(Arrayable|array $payload): static { $this->payload = $payload instanceof Arrayable ? $payload->toArray() : collect($payload)->map( fn ($p) => $p instanceof Arrayable ? $p->toArray() : $p )->all(); return $this; } /** * Broadcast the event to everyone except the current user. */ public function toOthers(): static { $this->includeCurrentUser = false; return $this; } /** * Broadcast the event. */ public function sendNow(): void { $this->shouldBroadcastNow = true; $this->send(); } /** * Broadcast the event. */ public function send(): void { $broadcast = broadcast($this)->via($this->connection); if (! $this->includeCurrentUser) { $broadcast->toOthers(); } } /** * Get the name the event should broadcast as. */ public function broadcastAs(): string { return $this->name ?: class_basename($this); } /** * Get the payload the event should broadcast with. * * @return array<string, mixed> */ public function broadcastWith(): array { return $this->payload; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]|string[]|string */ public function broadcastOn(): Channel|array { return $this->channels; } /** * Determine if the event should be broadcast synchronously. */ public function shouldBroadcastNow(): bool { return $this->shouldBroadcastNow; } } framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php 0000644 00000010507 15060132304 0022375 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)); } } framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php 0000644 00000025470 15060132304 0021413 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); } } framework/src/Illuminate/Broadcasting/Broadcasters/UsePusherChannelConventions.php 0000644 00000001465 15060132304 0024622 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; } } framework/src/Illuminate/Broadcasting/Broadcasters/LogBroadcaster.php 0000644 00000002074 15060132304 0022050 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); } } framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php 0000644 00000013044 15060132304 0022574 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; } } framework/src/Illuminate/Broadcasting/Broadcasters/NullBroadcaster.php 0000644 00000000716 15060132304 0022242 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 = []) { // } } framework/src/Illuminate/Broadcasting/Broadcasters/AblyBroadcaster.php 0000644 00000015035 15060132304 0022217 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; } /** * Set the underlying Ably SDK instance. * * @param \Ably\AblyRest $ably * @return void */ public function setAbly($ably) { $this->ably = $ably; } } framework/src/Illuminate/Bus/PendingBatch.php 0000644 00000022413 15060132304 0015217 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 Illuminate\Support\Traits\Conditionable; use Laravel\SerializableClosure\SerializableClosure; use Throwable; class PendingBatch { use Conditionable; /** * 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 when the batch is stored. * * @param callable $callback * @return $this */ public function before($callback) { $this->options['before'][] = $callback instanceof Closure ? new SerializableClosure($callback) : $callback; return $this; } /** * Get the "before" callbacks that have been registered with the pending batch. * * @return array */ public function beforeCallbacks() { return $this->options['before'] ?? []; } /** * Add a callback to be executed after a job in the batch have executed successfully. * * @param callable $callback * @return $this */ public function progress($callback) { $this->options['progress'][] = $callback instanceof Closure ? new SerializableClosure($callback) : $callback; return $this; } /** * Get the "progress" callbacks that have been registered with the pending batch. * * @return array */ public function progressCallbacks() { return $this->options['progress'] ?? []; } /** * 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 = $this->store($repository); $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 = $this->store($repository); 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) ); } /** * Dispatch the batch if the given truth test passes. * * @param bool|\Closure $boolean * @return \Illuminate\Bus\Batch|null */ public function dispatchIf($boolean) { return value($boolean) ? $this->dispatch() : null; } /** * Dispatch the batch unless the given truth test passes. * * @param bool|\Closure $boolean * @return \Illuminate\Bus\Batch|null */ public function dispatchUnless($boolean) { return ! value($boolean) ? $this->dispatch() : null; } /** * Store the batch using the given repository. * * @param \Illuminate\Bus\BatchRepository $repository * @return \Illuminate\Bus\Batch */ protected function store($repository) { $batch = $repository->store($this); collect($this->beforeCallbacks())->each(function ($handler) use ($batch) { try { return $handler($batch); } catch (Throwable $e) { if (function_exists('report')) { report($e); } } }); return $batch; } } framework/src/Illuminate/Bus/Events/BatchDispatched.php 0000644 00000000633 15060132304 0017147 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; } } framework/src/Illuminate/Bus/LICENSE.md 0000644 00000002063 15060132304 0013563 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. framework/src/Illuminate/Bus/Batch.php 0000644 00000031246 15060132304 0013716 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 ($this->hasProgressCallbacks()) { $batch = $this->fresh(); collect($this->options['progress'])->each(function ($handler) use ($batch) { $this->invokeHandlerCallback($handler, $batch); }); } 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 "progress" callbacks. * * @return bool */ public function hasProgressCallbacks() { return isset($this->options['progress']) && ! empty($this->options['progress']); } /** * 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 ($this->hasProgressCallbacks() && $this->allowsFailures()) { $batch = $this->fresh(); collect($this->options['progress'])->each(function ($handler) use ($batch, $e) { $this->invokeHandlerCallback($handler, $batch, $e); }); } 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; } } framework/src/Illuminate/Bus/Batchable.php 0000644 00000005417 15060132304 0014543 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|null $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]; } } framework/src/Illuminate/Bus/ChainedBatch.php 0000644 00000007334 15060132304 0015173 0 ustar 00 <?php namespace Illuminate\Bus; use Illuminate\Container\Container; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Collection; use Throwable; class ChainedBatch implements ShouldQueue { use Batchable, Dispatchable, InteractsWithQueue, Queueable; /** * The collection of batched jobs. * * @var \Illuminate\Support\Collection */ public Collection $jobs; /** * The name of the batch. * * @var string */ public string $name; /** * The batch options. * * @var array */ public array $options; /** * Create a new chained batch instance. * * @param \Illuminate\Bus\PendingBatch $batch * @return void */ public function __construct(PendingBatch $batch) { $this->jobs = static::prepareNestedBatches($batch->jobs); $this->name = $batch->name; $this->options = $batch->options; } /** * Prepare any nested batches within the given collection of jobs. * * @param \Illuminate\Support\Collection $jobs * @return \Illuminate\Support\Collection */ public static function prepareNestedBatches(Collection $jobs): Collection { return $jobs->map(fn ($job) => match (true) { is_array($job) => static::prepareNestedBatches(collect($job))->all(), $job instanceof Collection => static::prepareNestedBatches($job), $job instanceof PendingBatch => new ChainedBatch($job), default => $job, }); } /** * Handle the job. * * @return void */ public function handle() { $this->attachRemainderOfChainToEndOfBatch( $this->toPendingBatch() )->dispatch(); } /** * Convert the chained batch instance into a pending batch. * * @return \Illuminate\Bus\PendingBatch */ public function toPendingBatch() { $batch = Container::getInstance()->make(Dispatcher::class)->batch($this->jobs); $batch->name = $this->name; $batch->options = $this->options; if ($this->queue) { $batch->onQueue($this->queue); } if ($this->connection) { $batch->onConnection($this->connection); } foreach ($this->chainCatchCallbacks ?? [] as $callback) { $batch->catch(function (Batch $batch, ?Throwable $exception) use ($callback) { if (! $batch->allowsFailures()) { $callback($exception); } }); } return $batch; } /** * Move the remainder of the chain to a "finally" batch callback. * * @param \Illuminate\Bus\PendingBatch $batch * @return \Illuminate\Bus\PendingBatch */ protected function attachRemainderOfChainToEndOfBatch(PendingBatch $batch) { if (! empty($this->chained)) { $next = unserialize(array_shift($this->chained)); $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; $batch->finally(function (Batch $batch) use ($next) { if (! $batch->cancelled()) { Container::getInstance()->make(Dispatcher::class)->dispatch($next); } }); $this->chained = []; } return $batch; } } framework/src/Illuminate/Bus/DatabaseBatchRepository.php 0000644 00000025741 15060132304 0017446 0 ustar 00 <?php namespace Illuminate\Bus; use Carbon\CarbonImmutable; use Closure; use DateTimeInterface; use Illuminate\Database\Connection; use Illuminate\Database\PostgresConnection; use Illuminate\Database\Query\Expression; use Illuminate\Support\Str; use Throwable; 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()); } /** * Rollback the last database transaction for the connection. * * @return void */ public function rollBack() { $this->connection->rollBack(); } /** * 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 (Throwable) { 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, date_default_timezone_get()), $batch->cancelled_at ? CarbonImmutable::createFromTimestamp($batch->cancelled_at, date_default_timezone_get()) : $batch->cancelled_at, $batch->finished_at ? CarbonImmutable::createFromTimestamp($batch->finished_at, date_default_timezone_get()) : $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; } } framework/src/Illuminate/Bus/Dispatcher.php 0000644 00000017312 15060132304 0014761 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); $jobs = ChainedBatch::prepareNestedBatches($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; } } framework/src/Illuminate/Bus/PrunableBatchRepository.php 0000644 00000000476 15060132304 0017510 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); } framework/src/Illuminate/Bus/composer.json 0000644 00000001746 15060132304 0014710 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.2", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/pipeline": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Bus\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "illuminate/queue": "Required to use closures when chaining jobs (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Bus/Queueable.php 0000644 00000016245 15060132304 0014607 0 ustar 00 <?php namespace Illuminate\Bus; use Closure; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Support\Arr; use PHPUnit\Framework\Assert as PHPUnit; 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 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 = []; /** * 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; /** * 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; } /** * Set the delay for the job to zero seconds. * * @return $this */ public function withoutDelay() { $this->delay = 0; 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); }); } /** * Assert that the job has the given chain of jobs attached to it. * * @param array $expectedChain * @return void */ public function assertHasChain($expectedChain) { PHPUnit::assertTrue( collect($expectedChain)->isNotEmpty(), 'The expected chain can not be empty.' ); if (collect($expectedChain)->contains(fn ($job) => is_object($job))) { $expectedChain = collect($expectedChain)->map(fn ($job) => serialize($job))->all(); } else { $chain = collect($this->chained)->map(fn ($job) => get_class(unserialize($job)))->all(); } PHPUnit::assertTrue( $expectedChain === ($chain ?? $this->chained), 'The job does not have the expected chain.' ); } /** * Assert that the job has no remaining chained jobs. * * @return void */ public function assertDoesntHaveChain() { PHPUnit::assertEmpty($this->chained, 'The job has chained jobs.'); } } framework/src/Illuminate/Bus/BusServiceProvider.php 0000644 00000006404 15060132304 0016460 0 ustar 00 <?php namespace Illuminate\Bus; use Aws\DynamoDb\DynamoDbClient; 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\Arr; 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, function ($app) { $driver = $app->config->get('queue.batching.driver', 'database'); return $driver === 'dynamodb' ? $app->make(DynamoBatchRepository::class) : $app->make(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') ); }); $this->app->singleton(DynamoBatchRepository::class, function ($app) { $config = $app->config->get('queue.batching'); $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 DynamoBatchRepository( $app->make(BatchFactory::class), new DynamoDbClient($dynamoConfig), $app->config->get('app.name'), $app->config->get('queue.batching.table', 'job_batches'), ttl: $app->config->get('queue.batching.ttl', null), ttlAttribute: $app->config->get('queue.batching.ttl_attribute', 'ttl'), ); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [ Dispatcher::class, DispatcherContract::class, QueueingDispatcherContract::class, BatchRepository::class, DatabaseBatchRepository::class, ]; } } framework/src/Illuminate/Bus/DynamoBatchRepository.php 0000644 00000035677 15060132304 0017202 0 ustar 00 <?php namespace Illuminate\Bus; use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\Marshaler; use Carbon\CarbonImmutable; use Closure; use Illuminate\Support\Str; class DynamoBatchRepository implements BatchRepository { /** * The batch factory instance. * * @var \Illuminate\Bus\BatchFactory */ protected $factory; /** * The database connection instance. * * @var \Aws\DynamoDb\DynamoDbClient */ protected $dynamoDbClient; /** * The application name. * * @var string */ protected $applicationName; /** * The table to use to store batch information. * * @var string */ protected $table; /** * The time-to-live value for batch records. * * @var int */ protected $ttl; /** * The name of the time-to-live attribute for batch records. * * @var string */ protected $ttlAttribute; /** * The DynamoDB marshaler instance. * * @var \Aws\DynamoDb\Marshaler */ protected $marshaler; /** * Create a new batch repository instance. */ public function __construct( BatchFactory $factory, DynamoDbClient $dynamoDbClient, string $applicationName, string $table, ?int $ttl, ?string $ttlAttribute ) { $this->factory = $factory; $this->dynamoDbClient = $dynamoDbClient; $this->applicationName = $applicationName; $this->table = $table; $this->ttl = $ttl; $this->ttlAttribute = $ttlAttribute; $this->marshaler = new Marshaler; } /** * Retrieve a list of batches. * * @param int $limit * @param mixed $before * @return \Illuminate\Bus\Batch[] */ public function get($limit = 50, $before = null) { $condition = 'application = :application'; if ($before) { $condition = 'application = :application AND id < :id'; } $result = $this->dynamoDbClient->query([ 'TableName' => $this->table, 'KeyConditionExpression' => $condition, 'ExpressionAttributeValues' => array_filter([ ':application' => ['S' => $this->applicationName], ':id' => array_filter(['S' => $before]), ]), 'Limit' => $limit, 'ScanIndexForward' => false, ]); return array_map( fn ($b) => $this->toBatch($this->marshaler->unmarshalItem($b, mapAsObject: true)), $result['Items'] ); } /** * Retrieve information about an existing batch. * * @param string $batchId * @return \Illuminate\Bus\Batch|null */ public function find(string $batchId) { if ($batchId === '') { return null; } $b = $this->dynamoDbClient->getItem([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], ]); if (! isset($b['Item'])) { // If we didn't find it via a standard read, attempt consistent read... $b = $this->dynamoDbClient->getItem([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], 'ConsistentRead' => true, ]); if (! isset($b['Item'])) { return null; } } $batch = $this->marshaler->unmarshalItem($b['Item'], mapAsObject: true); 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(); $batch = [ '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, ]; if (! is_null($this->ttl)) { $batch[$this->ttlAttribute] = time() + $this->ttl; } $this->dynamoDbClient->putItem([ 'TableName' => $this->table, 'Item' => $this->marshaler->marshalItem( array_merge(['application' => $this->applicationName], $batch) ), ]); 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) { $update = 'SET total_jobs = total_jobs + :val, pending_jobs = pending_jobs + :val'; if ($this->ttl) { $update = "SET total_jobs = total_jobs + :val, pending_jobs = pending_jobs + :val, #{$this->ttlAttribute} = :ttl"; } $this->dynamoDbClient->updateItem(array_filter([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], 'UpdateExpression' => $update, 'ExpressionAttributeValues' => array_filter([ ':val' => ['N' => "$amount"], ':ttl' => array_filter(['N' => $this->getExpiryTime()]), ]), 'ExpressionAttributeNames' => $this->ttlExpressionAttributeName(), 'ReturnValues' => 'ALL_NEW', ])); } /** * 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) { $update = 'SET pending_jobs = pending_jobs - :inc'; if ($this->ttl !== null) { $update = "SET pending_jobs = pending_jobs - :inc, #{$this->ttlAttribute} = :ttl"; } $batch = $this->dynamoDbClient->updateItem(array_filter([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], 'UpdateExpression' => $update, 'ExpressionAttributeValues' => array_filter([ ':inc' => ['N' => '1'], ':ttl' => array_filter(['N' => $this->getExpiryTime()]), ]), 'ExpressionAttributeNames' => $this->ttlExpressionAttributeName(), 'ReturnValues' => 'ALL_NEW', ])); $values = $this->marshaler->unmarshalItem($batch['Attributes']); 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) { $update = 'SET failed_jobs = failed_jobs + :inc, failed_job_ids = list_append(failed_job_ids, :jobId)'; if ($this->ttl !== null) { $update = "SET failed_jobs = failed_jobs + :inc, failed_job_ids = list_append(failed_job_ids, :jobId), #{$this->ttlAttribute} = :ttl"; } $batch = $this->dynamoDbClient->updateItem(array_filter([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], 'UpdateExpression' => $update, 'ExpressionAttributeValues' => array_filter([ ':jobId' => $this->marshaler->marshalValue([$jobId]), ':inc' => ['N' => '1'], ':ttl' => array_filter(['N' => $this->getExpiryTime()]), ]), 'ExpressionAttributeNames' => $this->ttlExpressionAttributeName(), 'ReturnValues' => 'ALL_NEW', ])); $values = $this->marshaler->unmarshalItem($batch['Attributes']); return new UpdatedBatchJobCounts( $values['pending_jobs'], $values['failed_jobs'] ); } /** * Mark the batch that has the given ID as finished. * * @param string $batchId * @return void */ public function markAsFinished(string $batchId) { $update = 'SET finished_at = :timestamp'; if ($this->ttl !== null) { $update = "SET finished_at = :timestamp, #{$this->ttlAttribute} = :ttl"; } $this->dynamoDbClient->updateItem(array_filter([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], 'UpdateExpression' => $update, 'ExpressionAttributeValues' => array_filter([ ':timestamp' => ['N' => (string) time()], ':ttl' => array_filter(['N' => $this->getExpiryTime()]), ]), 'ExpressionAttributeNames' => $this->ttlExpressionAttributeName(), ])); } /** * Cancel the batch that has the given ID. * * @param string $batchId * @return void */ public function cancel(string $batchId) { $update = 'SET cancelled_at = :timestamp, finished_at = :timestamp'; if ($this->ttl !== null) { $update = "SET cancelled_at = :timestamp, finished_at = :timestamp, #{$this->ttlAttribute} = :ttl"; } $this->dynamoDbClient->updateItem(array_filter([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], 'UpdateExpression' => $update, 'ExpressionAttributeValues' => array_filter([ ':timestamp' => ['N' => (string) time()], ':ttl' => array_filter(['N' => $this->getExpiryTime()]), ]), 'ExpressionAttributeNames' => $this->ttlExpressionAttributeName(), ])); } /** * Delete the batch that has the given ID. * * @param string $batchId * @return void */ public function delete(string $batchId) { $this->dynamoDbClient->deleteItem([ 'TableName' => $this->table, 'Key' => [ 'application' => ['S' => $this->applicationName], 'id' => ['S' => $batchId], ], ]); } /** * Execute the given Closure within a storage specific transaction. * * @param \Closure $callback * @return mixed */ public function transaction(Closure $callback) { return $callback(); } /** * Rollback the last database transaction for the connection. * * @return void */ public function rollBack() { } /** * 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, $batch->failed_job_ids, $this->unserialize($batch->options) ?? [], CarbonImmutable::createFromTimestamp($batch->created_at, date_default_timezone_get()), $batch->cancelled_at ? CarbonImmutable::createFromTimestamp($batch->cancelled_at, date_default_timezone_get()) : $batch->cancelled_at, $batch->finished_at ? CarbonImmutable::createFromTimestamp($batch->finished_at, date_default_timezone_get()) : $batch->finished_at ); } /** * Create the underlying DynamoDB table. * * @return void */ public function createAwsDynamoTable(): void { $definition = [ 'TableName' => $this->table, 'AttributeDefinitions' => [ [ 'AttributeName' => 'application', 'AttributeType' => 'S', ], [ 'AttributeName' => 'id', 'AttributeType' => 'S', ], ], 'KeySchema' => [ [ 'AttributeName' => 'application', 'KeyType' => 'HASH', ], [ 'AttributeName' => 'id', 'KeyType' => 'RANGE', ], ], 'BillingMode' => 'PAY_PER_REQUEST', ]; $this->dynamoDbClient->createTable($definition); if (! is_null($this->ttl)) { $this->dynamoDbClient->updateTimeToLive([ 'TableName' => $this->table, 'TimeToLiveSpecification' => [ 'AttributeName' => $this->ttlAttribute, 'Enabled' => true, ], ]); } } /** * Delete the underlying DynamoDB table. */ public function deleteAwsDynamoTable(): void { $this->dynamoDbClient->deleteTable([ 'TableName' => $this->table, ]); } /** * Get the expiry time based on the configured time-to-live. * * @return string|null */ protected function getExpiryTime(): ?string { return is_null($this->ttl) ? null : (string) (time() + $this->ttl); } /** * Get the expression attribute name for the time-to-live attribute. * * @return array */ protected function ttlExpressionAttributeName(): array { return is_null($this->ttl) ? [] : ["#{$this->ttlAttribute}" => $this->ttlAttribute]; } /** * Serialize the given value. * * @param mixed $value * @return string */ protected function serialize($value) { return serialize($value); } /** * Unserialize the given value. * * @param string $serialized * @return mixed */ protected function unserialize($serialized) { return unserialize($serialized); } /** * Get the underlying DynamoDB client instance. * * @return \Aws\DynamoDb\DynamoDbClient */ public function getDynamoClient(): DynamoDbClient { return $this->dynamoDbClient; } /** * The name of the table that contains the batch records. * * @return string */ public function getTable(): string { return $this->table; } } framework/src/Illuminate/Bus/UpdatedBatchJobCounts.php 0000644 00000001530 15060132304 0017045 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; } } framework/src/Illuminate/Bus/BatchFactory.php 0000644 00000003324 15060132304 0015242 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); } } framework/src/Illuminate/Bus/UniqueLock.php 0000644 00000003277 15060132304 0014757 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; } } framework/src/Illuminate/Bus/BatchRepository.php 0000644 00000004417 15060132304 0016016 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); /** * Rollback the last database transaction for the connection. * * @return void */ public function rollBack(); } framework/src/Illuminate/Pagination/AbstractCursorPaginator.php 0000644 00000035676 15060132304 0021056 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 Stringable; use Traversable; /** * @mixin \Illuminate\Support\Collection */ abstract class AbstractCursorPaginator implements Htmlable, Stringable { 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) ->filter() ->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(); } } framework/src/Illuminate/Pagination/UrlWindow.php 0000644 00000013155 15060132304 0016166 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(); } } framework/src/Illuminate/Pagination/Paginator.php 0000644 00000010426 15060132304 0016156 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); } } framework/src/Illuminate/Pagination/LICENSE.md 0000644 00000002063 15060132304 0015123 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. framework/src/Illuminate/Pagination/composer.json 0000755 00000001604 15060132304 0016244 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.2", "ext-filter": "*", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Pagination\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Pagination/LengthAwarePaginator.php 0000644 00000014047 15060132304 0020303 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); } } framework/src/Illuminate/Pagination/AbstractPaginator.php 0000644 00000041104 15060132304 0017637 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 Stringable; use Traversable; /** * @mixin \Illuminate\Support\Collection */ abstract class AbstractPaginator implements Htmlable, Stringable { 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(); } } framework/src/Illuminate/Pagination/resources/views/tailwind.blade.php 0000644 00000021017 15060132304 0022260 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 dark:text-gray-600 dark:bg-gray-800 dark:border-gray-600"> {!! __('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 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:focus:border-blue-700 dark:active:bg-gray-700 dark:active:text-gray-300"> {!! __('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 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:focus:border-blue-700 dark:active:bg-gray-700 dark:active:text-gray-300"> {!! __('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 dark:text-gray-600 dark:bg-gray-800 dark:border-gray-600"> {!! __('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 dark:text-gray-400"> {!! __('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 rtl:flex-row-reverse 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 dark:bg-gray-800 dark:border-gray-600" 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 dark:bg-gray-800 dark:border-gray-600 dark:active:bg-gray-700 dark:focus:border-blue-800" 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 dark:bg-gray-800 dark:border-gray-600">{{ $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 dark:bg-gray-800 dark:border-gray-600">{{ $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 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-400 dark:hover:text-gray-300 dark:active:bg-gray-700 dark:focus:border-blue-800" 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 dark:bg-gray-800 dark:border-gray-600 dark:active:bg-gray-700 dark:focus:border-blue-800" 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 dark:bg-gray-800 dark:border-gray-600" 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 framework/src/Illuminate/Pagination/resources/views/bootstrap-4.blade.php 0000644 00000004044 15060132304 0022624 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 framework/src/Illuminate/Pagination/resources/views/simple-tailwind.blade.php 0000644 00000004041 15060132304 0023545 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 dark:text-gray-600 dark:bg-gray-800 dark:border-gray-600"> {!! __('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 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:focus:border-blue-700 dark:active:bg-gray-700 dark:active:text-gray-300"> {!! __('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 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:focus:border-blue-700 dark:active:bg-gray-700 dark:active:text-gray-300"> {!! __('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 dark:text-gray-600 dark:bg-gray-800 dark:border-gray-600"> {!! __('pagination.next') !!} </span> @endif </nav> @endif framework/src/Illuminate/Pagination/resources/views/bootstrap-5.blade.php 0000644 00000010300 15060132304 0022615 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 framework/src/Illuminate/Pagination/resources/views/simple-default.blade.php 0000644 00000001405 15060132304 0023357 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 framework/src/Illuminate/Pagination/resources/views/simple-bootstrap-4.blade.php 0000644 00000002035 15060132304 0024111 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 framework/src/Illuminate/Pagination/resources/views/default.blade.php 0000644 00000003510 15060132304 0022067 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 framework/src/Illuminate/Pagination/resources/views/semantic-ui.blade.php 0000644 00000003222 15060132304 0022661 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 framework/src/Illuminate/Pagination/resources/views/simple-bootstrap-5.blade.php 0000644 00000002224 15060132304 0024112 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 framework/src/Illuminate/Pagination/Cursor.php 0000644 00000006236 15060132304 0015513 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); } } framework/src/Illuminate/Pagination/CursorPaginator.php 0000644 00000010706 15060132304 0017355 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); } } framework/src/Illuminate/Pagination/PaginationState.php 0000644 00000002013 15060132304 0017315 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)); }); } } framework/src/Illuminate/Pagination/PaginationServiceProvider.php 0000755 00000001344 15060132304 0021361 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); } } framework/src/Illuminate/Events/functions.php 0000644 00000000552 15060132304 0015414 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); } } framework/src/Illuminate/Events/QueuedClosure.php 0000644 00000005220 15060132304 0016166 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); }; } } framework/src/Illuminate/Events/LICENSE.md 0000644 00000002063 15060132304 0014276 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. framework/src/Illuminate/Events/InvokeQueuedClosure.php 0000644 00000001477 15060132304 0017354 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); } } framework/src/Illuminate/Events/Dispatcher.php 0000755 00000053362 15060132304 0015504 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->failOnTimeout = $listener->failOnTimeout ?? false; $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; } } framework/src/Illuminate/Events/composer.json 0000755 00000002014 15060132304 0015413 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.2", "illuminate/bus": "^11.0", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Events\\": "" }, "files": [ "functions.php" ] }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Events/NullDispatcher.php 0000644 00000006055 15060132304 0016331 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); } } framework/src/Illuminate/Events/EventServiceProvider.php 0000755 00000001371 15060132304 0017524 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; }); }); } } framework/src/Illuminate/Events/CallQueuedListener.php 0000644 00000007452 15060132304 0017144 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 fail if the timeout is exceeded. * * @var bool */ public $failOnTimeout = false; /** * 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); } } framework/src/Illuminate/Http/Request.php 0000644 00000043642 15060132304 0014516 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 || strcasecmp($this->headers->get('Sec-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 values 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 */ #[\Override] 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 */ #[\Override] 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} */ #[\Override] public function hasSession(bool $skipIfUninitialized = false): bool { return $this->session instanceof SymfonySessionDecorator; } /** * {@inheritdoc} */ #[\Override] public function getSession(): SessionInterface { return $this->hasSession() ? $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->store; } /** * Set the session instance on the request. * * @param \Illuminate\Contracts\Session\Session $session * @return void */ public function setLaravelSession($session) { $this->session = new SymfonySessionDecorator($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)); } } framework/src/Illuminate/Http/Middleware/SetCacheHeaders.php 0000644 00000005073 15060132304 0020112 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Closure; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\StreamedResponse; 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 && ! $response instanceof StreamedResponse)) { return $response; } if (is_string($options)) { $options = $this->parseOptions($options); } if (! $response->isSuccessful()) { return $response; } 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'], date_default_timezone_get()); } 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(); } } framework/src/Illuminate/Http/Middleware/CheckResponseForModifications.php 0000644 00000001033 15060132304 0023043 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; } } framework/src/Illuminate/Http/Middleware/HandleCors.php 0000644 00000005303 15060132304 0017155 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); }); } } framework/src/Illuminate/Http/Middleware/FrameGuard.php 0000644 00000000763 15060132304 0017155 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; } } framework/src/Illuminate/Http/Middleware/ValidatePostSize.php 0000644 00000002274 15060132304 0020371 0 ustar 00 <?php namespace Illuminate\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, }; } } framework/src/Illuminate/Http/Middleware/TrustHosts.php 0000644 00000006056 15060132304 0017303 0 ustar 00 <?php namespace Illuminate\Http\Middleware; use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\Request; class TrustHosts { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The trusted hosts that have been configured to always be trusted. * * @var array<int, string>|(callable(): array<int, string>)|null */ protected static $alwaysTrust; /** * Indicates whether subdomains of the application URL should be trusted. * * @var bool|null */ protected static $subdomains; /** * 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 */ public function hosts() { if (is_null(static::$alwaysTrust)) { return [$this->allSubdomainsOfApplicationUrl()]; } $hosts = match (true) { is_array(static::$alwaysTrust) => static::$alwaysTrust, is_callable(static::$alwaysTrust) => call_user_func(static::$alwaysTrust), default => [], }; if (static::$subdomains) { $hosts[] = $this->allSubdomainsOfApplicationUrl(); } return $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); } /** * Specify the hosts that should always be trusted. * * @param array<int, string>|(callable(): array<int, string>) $hosts * @param bool $subdomains * @return void */ public static function at(array|callable $hosts, bool $subdomains = true) { static::$alwaysTrust = $hosts; static::$subdomains = $subdomains; } /** * 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).'$'; } } /** * Flush the state of the middleware. * * @return void */ public static function flushState() { static::$alwaysTrust = null; static::$subdomains = null; } } framework/src/Illuminate/Http/Middleware/AddLinkHeadersForPreloadedAssets.php 0000644 00000001352 15060132304 0023407 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(', ')); } }); } } framework/src/Illuminate/Http/Middleware/TrustProxies.php 0000644 00000012152 15060132304 0017626 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 trusted proxies headers for the application. * * @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_AWS_ELB; /** * The proxies that have been configured to always be trusted. * * @var array<int, string>|string|null */ protected static $alwaysTrustProxies; /** * The proxies headers that have been configured to always be trusted. * * @var int|null */ protected static $alwaysTrustHeaders; /** * 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() { $headers = $this->headers(); if (is_int($headers)) { return $headers; } return match ($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 headers. * * @return int */ protected function headers() { return static::$alwaysTrustHeaders ?: $this->headers; } /** * Get the trusted proxies. * * @return array|string|null */ protected function proxies() { return static::$alwaysTrustProxies ?: $this->proxies; } /** * Specify the IP addresses of proxies that should always be trusted. * * @param array|string $proxies * @return void */ public static function at(array|string $proxies) { static::$alwaysTrustProxies = $proxies; } /** * Specify the proxy headers that should always be trusted. * * @param int $headers * @return void */ public static function withHeaders(int $headers) { static::$alwaysTrustHeaders = $headers; } /** * Flush the state of the middleware. * * @return void */ public static function flushState() { static::$alwaysTrustHeaders = null; static::$alwaysTrustProxies = null; } } framework/src/Illuminate/Http/FileHelpers.php 0000644 00000002264 15060132304 0015263 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()); } } framework/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php 0000644 00000010403 15060132304 0021775 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; } } framework/src/Illuminate/Http/Concerns/InteractsWithFlashData.php 0000644 00000003010 15060132304 0021161 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([]); } } framework/src/Illuminate/Http/Concerns/InteractsWithInput.php 0000644 00000035032 15060132304 0020442 0 ustar 00 <?php namespace Illuminate\Http\Concerns; use Illuminate\Http\UploadedFile; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Date; use Illuminate\Support\Traits\Dumpable; use SplFileInfo; use stdClass; use Symfony\Component\HttpFoundation\InputBag; trait InteractsWithInput { use Dumpable; /** * 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 items. * * @param mixed $keys * @return $this */ public function dump($keys = []) { $keys = is_array($keys) ? $keys : func_get_args(); dump(count($keys) > 0 ? $this->only($keys) : $this->all()); return $this; } } framework/src/Illuminate/Http/Concerns/CanBePrecognitive.php 0000644 00000002000 15060132304 0020146 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); } } framework/src/Illuminate/Http/LICENSE.md 0000644 00000002063 15060132304 0013751 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. framework/src/Illuminate/Http/composer.json 0000755 00000002431 15060132304 0015071 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.2", "ext-filter": "*", "fruitcake/php-cors": "^1.3", "guzzlehttp/guzzle": "^7.8", "guzzlehttp/uri-template": "^1.0", "illuminate/collections": "^11.0", "illuminate/macroable": "^11.0", "illuminate/session": "^11.0", "illuminate/support": "^11.0", "symfony/http-foundation": "^7.0", "symfony/http-kernel": "^7.0", "symfony/polyfill-php83": "^1.28", "symfony/mime": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Http\\": "" } }, "suggest": { "ext-gd": "Required to use Illuminate\\Http\\Testing\\FileFactory::image()." }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Http/ResponseTrait.php 0000644 00000007405 15060132304 0015665 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); } } framework/src/Illuminate/Http/JsonResponse.php 0000755 00000006707 15060132304 0015522 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 */ #[\Override] 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 */ #[\Override] 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 */ #[\Override] 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); } } framework/src/Illuminate/Http/RedirectResponse.php 0000755 00000013503 15060132304 0016342 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); } } framework/src/Illuminate/Http/UploadedFile.php 0000644 00000010011 15060132304 0015403 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; } } framework/src/Illuminate/Http/Testing/MimeType.php 0000644 00000002625 15060132304 0016230 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)); } } framework/src/Illuminate/Http/Testing/FileFactory.php 0000644 00000005220 15060132304 0016700 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()); }); } } framework/src/Illuminate/Http/Testing/File.php 0000644 00000005557 15060132304 0015365 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']; } } framework/src/Illuminate/Http/Response.php 0000755 00000006054 15060132304 0014663 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 */ #[\Override] 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); } } framework/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php 0000644 00000027032 15060132304 0022675 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); } $loadedValue = $this->resource->{$relationship}; if (func_num_args() === 1) { return $loadedValue; } if ($loadedValue === null) { return; } return value($value, $loadedValue); } /** * 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 (! array_key_exists($attribute, $this->resource->getAttributes())) { 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) { if (func_num_args() < 5) { $default = new MissingValue; } $attribute = (string) Str::of($relationship)->snake()->append('_')->append($aggregate)->append('_')->finish($column); if (! array_key_exists($attribute, $this->resource->getAttributes())) { 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 ); } } framework/src/Illuminate/Http/Resources/MergeValue.php 0000644 00000001243 15060132304 0017063 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; } } } framework/src/Illuminate/Http/Resources/Json/PaginatedResourceResponse.php 0000644 00000005107 15060132304 0023066 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', ]); } } framework/src/Illuminate/Http/Resources/Json/JsonResource.php 0000644 00000014140 15060132304 0020361 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')); } } framework/src/Illuminate/Http/Resources/Json/ResourceCollection.php 0000644 00000006427 15060132304 0021554 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); } } framework/src/Illuminate/Http/Resources/Json/AnonymousResourceCollection.php 0000644 00000001234 15060132304 0023454 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); } } framework/src/Illuminate/Http/Resources/Json/ResourceResponse.php 0000644 00000006270 15060132304 0021253 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; } } framework/src/Illuminate/Http/Resources/MissingValue.php 0000644 00000000415 15060132304 0017435 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; } } framework/src/Illuminate/Http/Resources/CollectsResources.php 0000644 00000005000 15060132304 0020465 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(); } } framework/src/Illuminate/Http/Resources/DelegatesToResource.php 0000644 00000006447 15060132304 0020752 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); } } framework/src/Illuminate/Http/Resources/PotentiallyMissing.php 0000644 00000000331 15060132304 0020662 0 ustar 00 <?php namespace Illuminate\Http\Resources; interface PotentiallyMissing { /** * Determine if the object should be considered "missing". * * @return bool */ public function isMissing(); } framework/src/Illuminate/Http/File.php 0000644 00000000233 15060132304 0013732 0 ustar 00 <?php namespace Illuminate\Http; use Symfony\Component\HttpFoundation\File\File as SymfonyFile; class File extends SymfonyFile { use FileHelpers; } framework/src/Illuminate/Http/Exceptions/HttpResponseException.php 0000644 00000001426 15060132304 0021516 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; } } framework/src/Illuminate/Http/Exceptions/ThrottleRequestsException.php 0000644 00000001171 15060132304 0022416 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); } } framework/src/Illuminate/Http/Exceptions/PostTooLargeException.php 0000644 00000001125 15060132304 0021436 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); } } framework/src/Illuminate/Http/Client/Request.php 0000644 00000014337 15060132304 0015733 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.'); } } framework/src/Illuminate/Http/Client/PendingRequest.php 0000644 00000120017 15060132304 0017231 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 OutOfBoundsException; 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 \Psr\Http\Message\StreamInterface|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 mergeable via array_merge_recursive. * * @var array */ protected $mergeableOptions = [ '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, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, '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 \Psr\Http\Message\StreamInterface|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 array|int $times * @param Closure|int $sleepMilliseconds * @param callable|null $when * @param bool $throw * @return $this */ public function retry(array|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->mergeableOptions)), $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 * @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 * * @throws \Illuminate\Http\Client\ConnectionException */ 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 * * @throws \Illuminate\Http\Client\ConnectionException */ 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 * * @throws \Illuminate\Http\Client\ConnectionException */ 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 * * @throws \Illuminate\Http\Client\ConnectionException */ 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 * * @throws \Illuminate\Http\Client\ConnectionException */ 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 * * @throws \Illuminate\Http\Client\ConnectionException */ 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 * @throws \Illuminate\Http\Client\ConnectionException */ 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(new Request($e->getRequest())); 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 * @param int $attempt * @return \GuzzleHttp\Promise\PromiseInterface */ protected function makePromise(string $method, string $url, array $options = [], int $attempt = 1) { 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 (OutOfBoundsException|TransferException $e) { if ($e instanceof ConnectException) { $this->dispatchConnectionFailedEvent(new Request($e->getRequest())); return new ConnectionException($e->getMessage(), 0, $e); } return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse($this->newResponse($e->getResponse())) : $e; }) ->then(function (Response|ConnectionException|TransferException $response) use ($method, $url, $options, $attempt) { return $this->handlePromiseResponse($response, $method, $url, $options, $attempt); }); } /** * Handle the response of an asynchronous request. * * @param \Illuminate\Http\Client\Response $response * @param string $method * @param string $url * @param array $options * @param int $attempt * @return mixed */ protected function handlePromiseResponse(Response|ConnectionException|TransferException $response, $method, $url, $options, $attempt) { if ($response instanceof Response && $response->successful()) { return $response; } if ($response instanceof RequestException) { $response = $this->populateResponse($this->newResponse($response->getResponse())); } try { $shouldRetry = $this->retryWhenCallback ? call_user_func( $this->retryWhenCallback, $response instanceof Response ? $response->toException() : $response, $this ) : true; } catch (Exception $exception) { return $exception; } if ($attempt < $this->tries && $shouldRetry) { $options['delay'] = value( $this->retryDelay, $attempt, $response instanceof Response ? $response->toException() : $response ); return $this->makePromise($method, $url, $options, $attempt + 1); } if ($response instanceof Response && $this->throwCallback && ($this->throwIfCallback === null || call_user_func($this->throwIfCallback, $response))) { try { $response->throw($this->throwCallback); } catch (Exception $exception) { return $exception; } } if ($this->tries > 1 && $this->retryThrow) { return $response instanceof Response ? $response->toException() : $response; } return $response; } /** * 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->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->mergeableOptions)), ...$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. * * @param \Illuminate\Http\Client\Request $request * @return void */ protected function dispatchConnectionFailedEvent(Request $request) { if ($dispatcher = $this->factory?->getDispatcher()) { $dispatcher->dispatch(new ConnectionFailed($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; } } framework/src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php 0000644 00000006412 15060132304 0022146 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; } } framework/src/Illuminate/Http/Client/Events/ConnectionFailed.php 0000644 00000000720 15060132304 0020742 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; } } framework/src/Illuminate/Http/Client/Events/ResponseReceived.php 0000644 00000001342 15060132304 0021004 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; } } framework/src/Illuminate/Http/Client/Events/RequestSending.php 0000644 00000000716 15060132304 0020503 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; } } framework/src/Illuminate/Http/Client/RequestException.php 0000644 00000001767 15060132304 0017615 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"; } } framework/src/Illuminate/Http/Client/ConnectionException.php 0000644 00000000153 15060132304 0020250 0 ustar 00 <?php namespace Illuminate\Http\Client; class ConnectionException extends HttpClientException { // } framework/src/Illuminate/Http/Client/ResponseSequence.php 0000644 00000006735 15060132304 0017575 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); } } framework/src/Illuminate/Http/Client/Factory.php 0000644 00000026764 15060132304 0015721 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 options to apply to every request. * * @var \Closure|array */ protected $globalOptions = []; /** * 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; } /** * Set the options to apply to every request. * * @param \Closure|array $options * @return $this */ public function globalOptions($options) { $this->globalOptions = $options; 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 */ public function createPendingRequest() { return tap($this->newPendingRequest(), function ($request) { $request->stub($this->stubCallbacks)->preventStrayRequests($this->preventStrayRequests); }); } /** * Instantiate a new pending request instance for this factory. * * @return \Illuminate\Http\Client\PendingRequest */ protected function newPendingRequest() { return (new PendingRequest($this, $this->globalMiddleware))->withOptions(value($this->globalOptions)); } /** * Get the current event dispatcher implementation. * * @return \Illuminate\Contracts\Events\Dispatcher|null */ public function getDispatcher() { return $this->dispatcher; } /** * Get the array of global middleware. * * @return array */ public function getGlobalMiddleware() { return $this->globalMiddleware; } /** * 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 $this->createPendingRequest()->{$method}(...$parameters); } } framework/src/Illuminate/Http/Client/Response.php 0000644 00000023100 15060132304 0016065 0 ustar 00 <?php namespace Illuminate\Http\Client; use ArrayAccess; use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; use LogicException; use Stringable; class Response implements ArrayAccess, Stringable { 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. * * @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 * @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); } } framework/src/Illuminate/Http/Client/HttpClientException.php 0000644 00000000161 15060132304 0020226 0 ustar 00 <?php namespace Illuminate\Http\Client; use Exception; class HttpClientException extends Exception { // } framework/src/Illuminate/Http/Client/Pool.php 0000644 00000003414 15060132304 0015206 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); } } framework/src/Illuminate/Database/DatabaseTransactionRecord.php 0000755 00000003127 15060132304 0020721 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 parent instance of this transaction. * * @var \Illuminate\Database\DatabaseTransactionRecord */ public $parent; /** * 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 * @param \Illuminate\Database\DatabaseTransactionRecord|null $parent * @return void */ public function __construct($connection, $level, ?DatabaseTransactionRecord $parent = null) { $this->connection = $connection; $this->level = $level; $this->parent = $parent; } /** * 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; } } framework/src/Illuminate/Database/LostConnectionException.php 0000644 00000000174 15060132304 0020464 0 ustar 00 <?php namespace Illuminate\Database; use LogicException; class LostConnectionException extends LogicException { // } framework/src/Illuminate/Database/DeadlockException.php 0000644 00000000162 15060132304 0017226 0 ustar 00 <?php namespace Illuminate\Database; use PDOException; class DeadlockException extends PDOException { // } framework/src/Illuminate/Database/RecordsNotFoundException.php 0000755 00000000201 15060132304 0020573 0 ustar 00 <?php namespace Illuminate\Database; use RuntimeException; class RecordsNotFoundException extends RuntimeException { // } framework/src/Illuminate/Database/Connectors/SqlServerConnector.php 0000755 00000014711 15060132304 0021567 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(); } } framework/src/Illuminate/Database/Connectors/ConnectorInterface.php 0000755 00000000360 15060132304 0021534 0 ustar 00 <?php namespace Illuminate\Database\Connectors; interface ConnectorInterface { /** * Establish a database connection. * * @param array $config * @return \PDO */ public function connect(array $config); } framework/src/Illuminate/Database/Connectors/Connector.php 0000755 00000005537 15060132304 0017726 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 \Throwable */ 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; } } framework/src/Illuminate/Database/Connectors/MariaDbConnector.php 0000755 00000001342 15060132304 0021134 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use PDO; class MariaDbConnector extends MySqlConnector { /** * Get the sql_mode value. * * @param \PDO $connection * @param array $config * @return string|null */ protected function getSqlMode(PDO $connection, array $config) { if (isset($config['modes'])) { return implode(',', $config['modes']); } if (! isset($config['strict'])) { return null; } if (! $config['strict']) { return 'NO_ENGINE_SUBSTITUTION'; } return 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; } } framework/src/Illuminate/Database/Connectors/MySqlConnector.php 0000755 00000010543 15060132304 0020705 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->configureConnection($connection, $config); return $connection; } /** * 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}"; } /** * Configure the given PDO connection. * * @param \PDO $connection * @param array $config * @return void */ protected function configureConnection(PDO $connection, array $config) { if (isset($config['isolation_level'])) { $connection->exec(sprintf('SET SESSION TRANSACTION ISOLATION LEVEL %s;', $config['isolation_level'])); } $statements = []; if (isset($config['charset'])) { if (isset($config['collation'])) { $statements[] = sprintf("NAMES '%s' COLLATE '%s'", $config['charset'], $config['collation']); } else { $statements[] = sprintf("NAMES '%s'", $config['charset']); } } if (isset($config['timezone'])) { $statements[] = sprintf("time_zone='%s'", $config['timezone']); } $sqlMode = $this->getSqlMode($connection, $config); if ($sqlMode !== null) { $statements[] = sprintf("SESSION sql_mode='%s'", $sqlMode); } if ($statements !== []) { $connection->exec(sprintf('SET %s;', implode(', ', $statements))); } } /** * Get the sql_mode value. * * @param \PDO $connection * @param array $config * @return string|null */ protected function getSqlMode(PDO $connection, array $config) { if (isset($config['modes'])) { return implode(',', $config['modes']); } if (! isset($config['strict'])) { return null; } if (! $config['strict']) { return 'NO_ENGINE_SUBSTITUTION'; } $version = $config['version'] ?? $connection->getAttribute(PDO::ATTR_SERVER_VERSION); if (version_compare($version, '8.0.11') >= 0) { return 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; } return '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'; } } framework/src/Illuminate/Database/Connectors/ConnectionFactory.php 0000755 00000017536 15060132304 0021425 0 ustar 00 <?php namespace Illuminate\Database\Connectors; use Illuminate\Contracts\Container\Container; use Illuminate\Database\Connection; use Illuminate\Database\MariaDbConnection; 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, 'mariadb' => new MariaDbConnector, '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), 'mariadb' => new MariaDbConnection($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}]."), }; } } framework/src/Illuminate/Database/Connectors/SQLiteConnector.php 0000755 00000002533 15060132304 0021001 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); } } framework/src/Illuminate/Database/Connectors/PostgresConnector.php 0000755 00000015032 15060132304 0021444 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(); } } framework/src/Illuminate/Database/Eloquent/Prunable.php 0000644 00000002704 15060132304 0017211 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() { // } } framework/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php 0000755 00000026025 15060132304 0021302 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations; use BackedEnum; 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) { $foreignKey = $model->{$this->foreignKey}; return $foreignKey instanceof BackedEnum ? $foreignKey->value : $foreignKey; } /** * Get the name of the relationship. * * @return string */ public function getRelationName() { return $this->relationName; } } framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php 0000755 00000036253 15060132304 0021715 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() ); } /** * 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) { if ($this->parent->exists) { $this->query->limit($value); } else { $this->query->groupLimit($value, $this->getExistenceCompareKey()); } return $this; } /** * 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; } } framework/src/Illuminate/Database/Eloquent/Relations/HasOneThrough.php 0000644 00000004316 15060132304 0022120 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(); } } framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithDictionary.php 0000644 00000001620 15060132304 0025765 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use BackedEnum; use InvalidArgumentException; use UnitEnum; trait InteractsWithDictionary { /** * Get a dictionary key attribute - casting it to a string if necessary. * * @param mixed $attribute * @return mixed * * @throws \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; } } framework/src/Illuminate/Database/Eloquent/Relations/Concerns/ComparesRelatedModels.php 0000644 00000004047 15060132304 0025373 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; } } framework/src/Illuminate/Database/Eloquent/Relations/Concerns/SupportsDefaultModels.php 0000644 00000003021 15060132304 0025454 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; } } framework/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php 0000644 00000020761 15060132304 0022543 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; } } framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php 0000644 00000050674 15060132304 0025746 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Relations\Concerns; use BackedEnum; 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, []]; } if ($id instanceof BackedEnum) { $id = $id->value; } 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, }; } } framework/src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php 0000644 00000023230 15060132304 0023674 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; } } framework/src/Illuminate/Database/Eloquent/Relations/MorphMany.php 0000755 00000004471 15060132304 0021321 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)); } } framework/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php 0000644 00000013737 15060132304 0021626 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 fully qualified morph type for the relation. * * @return string */ public function getQualifiedMorphTypeName() { return $this->qualifyPivotColumn($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; } } framework/src/Illuminate/Database/Eloquent/Relations/MorphTo.php 0000644 00000030141 15060132304 0020765 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|null $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; } } } framework/src/Illuminate/Database/Eloquent/Relations/Pivot.php 0000755 00000000724 15060132304 0020505 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<string>|bool */ protected $guarded = []; } framework/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php 0000644 00000064545 15060132304 0022315 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\Query\Grammars\MySqlGrammar; 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|null */ 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 \Illuminate\Database\Eloquent\Model|static|null */ 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->query->applyAfterQueryCallbacks( $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); } /** * 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) { $column ??= $this->getRelated()->getQualifiedKeyName(); $alias ??= $this->getRelated()->getKeyName(); return $this->prepareQueryBuilder()->chunkByIdDesc($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); } /** * 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 */ public function lazyByIdDesc($chunkSize = 1000, $column = null, $alias = null) { $column ??= $this->getRelated()->getQualifiedKeyName(); $alias ??= $this->getRelated()->getKeyName(); return $this->prepareQueryBuilder()->lazyByIdDesc($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 ); } /** * 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) { if ($this->farParent->exists) { $this->query->limit($value); } else { $column = $this->getQualifiedFirstKeyName(); $grammar = $this->query->getQuery()->getGrammar(); if ($grammar instanceof MySqlGrammar && $grammar->useLegacyGroupLimit($this->query->getQuery())) { $column = 'laravel_through_key'; } $this->query->groupLimit($value, $column); } return $this; } /** * 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; } } framework/src/Illuminate/Database/Eloquent/Relations/HasMany.php 0000755 00000003011 15060132304 0020734 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); } } framework/src/Illuminate/Database/Eloquent/Relations/MorphOne.php 0000755 00000010254 15060132304 0021132 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()); } } framework/src/Illuminate/Database/Eloquent/Relations/MorphPivot.php 0000644 00000010742 15060132304 0021511 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->exists = false; $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; } } framework/src/Illuminate/Database/Eloquent/Relations/HasOne.php 0000755 00000010056 15060132304 0020560 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()); } } framework/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php 0000755 00000006775 15060132304 0022275 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; } } framework/src/Illuminate/Database/Eloquent/Relations/Relation.php 0000755 00000032461 15060132304 0021164 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; } } framework/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php 0000755 00000134141 15060132304 0022126 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\Query\Grammars\MySqlGrammar; 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<string|\Illuminate\Contracts\Database\Query\Expression> */ 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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression|array<string, string> $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $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|\Illuminate\Contracts\Database\Query\Expression $column * @return $this */ public function orWherePivotNotNull($column) { return $this->orWherePivotNull($column, true); } /** * Add an "order by" clause for a pivot table column. * * @param string|\Illuminate\Contracts\Database\Query\Expression $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|null */ 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 \Illuminate\Database\Eloquent\Model|static|null */ 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->query->applyAfterQueryCallbacks( $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) { 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); } /** * 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); } /** * 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->getRelated()->qualifyColumn( $this->getRelatedKeyName() ); $alias ??= $this->getRelatedKeyName(); return $this->prepareQueryBuilder()->orderedChunkById($count, function ($results, $page) use ($callback) { $this->hydratePivotRelation($results->all()); return $callback($results, $page); }, $column, $alias, $descending); } /** * 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; }); } /** * 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 */ public function lazyByIdDesc($chunkSize = 1000, $column = null, $alias = null) { $column ??= $this->getRelated()->qualifyColumn( $this->getRelatedKeyName() ); $alias ??= $this->getRelatedKeyName(); return $this->prepareQueryBuilder()->lazyByIdDesc($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() { if ($this->related->isIgnoringTouch()) { return; } $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); } /** * 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) { if ($this->parent->exists) { $this->query->limit($value); } else { $column = $this->getExistenceCompareKey(); $grammar = $this->query->getQuery()->getGrammar(); if ($grammar instanceof MySqlGrammar && $grammar->useLegacyGroupLimit($this->query->getQuery())) { $column = 'pivot_'.last(explode('.', $column)); } $this->query->groupLimit($value, $column); } return $this; } /** * 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|\Illuminate\Contracts\Database\Query\Expression $column * @return string|\Illuminate\Contracts\Database\Query\Expression */ public function qualifyPivotColumn($column) { if ($this->query->getQuery()->getGrammar()->isExpression($column)) { return $column; } return str_contains($column, '.') ? $column : $this->table.'.'.$column; } } framework/src/Illuminate/Database/Eloquent/Model.php 0000644 00000174237 15060132304 0016514 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; use Stringable; abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToString, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, Stringable, 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 object's 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; } if ($this->isClassDeviable($column)) { $amount = (clone $this)->setAttribute($column, $amount)->getAttributeFromArray($column); } 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->getDirtyForUpdate(); 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(), 'laravel_through_key', ])); $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\Contracts\Database\Eloquent\Builder */ 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(); } } framework/src/Illuminate/Database/Eloquent/PendingHasThroughRelationship.php 0000644 00000005714 15060132304 0023410 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 )); } } framework/src/Illuminate/Database/Eloquent/HigherOrderBuilderProxy.php 0000644 00000002055 15060132304 0022213 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); }); } } framework/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php 0000644 00000011736 15060132304 0022002 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; } } framework/src/Illuminate/Database/Eloquent/Concerns/HasUuids.php 0000644 00000004211 15060132304 0020733 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\Contracts\Database\Eloquent\Builder * * @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; } } framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php 0000644 00000026507 15060132304 0021122 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Eloquent\Attributes\ObservedBy; use Illuminate\Events\NullDispatcher; use Illuminate\Support\Arr; use InvalidArgumentException; use ReflectionClass; 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 = []; /** * Boot the has event trait for a model. * * @return void */ public static function bootHasEvents() { static::observe(static::resolveObserveAttributes()); } /** * Resolve the observe class names from the attributes. * * @return array */ public static function resolveObserveAttributes() { $reflectionClass = new ReflectionClass(static::class); return collect($reflectionClass->getAttributes(ObservedBy::class)) ->map(fn ($attribute) => $attribute->getArguments()) ->flatten() ->all(); } /** * 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 map for the model. * * @return array */ public function dispatchesEvents() { return $this->dispatchesEvents; } /** * 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); } } } } framework/src/Illuminate/Database/Eloquent/Concerns/HidesAttributes.php 0000644 00000005535 15060132304 0022323 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; } } framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php 0000644 00000207137 15060132304 0022004 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; use ValueError; 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; /** * Initialize the trait. * * @return void */ protected function initializeHasAttributes() { $this->casts = $this->ensureCastsAreStringValues( array_merge($this->casts, $this->casts()), ); } /** * 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($this->getCasts()[$key], $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; } /** * Determine whether an attribute exists on the model. * * @param string $key * @return bool */ public function hasAttribute($key) { if (! $key) { return false; } return array_key_exists($key, $this->attributes) || array_key_exists($key, $this->casts) || $this->hasGetMutator($key) || $this->hasAttributeMutator($key) || $this->isClassCastable($key); } /** * 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 ($this->hasAttribute($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) { $casts = $this->ensureCastsAreStringValues($casts); $this->casts = array_merge($this->casts, $casts); return $this; } /** * Ensure that the given casts are strings. * * @param array $casts * @return array */ protected function ensureCastsAreStringValues($casts) { foreach ($casts as $attribute => $cast) { $casts[$attribute] = match (true) { is_array($cast) => value(function () use ($cast) { if (count($cast) === 1) { return $cast[0]; } [$cast, $arguments] = [array_shift($cast), $cast]; return $cast.':'.implode(',', $arguments); }), default => $cast, }; } return $casts; } /** * 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($enumClass, $value); } else { $this->attributes[$key] = $this->getStorableEnumValue( $enumClass, $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 string $expectedEnum * @param \UnitEnum|\BackedEnum $value * @return string|int */ protected function getStorableEnumValue($expectedEnum, $value) { if (! $value instanceof $expectedEnum) { throw new ValueError(sprintf('Value [%s] is not of the expected enum type [%s].', var_export($value, true), $expectedEnum)); } 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::currentEncrypter()->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::currentEncrypter()->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; } /** * Get the current encrypter being used by the model. * * @return \Illuminate\Contracts\Encryption\Encrypter */ protected static function currentEncrypter() { return static::$encrypter ?? Crypt::getFacadeRoot(); } /** * 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, date_default_timezone_get()); } // 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 attributes that should be cast. * * @return array */ public function getCasts() { if ($this->getIncrementing()) { return array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts); } return $this->casts; } /** * Get the attributes that should be cast. * * @return array */ protected function casts() { return []; } /** * 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 have been changed since the last sync for an update operation. * * @return array */ protected function getDirtyForUpdate() { return $this->getDirty(); } /** * 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->isEncryptedCastable($key) && ! empty(static::currentEncrypter()->getPreviousKeys())) { return false; } 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])) { if (empty(static::currentEncrypter()->getPreviousKeys())) { return $this->fromEncryptedString($attribute) === $this->fromEncryptedString($original); } return false; } 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)) { if (static::preventsAccessingMissingAttributes() && ! array_key_exists($key, $this->attributes) && ($this->isEnumCastable($key) || in_array($this->getCastType($key), static::$primitiveCastTypes))) { $this->throwMissingAttributeExceptionIfApplicable($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(); } } framework/src/Illuminate/Database/Eloquent/Concerns/HasUlids.php 0000644 00000004216 15060132304 0020727 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\Contracts\Database\Eloquent\Builder * * @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; } } framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php 0000644 00000013606 15060132304 0022512 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; trait GuardsAttributes { /** * The attributes that are mass assignable. * * @var array<int, 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; } } framework/src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php 0000644 00000007576 15060132304 0022240 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Concerns; use Closure; use Illuminate\Database\Eloquent\Attributes\ScopedBy; use Illuminate\Database\Eloquent\Scope; use Illuminate\Support\Arr; use InvalidArgumentException; use ReflectionClass; trait HasGlobalScopes { /** * Boot the has global scopes trait for a model. * * @return void */ public static function bootHasGlobalScopes() { static::addGlobalScopes(static::resolveGlobalScopeAttributes()); } /** * Resolve the global scope class names from the attributes. * * @return array */ public static function resolveGlobalScopeAttributes() { $reflectionClass = new ReflectionClass(static::class); return collect($reflectionClass->getAttributes(ScopedBy::class)) ->map(fn ($attribute) => $attribute->getArguments()) ->flatten() ->all(); } /** * 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; } elseif (is_string($scope) && class_exists($scope) && is_subclass_of($scope, Scope::class)) { return static::$globalScopes[static::class][$scope] = new $scope; } throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope or be a class name of a class extending '.Scope::class); } /** * Register multiple global scopes on the model. * * @param array $scopes * @return void */ public static function addGlobalScopes(array $scopes) { foreach ($scopes as $key => $scope) { if (is_string($key)) { static::addGlobalScope($key, $scope); } else { static::addGlobalScope($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, []); } } framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php 0000644 00000100045 15060132304 0023372 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(); } if (empty($types)) { return $this->where(new Expression('0'), $operator, $count, $boolean); } 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 \Illuminate\Contracts\Database\Query\Expression|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) { if ($this->getQuery()->getGrammar()->isExpression($column)) { $aggregateColumn = $this->getQuery()->getGrammar()->getValue($column); } else { $hashedColumn = $this->getRelationHashedColumn($column, $relation); $aggregateColumn = $this->getQuery()->getGrammar()->wrap( $column === '*' ? $column : $relation->getRelated()->qualifyColumn($hashedColumn) ); } $expression = $function === 'exists' ? $aggregateColumn : sprintf('%s(%s)', $function, $aggregateColumn); } else { $expression = $this->getQuery()->getGrammar()->getValue($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 {$this->getQuery()->getGrammar()->getValue($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 \Illuminate\Contracts\Database\Query\Expression|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 \Illuminate\Contracts\Database\Query\Expression|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 \Illuminate\Contracts\Database\Query\Expression|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 \Illuminate\Contracts\Database\Query\Expression|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; } } framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php 0000644 00000074047 15060132304 0022504 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; } } framework/src/Illuminate/Database/Eloquent/Concerns/HasUniqueIds.php 0000644 00000001764 15060132304 0021562 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 []; } } framework/src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php 0000644 00000006322 15060132304 0023641 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; } } framework/src/Illuminate/Database/Eloquent/JsonEncodingException.php 0000644 00000002555 15060132304 0021704 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}."); } } framework/src/Illuminate/Database/Eloquent/Scope.php 0000644 00000000536 15060132304 0016513 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); } framework/src/Illuminate/Database/Eloquent/SoftDeletingScope.php 0000644 00000011237 15060132304 0021023 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; }); } } framework/src/Illuminate/Database/Eloquent/Builder.php 0000755 00000166054 15060132304 0017043 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', 'getrawbindings', 'implode', 'insert', 'insertgetid', 'insertorignore', 'insertusing', 'insertorignoreusing', 'max', 'min', 'raw', 'rawvalue', 'sum', 'tosql', 'torawsql', ]; /** * Applied global scopes. * * @var array */ protected $scopes = []; /** * Removed global scopes. * * @var array */ protected $removedScopes = []; /** * The callbacks that should be invoked after retrieving data from the database. * * @var array */ protected $afterQueryCallbacks = []; /** * 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 $this->applyAfterQueryCallbacks( $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.'.'); } /** * Register a closure to be invoked after the query is executed. * * @param \Closure $callback * @return $this */ public function afterQuery(Closure $callback) { $this->afterQueryCallbacks[] = $callback; return $this; } /** * Invoke the "after query" modification callbacks. * * @param mixed $result * @return mixed */ public function applyAfterQueryCallbacks($result) { foreach ($this->afterQueryCallbacks as $afterQueryCallback) { $result = $afterQueryCallback($result) ?: $result; } return $result; } /** * Get a lazy collection for the given query. * * @return \Illuminate\Support\LazyCollection */ public function cursor() { return $this->applyScopes()->query->cursor()->map(function ($record) { $model = $this->newModelInstance()->newFromBuilder($record); return $this->applyAfterQueryCallbacks($this->newModelInstance()->newCollection([$model]))->first(); })->reject(fn ($model) => is_null($model)); } /** * 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; $column = Str::after($column, "{$this->model->getTable()}."); // 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 $this->applyAfterQueryCallbacks( $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, $total = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $total = value($total) ?? $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(fn ($logicalOperator) => str_contains($logicalOperator, 'or'))) { $query->wheres[] = $this->createNestedWhere( $whereSlice, str_replace(' not', '', $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 Eloquent builder instances that are used in the union of the query. * * @return \Illuminate\Support\Collection */ protected function getUnionBuilders() { return isset($this->query->unions) ? collect($this->query->unions)->pluck('query') : collect(); } /** * 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; } } framework/src/Illuminate/Database/Eloquent/BroadcastsEvents.php 0000644 00000013354 15060132304 0020716 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; } } framework/src/Illuminate/Database/Eloquent/ModelNotFoundException.php 0000755 00000002613 15060132304 0022037 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; } } framework/src/Illuminate/Database/Eloquent/InvalidCastException.php 0000644 00000001642 15060132304 0021521 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 void */ 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; } } framework/src/Illuminate/Database/Eloquent/MassAssignmentException.php 0000755 00000000211 15060132304 0022246 0 ustar 00 <?php namespace Illuminate\Database\Eloquent; use RuntimeException; class MassAssignmentException extends RuntimeException { // } framework/src/Illuminate/Database/Eloquent/MassPrunable.php 0000644 00000002257 15060132304 0020040 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.'); } } framework/src/Illuminate/Database/Eloquent/MissingAttributeException.php 0000755 00000001064 15060132304 0022616 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) )); } } framework/src/Illuminate/Database/Eloquent/Attributes/ObservedBy.php 0000644 00000000535 15060132304 0021633 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Attributes; use Attribute; #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] class ObservedBy { /** * Create a new attribute instance. * * @param array|string $classes * @return void */ public function __construct(array|string $classes) { } } framework/src/Illuminate/Database/Eloquent/Attributes/ScopedBy.php 0000644 00000000533 15060132304 0021275 0 ustar 00 <?php namespace Illuminate\Database\Eloquent\Attributes; use Attribute; #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] class ScopedBy { /** * Create a new attribute instance. * * @param array|string $classes * @return void */ public function __construct(array|string $classes) { } } framework/src/Illuminate/Database/Eloquent/Factories/CrossJoinSequence.php 0000644 00000001035 15060132304 0022756 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); } } framework/src/Illuminate/Database/Eloquent/Factories/Relationship.php 0000644 00000004150 15060132304 0022016 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; } } framework/src/Illuminate/Database/Eloquent/Factories/BelongsToRelationship.php 0000644 00000005265 15060132304 0023643 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; } } framework/src/Illuminate/Database/Eloquent/Factories/Sequence.php 0000644 00000002160 15060132304 0021124 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; }); } } framework/src/Illuminate/Database/Eloquent/Factories/BelongsToManyRelationship.php 0000644 00000004035 15060132304 0024462 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; } } framework/src/Illuminate/Database/Eloquent/Factories/HasFactory.php 0000644 00000001535 15060132304 0021424 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() { // } } framework/src/Illuminate/Database/Eloquent/Factories/Factory.php 0000644 00000071245 15060132304 0020775 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) { $records ??= ($this->count ?? 1); $this->count = null; 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 ); } } } framework/src/Illuminate/Database/Eloquent/SoftDeletes.php 0000644 00000014427 15060132304 0017667 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()); } } framework/src/Illuminate/Database/Eloquent/Collection.php 0000755 00000053032 15060132304 0017537 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>|null $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()); } } framework/src/Illuminate/Database/Eloquent/BroadcastsEventsAfterCommit.php 0000644 00000000534 15060132304 0023045 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; } } framework/src/Illuminate/Database/Eloquent/Casts/Attribute.php 0000644 00000003670 15060132304 0020464 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; } } framework/src/Illuminate/Database/Eloquent/Casts/ArrayObject.php 0000644 00000001616 15060132304 0020724 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(); } } framework/src/Illuminate/Database/Eloquent/Casts/AsEncryptedCollection.php 0000644 00000003555 15060132304 0022760 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; } }; } /** * Specify the collection for the cast. * * @param class-string $class * @return string */ public static function using($class) { return static::class.':'.$class; } } framework/src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php 0000644 00000005341 15060132304 0021722 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])) { 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; } }; } /** * Specify the Enum for the cast. * * @param class-string $class * @return string */ public static function of($class) { return static::class.':'.$class; } } framework/src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php 0000644 00000005434 15060132304 0022037 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])) { 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; } }; } /** * Specify the Enum for the cast. * * @param class-string $class * @return string */ public static function of($class) { return static::class.':'.$class; } } framework/src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php 0000644 00000002420 15060132304 0021202 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, ArrayObject::ARRAY_AS_PROPS) : 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(); } }; } } framework/src/Illuminate/Database/Eloquent/Casts/AsEncryptedArrayObject.php 0000644 00000002620 15060132304 0023062 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; } }; } } framework/src/Illuminate/Database/Eloquent/Casts/AsStringable.php 0000644 00000001667 15060132304 0021103 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; } }; } } framework/src/Illuminate/Database/Eloquent/Casts/Json.php 0000644 00000002204 15060132304 0017422 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; } } framework/src/Illuminate/Database/Eloquent/Casts/AsCollection.php 0000644 00000003353 15060132304 0021076 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)]; } }; } /** * Specify the collection for the cast. * * @param class-string $class * @return string */ public static function using($class) { return static::class.':'.$class; } } framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php 0000755 00000001751 15060132304 0022556 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; } } framework/src/Illuminate/Database/Eloquent/QueueEntityResolver.php 0000644 00000001245 15060132304 0021443 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); } } framework/src/Illuminate/Database/Migrations/Migration.php 0000755 00000001025 15060132304 0017710 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; } } framework/src/Illuminate/Database/Migrations/stubs/migration.update.stub 0000755 00000001036 15060132304 0022561 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) { // }); } }; framework/src/Illuminate/Database/Migrations/stubs/migration.stub 0000755 00000000570 15060132304 0021302 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 { // } }; framework/src/Illuminate/Database/Migrations/stubs/migration.create.stub 0000755 00000001033 15060132304 0022537 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 }}'); } }; framework/src/Illuminate/Database/Migrations/MigrationCreator.php 0000755 00000013675 15060132304 0021246 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; } } framework/src/Illuminate/Database/Migrations/Migrator.php 0000755 00000053256 15060132304 0017560 0 ustar 00 <?php namespace Illuminate\Database\Migrations; use Illuminate\Console\View\Components\BulletList; 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')); $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 () { $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 () { $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) { $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']; })); } /** * 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) { $this->events?->dispatch($event); } } framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php 0000755 00000012562 15060132304 0023445 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 $batch * @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; } } framework/src/Illuminate/Database/Migrations/MigrationRepositoryInterface.php 0000755 00000003433 15060132304 0023636 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); } framework/src/Illuminate/Database/Concerns/ExplainsQueries.php 0000644 00000000704 15060132304 0020536 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); } } framework/src/Illuminate/Database/Concerns/CompilesJsonPaths.php 0000644 00000003007 15060132304 0021021 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.'"'; } } framework/src/Illuminate/Database/Concerns/ParsesSearchPath.php 0000644 00000001102 15060132304 0020606 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 ?? []); } } framework/src/Illuminate/Database/Concerns/BuildsQueries.php 0000644 00000044175 15060132304 0020207 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 * * @throws \RuntimeException */ 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)) { // Reset the union bindings so we can add the cursor where in the correct position... $this->setBindings([], 'union'); $addCursorConditions = function (self $builder, $previousColumn, $originalColumn, $i) use (&$addCursorConditions, $cursor, $orders) { $unionBuilders = $builder->getUnionBuilders(); 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($unionBuilder, $previousColumn), '=', $cursor->parameter($previousColumn) ); $this->addBinding($unionBuilder->getRawBindings()['where'], 'union'); }); } $builder->where(function (self $secondBuilder) use ($addCursorConditions, $cursor, $orders, $i, $unionBuilders) { ['column' => $column, 'direction' => $direction] = $orders[$i]; $originalColumn = $this->getOriginalColumnNameForCursorPagination($this, $column); $secondBuilder->where( Str::contains($originalColumn, ['(', ')']) ? new Expression($originalColumn) : $originalColumn, $direction === 'asc' ? '>' : '<', $cursor->parameter($column) ); if ($i < $orders->count() - 1) { $secondBuilder->orWhere(function (self $thirdBuilder) use ($addCursorConditions, $column, $originalColumn, $i) { $addCursorConditions($thirdBuilder, $column, $originalColumn, $i + 1); }); } $unionBuilders->each(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions) { $unionWheres = $unionBuilder->getRawBindings()['where']; $originalColumn = $this->getOriginalColumnNameForCursorPagination($unionBuilder, $column); $unionBuilder->where(function ($unionBuilder) use ($column, $direction, $cursor, $i, $orders, $addCursorConditions, $originalColumn, $unionWheres) { $unionBuilder->where( $originalColumn, $direction === 'asc' ? '>' : '<', $cursor->parameter($column) ); if ($i < $orders->count() - 1) { $unionBuilder->orWhere(function (self $fourthBuilder) use ($addCursorConditions, $column, $originalColumn, $i) { $addCursorConditions($fourthBuilder, $column, $originalColumn, $i + 1); }); } $this->addBinding($unionWheres, 'union'); $this->addBinding($unionBuilder->getRawBindings()['where'], 'union'); }); }); }); }; $addCursorConditions($this, null, 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; } } framework/src/Illuminate/Database/Concerns/ManagesTransactions.php 0000644 00000022647 15060132304 0021373 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; } $levelBeingCommitted = $this->transactions; try { if ($this->transactions == 1) { $this->fireConnectionEvent('committing'); $this->getPdo()->commit(); } $this->transactions = max(0, $this->transactions - 1); } catch (Throwable $e) { $this->handleCommitTransactionException( $e, $currentAttempt, $attempts ); continue; } $this->transactionsManager?->commit( $this->getName(), $levelBeingCommitted, $this->transactions ); $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() { foreach ($this->beforeStartingTransaction as $callback) { $callback($this); } $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(); } [$levelBeingCommitted, $this->transactions] = [ $this->transactions, max(0, $this->transactions - 1), ]; $this->transactionsManager?->commit( $this->getName(), $levelBeingCommitted, $this->transactions ); $this->fireConnectionEvent('committed'); } /** * 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.'); } } framework/src/Illuminate/Database/Query/JoinClause.php 0000755 00000007364 15060132304 0017020 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|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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); } } framework/src/Illuminate/Database/Query/JoinLateralClause.php 0000644 00000000143 15060132304 0020306 0 ustar 00 <?php namespace Illuminate\Database\Query; class JoinLateralClause extends JoinClause { // } framework/src/Illuminate/Database/Query/Processors/MariaDbProcessor.php 0000644 00000000161 15060132304 0022306 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; class MariaDbProcessor extends MySqlProcessor { // } framework/src/Illuminate/Database/Query/Processors/Processor.php 0000755 00000005644 15060132304 0021104 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 tables query. * * @param array $results * @return array */ public function processTables($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $result->name, 'schema' => $result->schema ?? null, // PostgreSQL and SQL Server 'size' => isset($result->size) ? (int) $result->size : null, 'comment' => $result->comment ?? null, // MySQL and PostgreSQL 'collation' => $result->collation ?? null, // MySQL only 'engine' => $result->engine ?? null, // MySQL only ]; }, $results); } /** * Process the results of a views query. * * @param array $results * @return array */ public function processViews($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $result->name, 'schema' => $result->schema ?? null, // PostgreSQL and SQL Server 'definition' => $result->definition, ]; }, $results); } /** * Process the results of a types query. * * @param array $results * @return array */ public function processTypes($results) { return $results; } /** * Process the results of a columns query. * * @param array $results * @return array */ public function processColumns($results) { return $results; } /** * Process the results of an indexes query. * * @param array $results * @return array */ public function processIndexes($results) { return $results; } /** * Process the results of a foreign keys query. * * @param array $results * @return array */ public function processForeignKeys($results) { return $results; } } framework/src/Illuminate/Database/Query/Processors/SQLiteProcessor.php 0000644 00000006770 15060132304 0022164 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; class SQLiteProcessor extends Processor { /** * Process the results of a columns query. * * @param array $results * @param string $sql * @return array */ public function processColumns($results, $sql = '') { $hasPrimaryKey = array_sum(array_column($results, 'primary')) === 1; return array_map(function ($result) use ($hasPrimaryKey, $sql) { $result = (object) $result; $type = strtolower($result->type); $collation = preg_match( '/\b'.preg_quote($result->name).'\b[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:default|check|as)\s*(?:\(.*?\))?[^,]*)*collate\s+["\'`]?(\w+)/i', $sql, $matches ) === 1 ? strtolower($matches[1]) : null; $isGenerated = in_array($result->extra, [2, 3]); $expression = $isGenerated && preg_match( '/\b'.preg_quote($result->name).'\b[^,]+\s+as\s+\(((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\)/i', $sql, $matches ) === 1 ? $matches[1] : null; return [ 'name' => $result->name, 'type_name' => strtok($type, '(') ?: '', 'type' => $type, 'collation' => $collation, 'nullable' => (bool) $result->nullable, 'default' => $result->default, 'auto_increment' => $hasPrimaryKey && $result->primary && $type === 'integer', 'comment' => null, 'generation' => $isGenerated ? [ 'type' => match ((int) $result->extra) { 3 => 'stored', 2 => 'virtual', default => null, }, 'expression' => $expression, ] : null, ]; }, $results); } /** * Process the results of an indexes query. * * @param array $results * @return array */ public function processIndexes($results) { $primaryCount = 0; $indexes = array_map(function ($result) use (&$primaryCount) { $result = (object) $result; if ($isPrimary = (bool) $result->primary) { $primaryCount += 1; } return [ 'name' => strtolower($result->name), 'columns' => explode(',', $result->columns), 'type' => null, 'unique' => (bool) $result->unique, 'primary' => $isPrimary, ]; }, $results); if ($primaryCount > 1) { $indexes = array_filter($indexes, fn ($index) => $index['name'] !== 'primary'); } return $indexes; } /** * Process the results of a foreign keys query. * * @param array $results * @return array */ public function processForeignKeys($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => null, 'columns' => explode(',', $result->columns), 'foreign_schema' => null, 'foreign_table' => $result->foreign_table, 'foreign_columns' => explode(',', $result->foreign_columns), 'on_update' => strtolower($result->on_update), 'on_delete' => strtolower($result->on_delete), ]; }, $results); } } framework/src/Illuminate/Database/Query/Processors/MySqlProcessor.php 0000644 00000005017 15060132304 0022061 0 ustar 00 <?php namespace Illuminate\Database\Query\Processors; class MySqlProcessor extends Processor { /** * 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 ?: null, 'generation' => $result->expression ? [ 'type' => match ($result->extra) { 'STORED GENERATED' => 'stored', 'VIRTUAL GENERATED' => 'virtual', default => null, }, 'expression' => $result->expression, ] : null, ]; }, $results); } /** * Process the results of an indexes query. * * @param array $results * @return array */ public function processIndexes($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $name = strtolower($result->name), 'columns' => explode(',', $result->columns), 'type' => strtolower($result->type), 'unique' => (bool) $result->unique, 'primary' => $name === 'primary', ]; }, $results); } /** * Process the results of a foreign keys query. * * @param array $results * @return array */ public function processForeignKeys($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $result->name, 'columns' => explode(',', $result->columns), 'foreign_schema' => $result->foreign_schema, 'foreign_table' => $result->foreign_table, 'foreign_columns' => explode(',', $result->foreign_columns), 'on_update' => strtolower($result->on_update), 'on_delete' => strtolower($result->on_delete), ]; }, $results); } } framework/src/Illuminate/Database/Query/Processors/PostgresProcessor.php 0000755 00000012505 15060132304 0022625 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 types query. * * @param array $results * @return array */ public function processTypes($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $result->name, 'schema' => $result->schema, 'implicit' => (bool) $result->implicit, 'type' => match (strtolower($result->type)) { 'b' => 'base', 'c' => 'composite', 'd' => 'domain', 'e' => 'enum', 'p' => 'pseudo', 'r' => 'range', 'm' => 'multirange', default => null, }, 'category' => match (strtolower($result->category)) { 'a' => 'array', 'b' => 'boolean', 'c' => 'composite', 'd' => 'date_time', 'e' => 'enum', 'g' => 'geometric', 'i' => 'network_address', 'n' => 'numeric', 'p' => 'pseudo', 'r' => 'range', 's' => 'string', 't' => 'timespan', 'u' => 'user_defined', 'v' => 'bit_string', 'x' => 'unknown', 'z' => 'internal_use', default => null, }, ]; }, $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' => $result->name, 'type_name' => $result->type_name, 'type' => $result->type, 'collation' => $result->collation, 'nullable' => (bool) $result->nullable, 'default' => $result->generated ? null : $result->default, 'auto_increment' => $autoincrement, 'comment' => $result->comment, 'generation' => $result->generated ? [ 'type' => match ($result->generated) { 's' => 'stored', default => null, }, 'expression' => $result->default, ] : null, ]; }, $results); } /** * Process the results of an indexes query. * * @param array $results * @return array */ public function processIndexes($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => strtolower($result->name), 'columns' => explode(',', $result->columns), 'type' => strtolower($result->type), 'unique' => (bool) $result->unique, 'primary' => (bool) $result->primary, ]; }, $results); } /** * Process the results of a foreign keys query. * * @param array $results * @return array */ public function processForeignKeys($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $result->name, 'columns' => explode(',', $result->columns), 'foreign_schema' => $result->foreign_schema, 'foreign_table' => $result->foreign_table, 'foreign_columns' => explode(',', $result->foreign_columns), 'on_update' => match (strtolower($result->on_update)) { 'a' => 'no action', 'r' => 'restrict', 'c' => 'cascade', 'n' => 'set null', 'd' => 'set default', default => null, }, 'on_delete' => match (strtolower($result->on_delete)) { 'a' => 'no action', 'r' => 'restrict', 'c' => 'cascade', 'n' => 'set null', 'd' => 'set default', default => null, }, ]; }, $results); } } framework/src/Illuminate/Database/Query/Processors/SqlServerProcessor.php 0000755 00000010326 15060132304 0022744 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 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, 'generation' => $result->expression ? [ 'type' => $result->persisted ? 'stored' : 'virtual', 'expression' => $result->expression, ] : null, ]; }, $results); } /** * Process the results of an indexes query. * * @param array $results * @return array */ public function processIndexes($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => strtolower($result->name), 'columns' => explode(',', $result->columns), 'type' => strtolower($result->type), 'unique' => (bool) $result->unique, 'primary' => (bool) $result->primary, ]; }, $results); } /** * Process the results of a foreign keys query. * * @param array $results * @return array */ public function processForeignKeys($results) { return array_map(function ($result) { $result = (object) $result; return [ 'name' => $result->name, 'columns' => explode(',', $result->columns), 'foreign_schema' => $result->foreign_schema, 'foreign_table' => $result->foreign_table, 'foreign_columns' => explode(',', $result->foreign_columns), 'on_update' => strtolower(str_replace('_', ' ', $result->on_update)), 'on_delete' => strtolower(str_replace('_', ' ', $result->on_delete)), ]; }, $results); } } framework/src/Illuminate/Database/Query/Expression.php 0000755 00000001371 15060132304 0017113 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; } } framework/src/Illuminate/Database/Query/Builder.php 0000755 00000362526 15060132304 0016356 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; use UnitEnum; 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|null */ 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 \Illuminate\Database\Query\Expression|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 maximum number of records to return per group. * * @var array */ public $groupLimit; /** * 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 = []; /** * The callbacks that should be invoked after retrieving data from the database. * * @var array */ protected $afterQueryCallbacks = []; /** * 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|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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 lateral join clause to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @param string $as * @param string $type * @return $this */ public function joinLateral($query, string $as, string $type = 'inner') { [$query, $bindings] = $this->createSub($query); $expression = '('.$query.') as '.$this->grammar->wrapTable($as); $this->addBinding($bindings, 'join'); $this->joins[] = $this->newJoinLateralClause($this, $type, new Expression($expression)); return $this; } /** * Add a lateral left join to the query. * * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @param string $as * @return $this */ public function leftJoinLateral($query, string $as) { return $this->joinLateral($query, $as, 'left'); } /** * Add a left join to the query. * * @param \Illuminate\Contracts\Database\Query\Expression|string $table * @param \Closure|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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|\Illuminate\Contracts\Database\Query\Expression|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); } /** * Get a new join lateral clause. * * @param \Illuminate\Database\Query\Builder $parentQuery * @param string $type * @param string $table * @return \Illuminate\Database\Query\JoinLateralClause */ protected function newJoinLateralClause(self $parentQuery, $type, $table) { return new JoinLateralClause($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 \Illuminate\Contracts\Database\Query\Expression|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 \Illuminate\Contracts\Database\Query\Expression|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 instanceof BackedEnum ? $value->value : $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->getStartDate(), $values->getEndDate()]; } $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 \DateTimeInterface|string|null $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 \DateTimeInterface|string|null $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 \DateTimeInterface|string|null $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 \DateTimeInterface|string|null $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 \DateTimeInterface|string|int|null $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 \DateTimeInterface|string|int|null $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 \DateTimeInterface|string|int|null $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 \DateTimeInterface|string|int|null $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 \DateTimeInterface|string|int|null $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 \DateTimeInterface|string|int|null $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 "where JSON overlaps" clause to the query. * * @param string $column * @param mixed $value * @param string $boolean * @param bool $not * @return $this */ public function whereJsonOverlaps($column, $value, $boolean = 'and', $not = false) { $type = 'JsonOverlaps'; $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 overlaps" clause to the query. * * @param string $column * @param mixed $value * @return $this */ public function orWhereJsonOverlaps($column, $value) { return $this->whereJsonOverlaps($column, $value, 'or'); } /** * Add a "where JSON not overlap" clause to the query. * * @param string $column * @param mixed $value * @param string $boolean * @return $this */ public function whereJsonDoesntOverlap($column, $value, $boolean = 'and') { return $this->whereJsonOverlaps($column, $value, $boolean, true); } /** * Add an "or where JSON not overlap" clause to the query. * * @param string $column * @param mixed $value * @return $this */ public function orWhereJsonDoesntOverlap($column, $value) { return $this->whereJsonDoesntOverlap($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 "where" clause to the query for multiple columns with "and" conditions between them. * * @param string[] $columns * @param mixed $operator * @param mixed $value * @param string $boolean * @return $this */ public function whereAll($columns, $operator = null, $value = null, $boolean = 'and') { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $this->whereNested(function ($query) use ($columns, $operator, $value) { foreach ($columns as $column) { $query->where($column, $operator, $value, 'and'); } }, $boolean); return $this; } /** * Add an "or where" clause to the query for multiple columns with "and" conditions between them. * * @param string[] $columns * @param string $operator * @param mixed $value * @return $this */ public function orWhereAll($columns, $operator = null, $value = null) { return $this->whereAll($columns, $operator, $value, 'or'); } /** * Add an "where" clause to the query for multiple columns with "or" conditions between them. * * @param string[] $columns * @param string $operator * @param mixed $value * @param string $boolean * @return $this */ public function whereAny($columns, $operator = null, $value = null, $boolean = 'and') { [$value, $operator] = $this->prepareValueAndOperator( $value, $operator, func_num_args() === 2 ); $this->whereNested(function ($query) use ($columns, $operator, $value) { foreach ($columns as $column) { $query->where($column, $operator, $value, 'or'); } }, $boolean); return $this; } /** * Add an "or where" clause to the query for multiple columns with "or" conditions between them. * * @param string[] $columns * @param string $operator * @param mixed $value * @return $this */ public function orWhereAny($columns, $operator = null, $value = null) { return $this->whereAny($columns, $operator, $value, '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->getStartDate(), $values->getEndDate()]; } $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; } /** * Add a "group limit" clause to the query. * * @param int $value * @param string $column * @return $this */ public function groupLimit($value, $column) { if ($value >= 0) { $this->groupLimit = compact('value', 'column'); } 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 = []; } /** * Register a closure to be invoked after the query is executed. * * @param \Closure $callback * @return $this */ public function afterQuery(Closure $callback) { $this->afterQueryCallbacks[] = $callback; return $this; } /** * Invoke the "after query" modification callbacks. * * @param mixed $result * @return mixed */ public function applyAfterQueryCallbacks($result) { foreach ($this->afterQueryCallbacks as $afterQueryCallback) { $result = $afterQueryCallback($result) ?: $result; } return $result; } /** * 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 = ['*']) { $items = collect($this->onceWithColumns(Arr::wrap($columns), function () { return $this->processor->processSelect($this, $this->runSelect()); })); return $this->applyAfterQueryCallbacks( isset($this->groupLimit) ? $this->withoutGroupLimitKeys($items) : $items ); } /** * 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 ); } /** * Remove the group limit keys from the results in the collection. * * @param \Illuminate\Support\Collection $items * @return \Illuminate\Support\Collection */ protected function withoutGroupLimitKeys($items) { $keysToRemove = ['laravel_row']; if (is_string($this->groupLimit['column'])) { $column = last(explode('.', $this->groupLimit['column'])); $keysToRemove[] = '@laravel_group := '.$this->grammar->wrap($column); $keysToRemove[] = '@laravel_group := '.$this->grammar->wrap('pivot_'.$column); } $items->each(function ($item) use ($keysToRemove) { foreach ($keysToRemove as $key) { unset($item->$key); } }); return $items; } /** * 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, $total = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $total = value($total) ?? $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 ); }))->map(function ($item) { return $this->applyAfterQueryCallbacks(collect([$item]))->first(); })->reject(fn ($item) => is_null($item)); } /** * 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 $this->applyAfterQueryCallbacks( 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) ); } /** * Insert new records into the table using a subquery while ignoring errors. * * @param array $columns * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query * @return int */ public function insertOrIgnoreUsing(array $columns, $query) { $this->applyBeforeQueryCallbacks(); [$sql, $bindings] = $this->createSub($query); return $this->connection->affectingStatement( $this->grammar->compileInsertOrIgnoreUsing($this, $columns, $sql), $this->cleanBindings($bindings) ); } /** * Update records in the database. * * @param array $values * @return int */ public function update(array $values) { $this->applyBeforeQueryCallbacks(); $values = collect($values)->map(function ($value) { if (! $value instanceof Builder) { return ['value' => $value, 'bindings' => $value]; } [$query, $bindings] = $this->parseSub($value); return ['value' => new Expression("({$query})"), 'bindings' => fn () => $bindings]; }); $sql = $this->grammar->compileUpdate($this, $values->map(fn ($value) => $value['value'])->all()); return $this->connection->update($sql, $this->cleanBindings( $this->grammar->prepareBindingsForUpdate($this->bindings, $values->map(fn ($value) => $value['bindings'])->all()) )); } /** * 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|callable $values * @return bool */ public function updateOrInsert(array $attributes, array|callable $values = []) { $exists = $this->where($attributes)->exists(); if ($values instanceof Closure) { $values = $values($exists); } if (! $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 query builder instances that are used in the union of the query. * * @return \Illuminate\Support\Collection */ protected function getUnionBuilders() { return isset($this->unions) ? collect($this->unions)->pluck('query') : collect(); } /** * 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) { if ($value instanceof UnitEnum) { return $value instanceof BackedEnum ? $value->value : $value->name; } return $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. * * @param mixed ...$args * @return $this */ public function dump(...$args) { dump( $this->toSql(), $this->getBindings(), ...$args, ); 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); } } framework/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php 0000755 00000035163 15060132304 0021770 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinLateralClause; 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 a row number clause. * * @param string $partition * @param string $orders * @return string */ protected function compileRowNumber($partition, $orders) { if (empty($orders)) { $orders = 'order by (select 0)'; } return parent::compileRowNumber($partition, $orders); } /** * 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 a "lateral join" clause. * * @param \Illuminate\Database\Query\JoinLateralClause $join * @param string $expression * @return string */ public function compileJoinLateral(JoinLateralClause $join, string $expression): string { $type = $join->type == 'left' ? 'outer' : 'cross'; return trim("{$type} apply {$expression}"); } /** * 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; } } framework/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php 0000755 00000052422 15060132304 0021645 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinLateralClause; 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 ignore statement using a subquery into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @param string $sql * @return string */ public function compileInsertOrIgnoreUsing(Builder $query, array $columns, string $sql) { return $this->compileInsertUsing($query, $columns, $sql).' 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; } /** * Compile a "lateral join" clause. * * @param \Illuminate\Database\Query\JoinLateralClause $join * @param string $expression * @return string */ public function compileJoinLateral(JoinLateralClause $join, string $expression): string { return trim("{$join->type} join lateral {$expression} on true"); } /** * 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; } } framework/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php 0000755 00000027027 15060132304 0021203 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" statement into SQL. * * @param string $column * @param mixed $value * @return string */ protected function compileJsonContains($column, $value) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return 'exists (select 1 from json_each('.$field.$path.') where '.$this->wrap('json_each.value').' is '.$value.')'; } /** * Prepare the binding for a "JSON contains" statement. * * @param mixed $binding * @return mixed */ public function prepareBindingForJsonContains($binding) { return $binding; } /** * 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 a group limit clause. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileGroupLimit(Builder $query) { $version = $query->getConnection()->getServerVersion(); if (version_compare($version, '3.25.0') >= 0) { return parent::compileGroupLimit($query); } $query->groupLimit = null; return $this->compileSelect($query); } /** * 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 an insert ignore statement using a subquery into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @param string $sql * @return string */ public function compileInsertOrIgnoreUsing(Builder $query, array $columns, string $sql) { return Str::replaceFirst('insert', 'insert or ignore', $this->compileInsertUsing($query, $columns, $sql)); } /** * 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 = ?' => [$this->getTablePrefix().$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.')'; } } framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php 0000755 00000034176 15060132304 0021112 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinLateralClause; 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 a group limit clause. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileGroupLimit(Builder $query) { return $this->useLegacyGroupLimit($query) ? $this->compileLegacyGroupLimit($query) : parent::compileGroupLimit($query); } /** * Determine whether to use a legacy group limit clause for MySQL < 8.0. * * @param \Illuminate\Database\Query\Builder $query * @return bool */ public function useLegacyGroupLimit(Builder $query) { $version = $query->getConnection()->getServerVersion(); return ! $query->getConnection()->isMaria() && version_compare($version, '8.0.11') < 0; } /** * Compile a group limit clause for MySQL < 8.0. * * Derived from https://softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileLegacyGroupLimit(Builder $query) { $limit = (int) $query->groupLimit['value']; $offset = $query->offset; if (isset($offset)) { $offset = (int) $offset; $limit += $offset; $query->offset = null; } $column = last(explode('.', $query->groupLimit['column'])); $column = $this->wrap($column); $partition = ', @laravel_row := if(@laravel_group = '.$column.', @laravel_row + 1, 1) as `laravel_row`'; $partition .= ', @laravel_group := '.$column; $orders = (array) $query->orders; array_unshift($orders, [ 'column' => $query->groupLimit['column'], 'direction' => 'asc', ]); $query->orders = $orders; $components = $this->compileComponents($query); $sql = $this->concatenate($components); $from = '(select @laravel_row := 0, @laravel_group := 0) as `laravel_vars`, ('.$sql.') as `laravel_table`'; $sql = 'select `laravel_table`.*'.$partition.' from '.$from.' having `laravel_row` <= '.$limit; if (isset($offset)) { $sql .= ' and `laravel_row` > '.$offset; } return $sql.' order by `laravel_row`'; } /** * 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 an insert ignore statement using a subquery into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @param string $sql * @return string */ public function compileInsertOrIgnoreUsing(Builder $query, array $columns, string $sql) { return Str::replaceFirst('insert', 'insert ignore', $this->compileInsertUsing($query, $columns, $sql)); } /** * 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 overlaps" statement into SQL. * * @param string $column * @param string $value * @return string */ protected function compileJsonOverlaps($column, $value) { [$field, $path] = $this->wrapJsonFieldAndPath($column); return 'json_overlaps('.$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; } /** * Compile a "lateral join" clause. * * @param \Illuminate\Database\Query\JoinLateralClause $join * @param string $expression * @return string */ public function compileJoinLateral(JoinLateralClause $join, string $expression): string { return trim("{$join->type} join lateral {$expression} on true"); } /** * 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.')'; } } framework/src/Illuminate/Database/Query/Grammars/Grammar.php 0000755 00000124310 15060132304 0020112 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\Database\Query\JoinLateralClause; 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 a "group limit" is in place, we will need to compile the SQL to use a // different syntax. This primarily supports limits on eager loads using // Eloquent. We'll also set the columns if they have not been defined. if (isset($query->groupLimit)) { if (is_null($query->columns)) { $query->columns = ['*']; } return $this->compileGroupLimit($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.')'; if ($join instanceof JoinLateralClause) { return $this->compileJoinLateral($join, $tableAndNestedJoins); } return trim("{$join->type} join {$tableAndNestedJoins} {$this->compileWheres($join)}"); })->implode(' '); } /** * Compile a "lateral join" clause. * * @param \Illuminate\Database\Query\JoinLateralClause $join * @param string $expression * @return string * * @throws \RuntimeException */ public function compileJoinLateral(JoinLateralClause $join, string $expression): string { throw new RuntimeException('This database engine does not support lateral joins.'); } /** * 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.'); } /** * Compile a "where JSON overlaps" clause. * * @param \Illuminate\Database\Query\Builder $query * @param array $where * @return string */ protected function whereJsonOverlaps(Builder $query, $where) { $not = $where['not'] ? 'not ' : ''; return $not.$this->compileJsonOverlaps( $where['column'], $this->parameter($where['value']) ); } /** * Compile a "JSON overlaps" statement into SQL. * * @param string $column * @param string $value * @return string * * @throws \RuntimeException */ protected function compileJsonOverlaps($column, $value) { throw new RuntimeException('This database engine does not support JSON overlaps 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 a group limit clause. * * @param \Illuminate\Database\Query\Builder $query * @return string */ protected function compileGroupLimit(Builder $query) { $selectBindings = array_merge($query->getRawBindings()['select'], $query->getRawBindings()['order']); $query->setBindings($selectBindings, 'select'); $query->setBindings([], 'order'); $limit = (int) $query->groupLimit['value']; $offset = $query->offset; if (isset($offset)) { $offset = (int) $offset; $limit += $offset; $query->offset = null; } $components = $this->compileComponents($query); $components['columns'] .= $this->compileRowNumber( $query->groupLimit['column'], $components['orders'] ?? '' ); unset($components['orders']); $table = $this->wrap('laravel_table'); $row = $this->wrap('laravel_row'); $sql = $this->concatenate($components); $sql = 'select * from ('.$sql.') as '.$table.' where '.$row.' <= '.$limit; if (isset($offset)) { $sql .= ' and '.$row.' > '.$offset; } return $sql.' order by '.$row; } /** * Compile a row number clause. * * @param string $partition * @param string $orders * @return string */ protected function compileRowNumber($partition, $orders) { $over = trim('partition by '.$this->wrap($partition).' '.$orders); return ', row_number() over ('.$over.') as '.$this->wrap('laravel_row'); } /** * 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 insert ignore statement using a subquery into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $columns * @param string $sql * @return string * * @throws \RuntimeException */ public function compileInsertOrIgnoreUsing(Builder $query, array $columns, string $sql) { throw new RuntimeException('This database engine does not support inserting while ignoring errors.'); } /** * 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']); $values = Arr::flatten(array_map(fn ($value) => value($value), $values)); 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; } } framework/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php 0000755 00000001607 15060132304 0021335 0 ustar 00 <?php namespace Illuminate\Database\Query\Grammars; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinLateralClause; use RuntimeException; class MariaDbGrammar extends MySqlGrammar { /** * Compile a "lateral join" clause. * * @param \Illuminate\Database\Query\JoinLateralClause $join * @param string $expression * @return string * * @throws \RuntimeException */ public function compileJoinLateral(JoinLateralClause $join, string $expression): string { throw new RuntimeException('This database engine does not support lateral joins.'); } /** * Determine whether to use a legacy group limit clause for MySQL < 8.0. * * @param \Illuminate\Database\Query\Builder $query * @return bool */ public function useLegacyGroupLimit(Builder $query) { return false; } } framework/src/Illuminate/Database/Query/IndexHint.php 0000755 00000000777 15060132304 0016657 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; } } framework/src/Illuminate/Database/SQLiteConnection.php 0000755 00000007516 15060132304 0017037 0 ustar 00 <?php namespace Illuminate\Database; use Exception; 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; } $schemaBuilder = $this->getSchemaBuilder(); try { $enableForeignKeyConstraints ? $schemaBuilder->enableForeignKeyConstraints() : $schemaBuilder->disableForeignKeyConstraints(); } catch (QueryException $e) { if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { throw $e; } } } /** * 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 database connection foreign key constraints configuration option. * * @return bool|null */ protected function getForeignKeyConstraintsConfigurationValue() { return $this->getConfig('foreign_key_constraints'); } } framework/src/Illuminate/Database/DetectsConcurrencyErrors.php 0000644 00000002132 15060132304 0020643 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', ]); } } framework/src/Illuminate/Database/Events/DatabaseRefreshed.php 0000644 00000000733 15060132304 0020445 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 ) { // } } framework/src/Illuminate/Database/Events/TransactionBeginning.php 0000644 00000000154 15060132304 0021214 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionBeginning extends ConnectionEvent { // } framework/src/Illuminate/Database/Events/ModelsPruned.php 0000644 00000001031 15060132304 0017502 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; } } framework/src/Illuminate/Database/Events/TransactionCommitted.php 0000644 00000000154 15060132304 0021241 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionCommitted extends ConnectionEvent { // } framework/src/Illuminate/Database/Events/QueryExecuted.php 0000644 00000002162 15060132304 0017703 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(); } } framework/src/Illuminate/Database/Events/TransactionCommitting.php 0000644 00000000155 15060132304 0021427 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionCommitting extends ConnectionEvent { // } framework/src/Illuminate/Database/Events/SchemaDumped.php 0000644 00000001375 15060132304 0017453 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; } } framework/src/Illuminate/Database/Events/StatementPrepared.php 0000644 00000001173 15060132304 0020537 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; } } framework/src/Illuminate/Database/Events/SchemaLoaded.php 0000644 00000001375 15060132304 0017425 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; } } framework/src/Illuminate/Database/Events/MigrationStarted.php 0000644 00000000147 15060132304 0020370 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationStarted extends MigrationEvent { // } framework/src/Illuminate/Database/Events/DatabaseBusy.php 0000644 00000001103 15060132304 0017450 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; } } framework/src/Illuminate/Database/Events/MigrationEvent.php 0000644 00000001450 15060132304 0020041 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; } } framework/src/Illuminate/Database/Events/MigrationEnded.php 0000644 00000000145 15060132304 0017777 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationEnded extends MigrationEvent { // } framework/src/Illuminate/Database/Events/MigrationsEvent.php 0000644 00000000770 15060132304 0020230 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; } } framework/src/Illuminate/Database/Events/NoPendingMigrations.php 0000644 00000000574 15060132304 0021032 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; } } framework/src/Illuminate/Database/Events/ConnectionEvent.php 0000644 00000001145 15060132304 0020210 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(); } } framework/src/Illuminate/Database/Events/ConnectionEstablished.php 0000644 00000000155 15060132304 0021356 0 ustar 00 <?php namespace Illuminate\Database\Events; class ConnectionEstablished extends ConnectionEvent { // } framework/src/Illuminate/Database/Events/ModelPruningFinished.php 0000644 00000000641 15060132304 0021164 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; } } framework/src/Illuminate/Database/Events/TransactionRolledBack.php 0000644 00000000155 15060132304 0021317 0 ustar 00 <?php namespace Illuminate\Database\Events; class TransactionRolledBack extends ConnectionEvent { // } framework/src/Illuminate/Database/Events/MigrationsEnded.php 0000644 00000000147 15060132304 0020164 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationsEnded extends MigrationsEvent { // } framework/src/Illuminate/Database/Events/ModelPruningStarting.php 0000644 00000000644 15060132304 0021231 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; } } framework/src/Illuminate/Database/Events/MigrationsStarted.php 0000644 00000000151 15060132304 0020546 0 ustar 00 <?php namespace Illuminate\Database\Events; class MigrationsStarted extends MigrationsEvent { // } framework/src/Illuminate/Database/LICENSE.md 0000644 00000002063 15060132304 0014536 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. framework/src/Illuminate/Database/UniqueConstraintViolationException.php 0000644 00000000153 15060132304 0022720 0 ustar 00 <?php namespace Illuminate\Database; class UniqueConstraintViolationException extends QueryException { } framework/src/Illuminate/Database/DetectsLostConnections.php 0000644 00000010057 15060132304 0020305 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', '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', 'SQLSTATE[HY000]: General error: 3989', 'went away', 'No such file or directory', 'server is shutting down', 'failed to connect to', ]); } } framework/src/Illuminate/Database/PostgresConnection.php 0000755 00000005443 15060132304 0017501 0 ustar 00 <?php namespace Illuminate\Database; use Exception; 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; } } framework/src/Illuminate/Database/ConnectionResolver.php 0000755 00000003720 15060132304 0017470 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; } } framework/src/Illuminate/Database/LazyLoadingViolationException.php 0000644 00000001401 15060132304 0021617 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 void */ 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; } } framework/src/Illuminate/Database/MariaDbConnection.php 0000755 00000004660 15060132304 0017172 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Database\Query\Grammars\MariaDbGrammar as QueryGrammar; use Illuminate\Database\Query\Processors\MariaDbProcessor; use Illuminate\Database\Schema\Grammars\MariaDbGrammar as SchemaGrammar; use Illuminate\Database\Schema\MariaDbBuilder; use Illuminate\Database\Schema\MariaDbSchemaState; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; class MariaDbConnection extends MySqlConnection { /** * Determine if the connected database is a MariaDB database. * * @return bool */ public function isMaria() { return true; } /** * Get the server version for the connection. * * @return string */ public function getServerVersion(): string { return Str::between(parent::getServerVersion(), '5.5.5-', '-MariaDB'); } /** * Get the default query grammar instance. * * @return \Illuminate\Database\Query\Grammars\MariaDbGrammar */ protected function getDefaultQueryGrammar() { ($grammar = new QueryGrammar)->setConnection($this); return $this->withTablePrefix($grammar); } /** * Get a schema builder instance for the connection. * * @return \Illuminate\Database\Schema\MariaDbBuilder */ public function getSchemaBuilder() { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } return new MariaDbBuilder($this); } /** * Get the default schema grammar instance. * * @return \Illuminate\Database\Schema\Grammars\MariaDbGrammar */ 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\MariaDbSchemaState */ public function getSchemaState(?Filesystem $files = null, ?callable $processFactory = null) { return new MariaDbSchemaState($this, $files, $processFactory); } /** * Get the default post processor instance. * * @return \Illuminate\Database\Query\Processors\MariaDbProcessor */ protected function getDefaultPostProcessor() { return new MariaDbProcessor; } } framework/src/Illuminate/Database/ConnectionInterface.php 0000755 00000010250 15060132304 0017563 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 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); /** * 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(); } framework/src/Illuminate/Database/MultipleRecordsFoundException.php 0000755 00000001351 15060132304 0021635 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; } } framework/src/Illuminate/Database/DatabaseManager.php 0000755 00000031007 15060132304 0016645 0 ustar 00 <?php namespace Illuminate\Database; 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; /** * 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) { $name = $name ?: $this->getDefaultConnection(); [$database, $type] = $this->parseConnectionName($name); // 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 ); $this->dispatchConnectionEstablishedEvent($this->connections[$name]); } return $this->connections[$name]; } /** * Get a database connection instance from the given configuration. * * @param string $name * @param array $config * @param bool $force * @return \Illuminate\Database\ConnectionInterface */ public function connectUsing(string $name, array $config, bool $force = false) { if ($force) { $this->purge($name); } if (isset($this->connections[$name])) { throw new RuntimeException("Cannot establish connection [$name] because another connection with that name already exists."); } $connection = $this->configure( $this->factory->make($config, $name), null ); $this->dispatchConnectionEstablishedEvent($connection); return tap($connection, fn ($connection) => $this->connections[$name] = $connection); } /** * 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); return $connection; } /** * Dispatch the ConnectionEstablished event if the event dispatcher is available. * * @param \Illuminate\Database\Connection $connection * @return void */ protected function dispatchConnectionEstablishedEvent(Connection $connection) { if (! $this->app->bound('events')) { return; } $this->app['events']->dispatch( new ConnectionEstablished($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; } /** * 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 supported drivers. * * @return string[] */ public function supportedDrivers() { return ['mysql', 'mariadb', '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); } } framework/src/Illuminate/Database/Capsule/Manager.php 0000755 00000012401 15060132304 0016611 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); } } framework/src/Illuminate/Database/MigrationServiceProvider.php 0000755 00000014115 15060132304 0020634 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) { $migrations = $app['config']['database.migrations']; $table = is_array($migrations) ? ($migrations['table'] ?? null) : $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, function ($app) { return new FreshCommand($app['migrator']); }); } /** * 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)); } } framework/src/Illuminate/Database/composer.json 0000644 00000003171 15060132304 0015655 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.2", "ext-pdo": "*", "brick/math": "^0.9.3|^0.10.2|^0.11|^0.12", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Database\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "ext-filter": "Required to use the Postgres database driver.", "fakerphp/faker": "Required to use the eloquent factory builder (^1.21).", "illuminate/console": "Required to use the database commands (^11.0).", "illuminate/events": "Required to use the observers with Eloquent (^11.0).", "illuminate/filesystem": "Required to use the migrations (^11.0).", "illuminate/pagination": "Required to paginate the result set (^11.0).", "symfony/finder": "Required to use Eloquent model factories (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Database/SQLiteDatabaseDoesNotExistException.php 0000644 00000001065 15060132304 0022622 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; } } framework/src/Illuminate/Database/DatabaseTransactionsManager.php 0000755 00000017377 15060132304 0021254 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Support\Collection; class DatabaseTransactionsManager { /** * All of the committed transactions. * * @var \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord> */ protected $committedTransactions; /** * All of the pending transactions. * * @var \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord> */ protected $pendingTransactions; /** * The current transaction. * * @var array */ protected $currentTransaction = []; /** * Create a new database transactions manager instance. * * @return void */ public function __construct() { $this->committedTransactions = new Collection; $this->pendingTransactions = new Collection; } /** * Start a new database transaction. * * @param string $connection * @param int $level * @return void */ public function begin($connection, $level) { $this->pendingTransactions->push( $newTransaction = new DatabaseTransactionRecord( $connection, $level, $this->currentTransaction[$connection] ?? null ) ); $this->currentTransaction[$connection] = $newTransaction; } /** * Commit the root database transaction and execute callbacks. * * @param string $connection * @param int $levelBeingCommitted * @param int $newTransactionLevel * @return array */ public function commit($connection, $levelBeingCommitted, $newTransactionLevel) { $this->stageTransactions($connection, $levelBeingCommitted); if (isset($this->currentTransaction[$connection])) { $this->currentTransaction[$connection] = $this->currentTransaction[$connection]->parent; } if (! $this->afterCommitCallbacksShouldBeExecuted($newTransactionLevel) && $newTransactionLevel !== 0) { return []; } // This method is only called when the root database transaction is committed so there // shouldn't be any pending transactions, but going to clear them here anyways just // in case. This method could be refactored to receive a level in the future too. $this->pendingTransactions = $this->pendingTransactions->reject( fn ($transaction) => $transaction->connection === $connection && $transaction->level >= $levelBeingCommitted )->values(); [$forThisConnection, $forOtherConnections] = $this->committedTransactions->partition( fn ($transaction) => $transaction->connection == $connection ); $this->committedTransactions = $forOtherConnections->values(); $forThisConnection->map->executeCallbacks(); return $forThisConnection; } /** * Move relevant pending transactions to a committed state. * * @param string $connection * @param int $levelBeingCommitted * @return void */ public function stageTransactions($connection, $levelBeingCommitted) { $this->committedTransactions = $this->committedTransactions->merge( $this->pendingTransactions->filter( fn ($transaction) => $transaction->connection === $connection && $transaction->level >= $levelBeingCommitted ) ); $this->pendingTransactions = $this->pendingTransactions->reject( fn ($transaction) => $transaction->connection === $connection && $transaction->level >= $levelBeingCommitted ); } /** * Rollback the active database transaction. * * @param string $connection * @param int $newTransactionLevel * @return void */ public function rollback($connection, $newTransactionLevel) { if ($newTransactionLevel === 0) { $this->removeAllTransactionsForConnection($connection); } else { $this->pendingTransactions = $this->pendingTransactions->reject( fn ($transaction) => $transaction->connection == $connection && $transaction->level > $newTransactionLevel )->values(); if ($this->currentTransaction) { do { $this->removeCommittedTransactionsThatAreChildrenOf($this->currentTransaction[$connection]); $this->currentTransaction[$connection] = $this->currentTransaction[$connection]->parent; } while ( isset($this->currentTransaction[$connection]) && $this->currentTransaction[$connection]->level > $newTransactionLevel ); } } } /** * Remove all pending, completed, and current transactions for the given connection name. * * @param string $connection * @return void */ protected function removeAllTransactionsForConnection($connection) { $this->currentTransaction[$connection] = null; $this->pendingTransactions = $this->pendingTransactions->reject( fn ($transaction) => $transaction->connection == $connection )->values(); $this->committedTransactions = $this->committedTransactions->reject( fn ($transaction) => $transaction->connection == $connection )->values(); } /** * Remove all transactions that are children of the given transaction. * * @param \Illuminate\Database\DatabaseTransactionRecord $transaction * @return void */ protected function removeCommittedTransactionsThatAreChildrenOf(DatabaseTransactionRecord $transaction) { [$removedTransactions, $this->committedTransactions] = $this->committedTransactions->partition( fn ($committed) => $committed->connection == $transaction->connection && $committed->parent === $transaction ); // There may be multiple deeply nested transactions that have already committed that we // also need to remove. We will recurse down the children of all removed transaction // instances until there are no more deeply nested child transactions for removal. $removedTransactions->each( fn ($transaction) => $this->removeCommittedTransactionsThatAreChildrenOf($transaction) ); } /** * 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->pendingTransactions; } /** * 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 of the pending transactions. * * @return \Illuminate\Support\Collection */ public function getPendingTransactions() { return $this->pendingTransactions; } /** * Get all of the committed transactions. * * @return \Illuminate\Support\Collection */ public function getCommittedTransactions() { return $this->committedTransactions; } } framework/src/Illuminate/Database/QueryException.php 0000644 00000004124 15060132304 0016627 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; } } framework/src/Illuminate/Database/SqlServerConnection.php 0000755 00000007572 15060132304 0017626 0 ustar 00 <?php namespace Illuminate\Database; use Closure; use Exception; 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; } } framework/src/Illuminate/Database/MultipleColumnsSelectedException.php 0000644 00000000211 15060132304 0022320 0 ustar 00 <?php namespace Illuminate\Database; use RuntimeException; class MultipleColumnsSelectedException extends RuntimeException { // } framework/src/Illuminate/Database/ConnectionResolverInterface.php 0000755 00000001077 15060132304 0021314 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); } framework/src/Illuminate/Database/Seeder.php 0000755 00000011207 15060132304 0015055 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); 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(); } } framework/src/Illuminate/Database/ClassMorphViolationException.php 0000644 00000001015 15060132304 0021456 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; } } framework/src/Illuminate/Database/ConfigurationUrlParser.php 0000644 00000000300 15060132304 0020302 0 ustar 00 <?php namespace Illuminate\Database; use Illuminate\Support\ConfigurationUrlParser as BaseConfigurationUrlParser; class ConfigurationUrlParser extends BaseConfigurationUrlParser { // } framework/src/Illuminate/Database/Console/WipeCommand.php 0000644 00000005453 15060132304 0017456 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Console\Prohibitable; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'db:wipe')] class WipeCommand extends Command { use ConfirmableTrait, Prohibitable; /** * 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->isProhibited() || ! $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'], ]; } } framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php 0000644 00000006647 15060132304 0020503 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'], ]; } } framework/src/Illuminate/Database/Console/Seeds/stubs/seeder.stub 0000644 00000000411 15060132304 0021100 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 { // } } framework/src/Illuminate/Database/Console/Seeds/SeederMakeCommand.php 0000644 00000003642 15060132304 0021620 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\\'; } } framework/src/Illuminate/Database/Console/Seeds/WithoutModelEvents.php 0000644 00000000626 15060132304 0022124 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); } } framework/src/Illuminate/Database/Console/DumpCommand.php 0000644 00000005605 15060132304 0017456 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) { $migrations = Config::get('database.migrations', 'migrations'); $migrationTable = is_array($migrations) ? ($migrations['table'] ?? 'migrations') : $migrations; return $connection->getSchemaState() ->withMigrationTable($connection->getTablePrefix().$migrationTable) ->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)); }); } } framework/src/Illuminate/Database/Console/Migrations/ResetCommand.php 0000755 00000005243 15060132304 0021750 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\ConfirmableTrait; use Illuminate\Console\Prohibitable; use Illuminate\Database\Migrations\Migrator; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'migrate:reset')] class ResetCommand extends BaseCommand { use ConfirmableTrait, Prohibitable; /** * 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->isProhibited() || ! $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'], ]; } } framework/src/Illuminate/Database/Console/Migrations/BaseCommand.php 0000755 00000002652 15060132304 0021541 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'; } } framework/src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php 0000644 00000010725 15060132304 0023052 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; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'make:migration')] 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'], ]; } } framework/src/Illuminate/Database/Console/Migrations/StatusCommand.php 0000644 00000010513 15060132304 0022142 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Database\Migrations\Migrator; use Illuminate\Support\Collection; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'migrate:status')] 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') !== false, 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') !== false) { $this->components->info('No pending migrations'); } else { $this->components->info('No migrations found'); } if ($this->option('pending') && $migrations->some(fn ($m) => str($m[1])->contains('Pending'))) { return $this->option('pending'); } }); } /** * 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_OPTIONAL, 'Only list pending migrations', false], ['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'], ]; } } framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php 0000755 00000024406 15060132304 0022260 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 RuntimeException; use Symfony\Component\Console\Attribute\AsCommand; use Throwable; use function Laravel\Prompts\confirm; #[AsCommand(name: 'migrate')] 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} {--graceful : Return a successful exit code even if an error occurs}'; /** * 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; } try { $this->runMigrations(); } catch (Throwable $e) { if ($this->option('graceful')) { $this->components->warn($e->getMessage()); return 0; } throw $e; } return 0; } /** * Run the pending migrations. * * @return void */ protected function runMigrations() { $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, ]); } }); } /** * 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 && in_array($connection->getDriverName(), ['mysql', 'mariadb'])) { return $this->createMissingMysqlDatabase($connection); } return false; } catch (Throwable) { return false; } }); } /** * Create a missing SQLite database. * * @param string $path * @return bool * * @throws \RuntimeException */ protected function createMissingSqliteDatabase($path) { if ($this->option('force')) { return touch($path); } if ($this->option('no-interaction')) { return false; } $this->components->warn('The SQLite database configured for this application does not exist: '.$path); if (! confirm('Would you like to create it?', default: true)) { $this->components->info('Operation cancelled. No database was created.'); throw new RuntimeException('Database was not created. Aborting migration.'); } return touch($path); } /** * Create a missing MySQL database. * * @return bool * * @throws \RuntimeException */ 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: true)) { $this->components->info('Operation cancelled. No database was created.'); throw new RuntimeException('Database was not created. Aborting migration.'); } } 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'); } } framework/src/Illuminate/Database/Console/Migrations/TableGuesser.php 0000644 00000001613 15060132304 0021746 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]; } } } } framework/src/Illuminate/Database/Console/Migrations/RefreshCommand.php 0000755 00000011741 15060132304 0022264 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Console\Prohibitable; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\DatabaseRefreshed; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'migrate:refresh')] class RefreshCommand extends Command { use ConfirmableTrait, Prohibitable; /** * 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->isProhibited() || ! $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'], ]; } } framework/src/Illuminate/Database/Console/Migrations/InstallCommand.php 0000755 00000003215 15060132304 0022271 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\Command; use Illuminate\Database\Migrations\MigrationRepositoryInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'migrate:install')] 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'], ]; } } framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php 0000644 00000011047 15060132304 0021731 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\Command; use Illuminate\Console\ConfirmableTrait; use Illuminate\Console\Prohibitable; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\DatabaseRefreshed; use Illuminate\Database\Migrations\Migrator; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand(name: 'migrate:fresh')] class FreshCommand extends Command { use ConfirmableTrait, Prohibitable; /** * 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'; /** * The migrator instance. * * @var \Illuminate\Database\Migrations\Migrator */ protected $migrator; /** * Create a new fresh 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->isProhibited() || ! $this->confirmToProceed()) { return 1; } $database = $this->input->getOption('database'); $this->migrator->usingConnection($database, function () use ($database) { if ($this->migrator->repositoryExists()) { $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'], ]; } } framework/src/Illuminate/Database/Console/Migrations/RollbackCommand.php 0000755 00000005135 15060132304 0022417 0 ustar 00 <?php namespace Illuminate\Database\Console\Migrations; use Illuminate\Console\ConfirmableTrait; use Illuminate\Database\Migrations\Migrator; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; #[AsCommand('migrate:rollback')] 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'], ]; } } framework/src/Illuminate/Database/Console/TableCommand.php 0000644 00000017202 15060132304 0017574 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Schema\Builder; use Illuminate\Support\Arr; use Illuminate\Support\Number; 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) { $connection = $connections->connection($this->input->getOption('database')); $schema = $connection->getSchemaBuilder(); $tables = $schema->getTables(); $tableName = $this->argument('table') ?: select( 'Which table would you like to inspect?', array_column($tables, 'name') ); $table = Arr::first($tables, fn ($table) => $table['name'] === $tableName); if (! $table) { $this->components->warn("Table [{$table}] doesn't exist."); return 1; } $tableName = $this->withoutTablePrefix($connection, $table['name']); $columns = $this->columns($schema, $tableName); $indexes = $this->indexes($schema, $tableName); $foreignKeys = $this->foreignKeys($schema, $tableName); $data = [ 'table' => [ 'name' => $table['name'], 'columns' => count($columns), 'size' => $table['size'], ], 'columns' => $columns, 'indexes' => $indexes, 'foreign_keys' => $foreignKeys, ]; $this->display($data); return 0; } /** * Get the information regarding the table's columns. * * @param \Illuminate\Database\Schema\Builder $schema * @param string $table * @return \Illuminate\Support\Collection */ protected function columns(Builder $schema, string $table) { return collect($schema->getColumns($table))->map(fn ($column) => [ 'column' => $column['name'], 'attributes' => $this->getAttributesForColumn($column), 'default' => $column['default'], 'type' => $column['type'], ]); } /** * Get the attributes for a table column. * * @param array $column * @return \Illuminate\Support\Collection */ protected function getAttributesForColumn($column) { return collect([ $column['type_name'], $column['auto_increment'] ? 'autoincrement' : null, $column['nullable'] ? 'nullable' : null, $column['collation'], ])->filter(); } /** * Get the information regarding the table's indexes. * * @param \Illuminate\Database\Schema\Builder $schema * @param string $table * @return \Illuminate\Support\Collection */ protected function indexes(Builder $schema, string $table) { return collect($schema->getIndexes($table))->map(fn ($index) => [ 'name' => $index['name'], 'columns' => collect($index['columns']), 'attributes' => $this->getAttributesForIndex($index), ]); } /** * Get the attributes for a table index. * * @param array $index * @return \Illuminate\Support\Collection */ protected function getAttributesForIndex($index) { return collect([ $index['type'], count($index['columns']) > 1 ? 'compound' : null, $index['unique'] && ! $index['primary'] ? 'unique' : null, $index['primary'] ? 'primary' : null, ])->filter(); } /** * Get the information regarding the table's foreign keys. * * @param \Illuminate\Database\Schema\Builder $schema * @param string $table * @return \Illuminate\Support\Collection */ protected function foreignKeys(Builder $schema, string $table) { return collect($schema->getForeignKeys($table))->map(fn ($foreignKey) => [ 'name' => $foreignKey['name'], 'columns' => collect($foreignKey['columns']), 'foreign_schema' => $foreignKey['foreign_schema'], 'foreign_table' => $foreignKey['foreign_table'], 'foreign_columns' => collect($foreignKey['foreign_columns']), 'on_update' => $foreignKey['on_update'], 'on_delete' => $foreignKey['on_delete'], ]); } /** * 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::fileSize($size, 2)); } $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(', ').'</>', (! is_null($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['columns']->implode(', ').' references '.$foreignKey['foreign_columns']->implode(', ').' on '.$foreignKey['foreign_table'].'</>', $foreignKey['on_update'].' / '.$foreignKey['on_delete'], ); }); $this->newLine(); } } } framework/src/Illuminate/Database/Console/ShowModelCommand.php 0000644 00000041441 15060132304 0020450 0 ustar 00 <?php namespace Illuminate\Database\Console; use BackedEnum; 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() { $class = $this->qualifyModel($this->argument('model')); try { $model = $this->laravel->make($class); $class = get_class($model); } catch (BindingResolutionException $e) { $this->components->error($e->getMessage()); return 1; } 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->getEvents($model), $this->getObservers($model), ); return 0; } /** * 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->getSchemaBuilder(); $table = $model->getTable(); $columns = $schema->getColumns($table); $indexes = $schema->getIndexes($table); return collect($columns) ->map(fn ($column) => [ 'name' => $column['name'], 'type' => $column['type'], 'increments' => $column['auto_increment'], 'nullable' => $column['nullable'], 'default' => $this->getColumnDefault($column, $model), 'unique' => $this->columnIsUnique($column['name'], $indexes), 'fillable' => $model->isFillable($column['name']), 'hidden' => $this->attributeIsHidden($column['name'], $model), 'appended' => null, 'cast' => $this->getCastType($column['name'], $model), ]) ->merge($this->getVirtualAttributes($model, $columns)); } /** * Get the virtual (non-column) attributes for the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @param array $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)->contains('name', $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 Events that the model dispatches. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Support\Collection */ protected function getEvents($model) { return collect($model->dispatchesEvents()) ->map(fn (string $class, string $event) => [ 'event' => $event, 'class' => $class, ])->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 $events * @param \Illuminate\Support\Collection $observers * @return void */ protected function display($class, $database, $table, $policy, $attributes, $relations, $events, $observers) { $this->option('json') ? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers) : $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $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 $events * @param \Illuminate\Support\Collection $observers * @return void */ protected function displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers) { $this->output->writeln( collect([ 'class' => $class, 'database' => $database, 'table' => $table, 'policy' => $policy, 'attributes' => $attributes, 'relations' => $relations, 'events' => $events, '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 $events * @param \Illuminate\Support\Collection $observers * @return void */ protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $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>Events</>'); if ($events->count()) { foreach ($events as $event) { $this->components->twoColumnDetail( sprintf('%s', $event['event']), sprintf('%s', $event['class']), ); } } $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 default value for the given column. * * @param array $column * @param \Illuminate\Database\Eloquent\Model $model * @return mixed|null */ protected function getColumnDefault($column, $model) { $attributeDefault = $model->getAttributes()[$column['name']] ?? null; return match (true) { $attributeDefault instanceof BackedEnum => $attributeDefault->value, $attributeDefault instanceof UnitEnum => $attributeDefault->name, default => $attributeDefault ?? $column['default'], }; } /** * 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 array $indexes * @return bool */ protected function columnIsUnique($column, $indexes) { return collect($indexes)->contains( fn ($index) => count($index['columns']) === 1 && $index['columns'][0] === $column && $index['unique'] ); } /** * 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; } } framework/src/Illuminate/Database/Console/Factories/stubs/factory.stub 0000644 00000000655 15060132304 0022166 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 [ // ]; } } framework/src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php 0000644 00000007170 15060132304 0022674 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'], ]; } } framework/src/Illuminate/Database/Console/ShowCommand.php 0000644 00000021210 15060132304 0017457 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Schema\Builder; use Illuminate\Support\Arr; use Illuminate\Support\Number; 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 </>} {--types : Show the user defined types}'; /** * 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) { $connection = $connections->connection($database = $this->input->getOption('database')); $schema = $connection->getSchemaBuilder(); $data = [ 'platform' => [ 'config' => $this->getConfigFromDatabase($database), 'name' => $this->getConnectionName($connection, $database), 'version' => $connection->getServerVersion(), 'open_connections' => $this->getConnectionCount($connection), ], 'tables' => $this->tables($connection, $schema), ]; if ($this->option('views')) { $data['views'] = $this->views($connection, $schema); } if ($this->option('types')) { $data['types'] = $this->types($connection, $schema); } $this->display($data); return 0; } /** * Get information regarding the tables within the database. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\Schema\Builder $schema * @return \Illuminate\Support\Collection */ protected function tables(ConnectionInterface $connection, Builder $schema) { return collect($schema->getTables())->map(fn ($table) => [ 'table' => $table['name'], 'schema' => $table['schema'], 'size' => $table['size'], 'rows' => $this->option('counts') ? $connection->table($table['name'])->count() : null, 'engine' => $table['engine'], 'collation' => $table['collation'], 'comment' => $table['comment'], ]); } /** * Get information regarding the views within the database. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\Schema\Builder $schema * @return \Illuminate\Support\Collection */ protected function views(ConnectionInterface $connection, Builder $schema) { return collect($schema->getViews()) ->reject(fn ($view) => str($view['name'])->startsWith(['pg_catalog', 'information_schema', 'spt_'])) ->map(fn ($view) => [ 'view' => $view['name'], 'schema' => $view['schema'], 'rows' => $connection->table($view->getName())->count(), ]); } /** * Get information regarding the user-defined types within the database. * * @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\Schema\Builder $schema * @return \Illuminate\Support\Collection */ protected function types(ConnectionInterface $connection, Builder $schema) { return collect($schema->getTypes()) ->map(fn ($type) => [ 'name' => $type['name'], 'schema' => $type['schema'], 'type' => $type['type'], 'category' => $type['category'], ]); } /** * 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; $types = $data['types'] ?? null; $this->newLine(); $this->components->twoColumnDetail('<fg=green;options=bold>'.$platform['name'].'</>', $platform['version']); $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::fileSize($tableSizeSum, 2)); } $this->newLine(); if ($tables->isNotEmpty()) { $hasSchema = ! is_null($tables->first()['schema']); $this->components->twoColumnDetail( ($hasSchema ? '<fg=green;options=bold>Schema</> <fg=gray;options=bold>/</> ' : '').'<fg=green;options=bold>Table</>', 'Size'.($this->option('counts') ? ' <fg=gray;options=bold>/</> <fg=yellow;options=bold>Rows</>' : '') ); $tables->each(function ($table) { if ($tableSize = $table['size']) { $tableSize = Number::fileSize($tableSize, 2); } $this->components->twoColumnDetail( ($table['schema'] ? $table['schema'].' <fg=gray;options=bold>/</> ' : '').$table['table'].($this->output->isVerbose() ? ' <fg=gray>'.$table['engine'].'</>' : null), ($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()) { $hasSchema = ! is_null($views->first()['schema']); $this->components->twoColumnDetail( ($hasSchema ? '<fg=green;options=bold>Schema</> <fg=gray;options=bold>/</> ' : '').'<fg=green;options=bold>View</>', '<fg=green;options=bold>Rows</>' ); $views->each(fn ($view) => $this->components->twoColumnDetail( ($view['schema'] ? $view['schema'].' <fg=gray;options=bold>/</> ' : '').$view['view'], Number::format($view['rows']) )); $this->newLine(); } if ($types && $types->isNotEmpty()) { $hasSchema = ! is_null($types->first()['schema']); $this->components->twoColumnDetail( ($hasSchema ? '<fg=green;options=bold>Schema</> <fg=gray;options=bold>/</> ' : '').'<fg=green;options=bold>Type</>', '<fg=green;options=bold>Type</> <fg=gray;options=bold>/</> <fg=green;options=bold>Category</>' ); $types->each(fn ($type) => $this->components->twoColumnDetail( ($type['schema'] ? $type['schema'].' <fg=gray;options=bold>/</> ' : '').$type['name'], $type['type'].' <fg=gray;options=bold>/</> '.$type['category'] )); $this->newLine(); } } } framework/src/Illuminate/Database/Console/DatabaseInspectionCommand.php 0000644 00000005605 15060132304 0022311 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Console\Command; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\MariaDbConnection; use Illuminate\Database\MySqlConnection; use Illuminate\Database\PostgresConnection; use Illuminate\Database\SQLiteConnection; use Illuminate\Database\SqlServerConnection; use Illuminate\Support\Arr; abstract class DatabaseInspectionCommand extends Command { /** * Get a human-readable name for the given connection. * * @param \Illuminate\Database\ConnectionInterface $connection * @param string $database * @return string */ protected function getConnectionName(ConnectionInterface $connection, $database) { return match (true) { $connection instanceof MySqlConnection && $connection->isMaria() => 'MariaDB', $connection instanceof MySqlConnection => 'MySQL', $connection instanceof MariaDbConnection => 'MariaDB', $connection instanceof PostgresConnection => 'PostgreSQL', $connection instanceof SQLiteConnection => 'SQLite', $connection instanceof SqlServerConnection => 'SQL Server', default => $database, }; } /** * 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']); } /** * 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 str_starts_with($table, $prefix) ? substr($table, strlen($prefix)) : $table; } } framework/src/Illuminate/Database/Console/MonitorCommand.php 0000644 00000007455 15060132304 0020205 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Events\DatabaseBusy; 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 */ public function __construct(ConnectionResolverInterface $connection, Dispatcher $events) { parent::__construct(); $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'] ) ); }); } } framework/src/Illuminate/Database/Console/DbCommand.php 0000644 00000015663 15060132304 0017103 0 ustar 00 <?php namespace Illuminate\Database\Console; use Illuminate\Console\Command; use Illuminate\Support\ConfigurationUrlParser; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Process\Process; use UnexpectedValueException; #[AsCommand(name: 'db')] 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', 'mariadb' => '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 MariaDB CLI. * * @param array $connection * @return array */ protected function getMariaDbArguments(array $connection) { return $this->getMysqlArguments($connection); } /** * 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'] : ''), ], 'trust_server_certificate' => ['-C'], ], $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)); } } framework/src/Illuminate/Database/Console/PruneCommand.php 0000644 00000013611 15060132304 0017636 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\Console\Attribute\AsCommand; use Symfony\Component\Finder\Finder; #[AsCommand(name: 'model:prune')] 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} {--path=* : Absolute path(s) to directories where models are located} {--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(Finder::create()->in($this->getPath())->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 path where models are located. * * @return string[]|string */ protected function getPath() { if (! empty($path = $this->option('path'))) { return collect($path)->map(function ($path) { return base_path($path); })->all(); } 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."); } } } framework/src/Illuminate/Database/MySqlConnection.php 0000755 00000006245 15060132304 0016741 0 ustar 00 <?php namespace Illuminate\Database; use Exception; 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 Illuminate\Support\Str; 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 server version for the connection. * * @return string */ public function getServerVersion(): string { return str_contains($version = parent::getServerVersion(), 'MariaDB') ? Str::between($version, '5.5.5-', '-MariaDB') : $version; } /** * 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; } } framework/src/Illuminate/Database/Schema/SqliteSchemaState.php 0000644 00000005340 15060132304 0020427 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'], ]; } } framework/src/Illuminate/Database/Schema/SqlServerBuilder.php 0000644 00000011163 15060132304 0020301 0 ustar 00 <?php namespace Illuminate\Database\Schema; use InvalidArgumentException; 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) ); } /** * Determine if the given table exists. * * @param string $table * @return bool */ public function hasTable($table) { [$schema, $table] = $this->parseSchemaAndTable($table); $schema ??= $this->getDefaultSchema(); $table = $this->connection->getTablePrefix().$table; foreach ($this->getTables() as $value) { if (strtolower($table) === strtolower($value['name']) && strtolower($schema) === strtolower($value['schema'])) { return true; } } return false; } /** * Determine if the given view exists. * * @param string $view * @return bool */ public function hasView($view) { [$schema, $view] = $this->parseSchemaAndTable($view); $schema ??= $this->getDefaultSchema(); $view = $this->connection->getTablePrefix().$view; foreach ($this->getViews() as $value) { if (strtolower($view) === strtolower($value['name']) && strtolower($schema) === strtolower($value['schema'])) { return true; } } return false; } /** * 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()); } /** * Get the columns for a given table. * * @param string $table * @return array */ public function getColumns($table) { [$schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; $results = $this->connection->selectFromWriteConnection( $this->grammar->compileColumns($schema, $table) ); return $this->connection->getPostProcessor()->processColumns($results); } /** * Get the indexes for a given table. * * @param string $table * @return array */ public function getIndexes($table) { [$schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processIndexes( $this->connection->selectFromWriteConnection($this->grammar->compileIndexes($schema, $table)) ); } /** * Get the foreign keys for a given table. * * @param string $table * @return array */ public function getForeignKeys($table) { [$schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processForeignKeys( $this->connection->selectFromWriteConnection($this->grammar->compileForeignKeys($schema, $table)) ); } /** * Get the default schema for the connection. * * @return string */ protected function getDefaultSchema() { return $this->connection->scalar($this->grammar->compileDefaultSchema()); } /** * Parse the database object reference and extract the schema and table. * * @param string $reference * @return array */ protected function parseSchemaAndTable($reference) { $parts = array_pad(explode('.', $reference, 2), -2, null); if (str_contains($parts[1], '.')) { $database = $parts[0]; throw new InvalidArgumentException("Using three-part reference is not supported, you may use `Schema::connection('$database')` instead."); } return $parts; } } framework/src/Illuminate/Database/Schema/SQLiteBuilder.php 0000644 00000005635 15060132304 0017523 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Database\QueryException; 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; } /** * Get the tables for the database. * * @param bool $withSize * @return array */ public function getTables($withSize = true) { if ($withSize) { try { $withSize = $this->connection->scalar($this->grammar->compileDbstatExists()); } catch (QueryException $e) { $withSize = false; } } return $this->connection->getPostProcessor()->processTables( $this->connection->selectFromWriteConnection($this->grammar->compileTables($withSize)) ); } /** * 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)), $this->connection->scalar($this->grammar->compileSqlCreateStatement($table)) ); } /** * 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()); } /** * Empty the database file. * * @return void */ public function refreshDatabaseFile() { file_put_contents($this->connection->getDatabaseName(), ''); } } framework/src/Illuminate/Database/Schema/Blueprint.php 0000755 00000136523 15060132304 0017023 0 ustar 00 <?php namespace Illuminate\Database\Schema; 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\Schema\Grammars\MySqlGrammar; 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) { if ($command->shouldBeSkipped) { continue; } $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) { // } /** * 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($connection, $grammar); $this->addFluentCommands($connection, $grammar); } /** * Add the index commands fluently specified on columns. * * @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @return void */ protected function addFluentIndexes(Connection $connection, Grammar $grammar) { foreach ($this->columns as $column) { foreach (['primary', 'unique', 'index', 'fulltext', 'fullText', 'spatialIndex'] as $index) { // If the column is supposed to be changed to an auto increment column and // the specified index is primary, there is no need to add a command on // MySQL, as it will be handled during the column definition instead. if ($index === 'primary' && $column->autoIncrement && $column->change && $grammar instanceof MySqlGrammar) { continue 2; } // 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) { 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'); } /** * Specify the storage engine that should be used for the table. * * @param string $engine * @return void */ public function engine($engine) { $this->engine = $engine; } /** * Specify that the InnoDB storage engine should be used for the table (MySQL only). * * @return void */ public function innoDb() { $this->engine('InnoDB'); } /** * Specify the character set that should be used for the table. * * @param string $charset * @return void */ public function charset($charset) { $this->charset = $charset; } /** * Specify the collation that should be used for the table. * * @param string $collation * @return void */ public function collation($collation) { $this->collation = $collation; } /** * 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 $precision * @return \Illuminate\Database\Schema\ColumnDefinition */ public function float($column, $precision = 53) { return $this->addColumn('float', $column, compact('precision')); } /** * Create a new double column on the table. * * @param string $column * @return \Illuminate\Database\Schema\ColumnDefinition */ public function double($column) { return $this->addColumn('double', $column); } /** * Create a new decimal column on the table. * * @param string $column * @param int $total * @param int $places * @return \Illuminate\Database\Schema\ColumnDefinition */ public function decimal($column, $total = 8, $places = 2) { return $this->addColumn('decimal', $column, compact('total', 'places')); } /** * 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 * @param int|null $length * @param bool $fixed * @return \Illuminate\Database\Schema\ColumnDefinition */ public function binary($column, $length = null, $fixed = false) { return $this->addColumn('binary', $column, compact('length', 'fixed')); } /** * 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 * @param string|null $subtype * @param int $srid * @return \Illuminate\Database\Schema\ColumnDefinition */ public function geometry($column, $subtype = null, $srid = 0) { return $this->addColumn('geometry', $column, compact('subtype', 'srid')); } /** * Create a new geography column on the table. * * @param string $column * @param string|null $subtype * @param int $srid * @return \Illuminate\Database\Schema\ColumnDefinition */ public function geography($column, $subtype = null, $srid = 4326) { return $this->addColumn('geography', $column, compact('subtype', 'srid')); } /** * 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) { $table = str_contains($this->table, '.') ? substr_replace($this->table, '.'.$this->prefix, strrpos($this->table, '.'), 1) : $this->prefix.$this->table; $index = strtolower($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; }); } } framework/src/Illuminate/Database/Schema/ForeignKeyDefinition.php 0000644 00000004007 15060132304 0021116 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 updates should have "no action". * * @return $this */ public function noActionOnUpdate() { return $this->onUpdate('no action'); } /** * 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'); } } framework/src/Illuminate/Database/Schema/Builder.php 0000755 00000035557 15060132304 0016452 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Closure; use Illuminate\Container\Container; use Illuminate\Database\Connection; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use LogicException; class Builder { use Macroable; /** * 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'; /** * 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'); } /** * 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; foreach ($this->getTables(false) as $value) { if (strtolower($table) === strtolower($value['name'])) { return true; } } return false; } /** * Determine if the given view exists. * * @param string $view * @return bool */ public function hasView($view) { $view = $this->connection->getTablePrefix().$view; foreach ($this->getViews() as $value) { if (strtolower($view) === strtolower($value['name'])) { return true; } } return false; } /** * Get the tables that belong to the database. * * @return array */ public function getTables() { return $this->connection->getPostProcessor()->processTables( $this->connection->selectFromWriteConnection($this->grammar->compileTables()) ); } /** * Get the names of the tables that belong to the database. * * @return array */ public function getTableListing() { return array_column($this->getTables(), 'name'); } /** * Get the views that belong to the database. * * @return array */ public function getViews() { return $this->connection->getPostProcessor()->processViews( $this->connection->selectFromWriteConnection($this->grammar->compileViews()) ); } /** * Get the user-defined types that belong to the database. * * @return array */ public function getTypes() { throw new LogicException('This database driver does not support user-defined types.'); } /** * 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) { $columns = $this->getColumns($table); foreach ($columns as $value) { if (strtolower($value['name']) === strtolower($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)) ); } /** * Get the indexes for a given table. * * @param string $table * @return array */ public function getIndexes($table) { $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processIndexes( $this->connection->selectFromWriteConnection($this->grammar->compileIndexes($table)) ); } /** * Get the names of the indexes for a given table. * * @param string $table * @return array */ public function getIndexListing($table) { return array_column($this->getIndexes($table), 'name'); } /** * Determine if the given table has a given index. * * @param string $table * @param string|array $index * @param string|null $type * @return bool */ public function hasIndex($table, $index, $type = null) { $type = is_null($type) ? $type : strtolower($type); foreach ($this->getIndexes($table) as $value) { $typeMatches = is_null($type) || ($type === 'primary' && $value['primary']) || ($type === 'unique' && $value['unique']) || $type === $value['type']; if (($value['name'] === $index || $value['columns'] === $index) && $typeMatches) { return true; } } return false; } /** * Get the foreign keys for a given table. * * @param string $table * @return array */ public function getForeignKeys($table) { $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processForeignKeys( $this->connection->selectFromWriteConnection($this->grammar->compileForeignKeys($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.'); } /** * 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; } } framework/src/Illuminate/Database/Schema/ColumnDefinition.php 0000644 00000004764 15060132304 0020323 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 * @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|\Illuminate\Contracts\Database\Query\Expression $expression = null) Create a SQL compliant identity column (PostgreSQL) * @method $this index(bool|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(bool $value = true) Add a primary index * @method $this fulltext(bool|string $indexName = null) Add a fulltext index * @method $this spatialIndex(bool|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|\Illuminate\Contracts\Database\Query\Expression $expression) Create a stored generated column (MySQL/PostgreSQL/SQLite) * @method $this type(string $type) Specify a type for the column * @method $this unique(bool|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|\Illuminate\Contracts\Database\Query\Expression $expression) Create a virtual generated column (MySQL/PostgreSQL/SQLite) */ class ColumnDefinition extends Fluent { // } framework/src/Illuminate/Database/Schema/PostgresSchemaState.php 0000644 00000005104 15060132304 0020772 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'], ]; } } framework/src/Illuminate/Database/Schema/MySqlSchemaState.php 0000644 00000012461 15060132304 0020235 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; } } framework/src/Illuminate/Database/Schema/MariaDbSchemaState.php 0000644 00000000745 15060132304 0020471 0 ustar 00 <?php namespace Illuminate\Database\Schema; class MariaDbSchemaState extends MySqlSchemaState { /** * Get the base dump command arguments for MariaDB 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'; return $command.' "${:LARAVEL_LOAD_DATABASE}"'; } } framework/src/Illuminate/Database/Schema/SchemaState.php 0000644 00000005277 15060132304 0017256 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; } } framework/src/Illuminate/Database/Schema/MySqlBuilder.php 0000755 00000007143 15060132304 0017426 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) ); } /** * Get the tables for the database. * * @return array */ public function getTables() { return $this->connection->getPostProcessor()->processTables( $this->connection->selectFromWriteConnection( $this->grammar->compileTables($this->connection->getDatabaseName()) ) ); } /** * Get the views for the database. * * @return array */ public function getViews() { return $this->connection->getPostProcessor()->processViews( $this->connection->selectFromWriteConnection( $this->grammar->compileViews($this->connection->getDatabaseName()) ) ); } /** * 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); } /** * Get the indexes for a given table. * * @param string $table * @return array */ public function getIndexes($table) { $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processIndexes( $this->connection->selectFromWriteConnection( $this->grammar->compileIndexes($this->connection->getDatabaseName(), $table) ) ); } /** * Get the foreign keys for a given table. * * @param string $table * @return array */ public function getForeignKeys($table) { $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processForeignKeys( $this->connection->selectFromWriteConnection( $this->grammar->compileForeignKeys($this->connection->getDatabaseName(), $table) ) ); } /** * Drop all tables from the database. * * @return void */ public function dropAllTables() { $tables = array_column($this->getTables(), 'name'); 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 = array_column($this->getViews(), 'name'); if (empty($views)) { return; } $this->connection->statement( $this->grammar->compileDropAllViews($views) ); } } framework/src/Illuminate/Database/Schema/ForeignIdColumnDefinition.php 0000644 00000003032 15060132304 0022075 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); } } framework/src/Illuminate/Database/Schema/PostgresBuilder.php 0000755 00000017401 15060132304 0020165 0 ustar 00 <?php namespace Illuminate\Database\Schema; use Illuminate\Database\Concerns\ParsesSearchPath; use InvalidArgumentException; 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) { [$schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; foreach ($this->getTables() as $value) { if (strtolower($table) === strtolower($value['name']) && strtolower($schema) === strtolower($value['schema'])) { return true; } } return false; } /** * Determine if the given view exists. * * @param string $view * @return bool */ public function hasView($view) { [$schema, $view] = $this->parseSchemaAndTable($view); $view = $this->connection->getTablePrefix().$view; foreach ($this->getViews() as $value) { if (strtolower($view) === strtolower($value['name']) && strtolower($schema) === strtolower($value['schema'])) { return true; } } return false; } /** * Get the user-defined types that belong to the database. * * @return array */ public function getTypes() { return $this->connection->getPostProcessor()->processTypes( $this->connection->selectFromWriteConnection($this->grammar->compileTypes()) ); } /** * Drop all tables from the database. * * @return void */ public function dropAllTables() { $tables = []; $excludedTables = $this->grammar->escapeNames( $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys'] ); $schemas = $this->grammar->escapeNames($this->getSchemas()); foreach ($this->getTables() as $table) { $qualifiedName = $table['schema'].'.'.$table['name']; if (empty(array_intersect($this->grammar->escapeNames([$table['name'], $qualifiedName]), $excludedTables)) && in_array($this->grammar->escapeNames([$table['schema']])[0], $schemas)) { $tables[] = $qualifiedName; } } if (empty($tables)) { return; } $this->connection->statement( $this->grammar->compileDropAllTables($tables) ); } /** * Drop all views from the database. * * @return void */ public function dropAllViews() { $views = []; $schemas = $this->grammar->escapeNames($this->getSchemas()); foreach ($this->getViews() as $view) { if (in_array($this->grammar->escapeNames([$view['schema']])[0], $schemas)) { $views[] = $view['schema'].'.'.$view['name']; } } if (empty($views)) { return; } $this->connection->statement( $this->grammar->compileDropAllViews($views) ); } /** * Drop all types from the database. * * @return void */ public function dropAllTypes() { $types = []; $domains = []; $schemas = $this->grammar->escapeNames($this->getSchemas()); foreach ($this->getTypes() as $type) { if (! $type['implicit'] && in_array($this->grammar->escapeNames([$type['schema']])[0], $schemas)) { if ($type['type'] === 'domain') { $domains[] = $type['schema'].'.'.$type['name']; } else { $types[] = $type['schema'].'.'.$type['name']; } } } if (! empty($types)) { $this->connection->statement($this->grammar->compileDropAllTypes($types)); } if (! empty($domains)) { $this->connection->statement($this->grammar->compileDropAllDomains($domains)); } } /** * Get the columns for a given table. * * @param string $table * @return array */ public function getColumns($table) { [$schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; $results = $this->connection->selectFromWriteConnection( $this->grammar->compileColumns($schema, $table) ); return $this->connection->getPostProcessor()->processColumns($results); } /** * Get the indexes for a given table. * * @param string $table * @return array */ public function getIndexes($table) { [$schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processIndexes( $this->connection->selectFromWriteConnection($this->grammar->compileIndexes($schema, $table)) ); } /** * Get the foreign keys for a given table. * * @param string $table * @return array */ public function getForeignKeys($table) { [$schema, $table] = $this->parseSchemaAndTable($table); $table = $this->connection->getTablePrefix().$table; return $this->connection->getPostProcessor()->processForeignKeys( $this->connection->selectFromWriteConnection($this->grammar->compileForeignKeys($schema, $table)) ); } /** * Get the schemas for the connection. * * @return array */ protected function getSchemas() { return $this->parseSearchPath( $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ?: 'public' ); } /** * Parse the database object reference and extract the schema and table. * * @param string $reference * @return array */ protected function parseSchemaAndTable($reference) { $parts = explode('.', $reference); if (count($parts) > 2) { $database = $parts[0]; throw new InvalidArgumentException("Using three-part reference is not supported, you may use `Schema::connection('$database')` instead."); } // 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 = $this->getSchemas()[0]; if (count($parts) === 2) { $schema = $parts[0]; array_shift($parts); } return [$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)); } } framework/src/Illuminate/Database/Schema/MariaDbBuilder.php 0000755 00000000143 15060132304 0017651 0 ustar 00 <?php namespace Illuminate\Database\Schema; class MariaDbBuilder extends MySqlBuilder { // } framework/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php 0000755 00000075673 15060132304 0022075 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 query to determine the name of the default schema. * * @return string */ public function compileDefaultSchema() { return 'select schema_name()'; } /** * 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 the tables. * * @return string */ public function compileTables() { return 'select t.name as name, schema_name(t.schema_id) as [schema], sum(u.total_pages) * 8 * 1024 as size ' .'from sys.tables as t ' .'join sys.partitions as p on p.object_id = t.object_id ' .'join sys.allocation_units as u on u.container_id = p.hobt_id ' .'group by t.name, t.schema_id ' .'order by t.name'; } /** * Compile the query to determine the views. * * @return string */ public function compileViews() { return 'select name, schema_name(v.schema_id) as [schema], definition from sys.views as v ' .'inner join sys.sql_modules as m on v.object_id = m.object_id ' .'order by name'; } /** * Compile the query to determine the columns. * * @param string $schema * @param string $table * @return string */ public function compileColumns($schema, $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, ' .'com.definition as [expression], is_persisted as [persisted], ' .'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' " .'left join sys.computed_columns as com on col.column_id = com.column_id and col.object_id = com.object_id ' ."where obj.type in ('U', 'V') and obj.name = %s and scm.name = %s " .'order by col.column_id', $this->quoteString($table), $schema ? $this->quoteString($schema) : 'schema_name()', ); } /** * Compile the query to determine the indexes. * * @param string $schema * @param string $table * @return string */ public function compileIndexes($schema, $table) { return sprintf( "select idx.name as name, string_agg(col.name, ',') within group (order by idxcol.key_ordinal) as columns, " .'idx.type_desc as [type], idx.is_unique as [unique], idx.is_primary_key as [primary] ' .'from sys.indexes as idx ' .'join sys.tables as tbl on idx.object_id = tbl.object_id ' .'join sys.schemas as scm on tbl.schema_id = scm.schema_id ' .'join sys.index_columns as idxcol on idx.object_id = idxcol.object_id and idx.index_id = idxcol.index_id ' .'join sys.columns as col on idxcol.object_id = col.object_id and idxcol.column_id = col.column_id ' .'where tbl.name = %s and scm.name = %s ' .'group by idx.name, idx.type_desc, idx.is_unique, idx.is_primary_key', $this->quoteString($table), $schema ? $this->quoteString($schema) : 'schema_name()', ); } /** * Compile the query to determine the foreign keys. * * @param string $schema * @param string $table * @return string */ public function compileForeignKeys($schema, $table) { return sprintf( 'select fk.name as name, ' ."string_agg(lc.name, ',') within group (order by fkc.constraint_column_id) as columns, " .'fs.name as foreign_schema, ft.name as foreign_table, ' ."string_agg(fc.name, ',') within group (order by fkc.constraint_column_id) as foreign_columns, " .'fk.update_referential_action_desc as on_update, ' .'fk.delete_referential_action_desc as on_delete ' .'from sys.foreign_keys as fk ' .'join sys.foreign_key_columns as fkc on fkc.constraint_object_id = fk.object_id ' .'join sys.tables as lt on lt.object_id = fk.parent_object_id ' .'join sys.schemas as ls on lt.schema_id = ls.schema_id ' .'join sys.columns as lc on fkc.parent_object_id = lc.object_id and fkc.parent_column_id = lc.column_id ' .'join sys.tables as ft on ft.object_id = fk.referenced_object_id ' .'join sys.schemas as fs on ft.schema_id = fs.schema_id ' .'join sys.columns as fc on fkc.referenced_object_id = fc.object_id and fkc.referenced_column_id = fc.column_id ' .'where lt.name = %s and ls.name = %s ' .'group by fk.name, fs.name, ft.name, fk.update_referential_action_desc, fk.delete_referential_action_desc', $this->quoteString($table), $schema ? $this->quoteString($schema) : 'schema_name()', ); } /** * 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 sprintf("sp_rename %s, %s, N'COLUMN'", $this->quoteString($this->wrapTable($blueprint).'.'.$this->wrap($command->from)), $this->wrap($command->to) ); } /** * 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) { $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 object_id(%s, \'U\') is not null drop table %s', $this->quoteString($this->wrapTable($blueprint)), $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)."'"; $table = $this->wrapTable($blueprint); $tableName = $this->quoteString($this->wrapTable($blueprint)); $sql = "DECLARE @sql NVARCHAR(MAX) = '';"; $sql .= "SELECT @sql += 'ALTER TABLE $table DROP CONSTRAINT ' + OBJECT_NAME([default_object_id]) + ';' "; $sql .= 'FROM sys.columns '; $sql .= "WHERE [object_id] = OBJECT_ID($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) { return sprintf('sp_rename %s, %s', $this->quoteString($this->wrapTable($blueprint)), $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 %s, %s, N'INDEX'", $this->quoteString($this->wrapTable($blueprint).'.'.$this->wrap($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;"; } /** * 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) { if ($column->precision) { return "float({$column->precision})"; } return 'float'; } /** * 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 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) { if ($column->length) { return $column->fixed ? "binary({$column->length})" : "varbinary({$column->length})"; } 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 */ protected function typeGeometry(Fluent $column) { return 'geometry'; } /** * Create the column definition for a spatial Geography type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeGeography(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 ({$this->getValue($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 $this->hasCommand($blueprint, 'primary') ? ' identity' : ' 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'"; } } framework/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php 0000755 00000106527 15060132304 0021746 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 the tables. * * @return string */ public function compileTables() { return 'select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, ' ."obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n " ."where c.relkind in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema') " .'order by c.relname'; } /** * Compile the query to determine the views. * * @return string */ public function compileViews() { return "select viewname as name, schemaname as schema, definition from pg_views where schemaname not in ('pg_catalog', 'information_schema') order by viewname"; } /** * Compile the query to determine the user-defined types. * * @return string */ public function compileTypes() { return 'select t.typname as name, n.nspname as schema, t.typtype as type, t.typcategory as category, ' ."((t.typinput = 'array_in'::regproc and t.typoutput = 'array_out'::regproc) or t.typtype = 'm') as implicit " .'from pg_type t join pg_namespace n on n.oid = t.typnamespace ' .'left join pg_class c on c.oid = t.typrelid ' .'left join pg_type el on el.oid = t.typelem ' .'left join pg_class ce on ce.oid = el.typrelid ' ."where ((t.typrelid = 0 and (ce.relkind = 'c' or ce.relkind is null)) or c.relkind = 'c') " ."and not exists (select 1 from pg_depend d where d.objid in (t.oid, t.typelem) and d.deptype = 'e') " ."and n.nspname not in ('pg_catalog', 'information_schema')"; } /** * Compile the query to determine the columns. * * @param string $schema * @param string $table * @return string */ public function compileColumns($schema, $table) { return sprintf( 'select 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, ' .(version_compare($this->connection?->getServerVersion(), '12.0', '<') ? "'' as generated, " : 'a.attgenerated as generated, ') .'col_description(c.oid, a.attnum) 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 the query to determine the indexes. * * @param string $schema * @param string $table * @return string */ public function compileIndexes($schema, $table) { return sprintf( "select ic.relname as name, string_agg(a.attname, ',' order by indseq.ord) as columns, " .'am.amname as "type", i.indisunique as "unique", i.indisprimary as "primary" ' .'from pg_index i ' .'join pg_class tc on tc.oid = i.indrelid ' .'join pg_namespace tn on tn.oid = tc.relnamespace ' .'join pg_class ic on ic.oid = i.indexrelid ' .'join pg_am am on am.oid = ic.relam ' .'join lateral unnest(i.indkey) with ordinality as indseq(num, ord) on true ' .'left join pg_attribute a on a.attrelid = i.indrelid and a.attnum = indseq.num ' .'where tc.relname = %s and tn.nspname = %s ' .'group by ic.relname, am.amname, i.indisunique, i.indisprimary', $this->quoteString($table), $this->quoteString($schema) ); } /** * Compile the query to determine the foreign keys. * * @param string $schema * @param string $table * @return string */ public function compileForeignKeys($schema, $table) { return sprintf( 'select c.conname as name, ' ."string_agg(la.attname, ',' order by conseq.ord) as columns, " .'fn.nspname as foreign_schema, fc.relname as foreign_table, ' ."string_agg(fa.attname, ',' order by conseq.ord) as foreign_columns, " .'c.confupdtype as on_update, c.confdeltype as on_delete ' .'from pg_constraint c ' .'join pg_class tc on c.conrelid = tc.oid ' .'join pg_namespace tn on tn.oid = tc.relnamespace ' .'join pg_class fc on c.confrelid = fc.oid ' .'join pg_namespace fn on fn.oid = fc.relnamespace ' .'join lateral unnest(c.conkey) with ordinality as conseq(num, ord) on true ' .'join pg_attribute la on la.attrelid = c.conrelid and la.attnum = conseq.num ' .'join pg_attribute fa on fa.attrelid = c.confrelid and fa.attnum = c.confkey[conseq.ord] ' ."where c.contype = 'f' and tc.relname = %s and tn.nspname = %s " .'group by c.conname, fn.nspname, fc.relname, c.confupdtype, c.confdeltype', $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'))) { $table = last(explode('.', $blueprint->getTable())); return 'alter sequence '.$blueprint->getPrefix().$table.'_'.$command->column->name.'_seq restart with '.$value; } } /** * 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) { $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 drop all domains. * * @param array $domains * @return string */ public function compileDropAllDomains($domains) { return 'drop domain '.implode(',', $this->escapeNames($domains)).' cascade'; } /** * 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) { $table = last(explode('.', $blueprint->getTable())); $index = $this->wrap("{$blueprint->getPrefix()}{$table}_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) && ! $column->change ? '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) && ! $column->change ? '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) && ! $column->change ? 'smallserial' : 'smallint'; } /** * Create the column definition for a float type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeFloat(Fluent $column) { if ($column->precision) { return "float({$column->precision})"; } return 'float'; } /** * 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) { if ($column->subtype) { return sprintf('geometry(%s%s)', strtolower($column->subtype), $column->srid ? ','.$column->srid : '' ); } return 'geometry'; } /** * Create the column definition for a spatial Geography type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeGeography(Fluent $column) { if ($column->subtype) { return sprintf('geography(%s%s)', strtolower($column->subtype), $column->srid ? ','.$column->srid : '' ); } return 'geography'; } /** * 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) { if (! $column->autoIncrement || ! is_null($column->generatedAs)) { return is_null($column->default) ? 'drop default' : 'set default '.$this->getDefaultValue($column->default); } return null; } 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 && ! $this->hasCommand($blueprint, 'primary') && (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 ({$this->getValue($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 ({$this->getValue($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 = $column->autoIncrement && is_null($sql) ? [] : ['drop identity if exists']; if (! is_null($sql)) { $changes[] = 'add '.$sql; } return $changes; } return $sql; } } framework/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php 0000644 00000100124 15060132304 0021261 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\Database\Schema\ColumnDefinition; use Illuminate\Database\Schema\ForeignKeyDefinition; use Illuminate\Database\Schema\IndexDefinition; 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', 'Collate', 'VirtualAs', 'StoredAs']; /** * The columns available as serials. * * @var string[] */ protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; /** * Compile the query to determine the SQL text that describes the given object. * * @param string $name * @param string $type * @return string */ public function compileSqlCreateStatement($name, $type = 'table') { return sprintf('select "sql" from sqlite_master where type = %s and name = %s', $this->quoteString($type), $this->quoteString(str_replace('.', '__', $name)) ); } /** * Compile the query to determine if the dbstat table is available. * * @return string */ public function compileDbstatExists() { return "select exists (select 1 from pragma_compile_options where compile_options = 'ENABLE_DBSTAT_VTAB') as enabled"; } /** * Compile the query to determine the tables. * * @param bool $withSize * @return string */ public function compileTables($withSize = false) { return $withSize ? 'select m.tbl_name as name, sum(s.pgsize) as size from sqlite_master as m ' .'join dbstat as s on s.name = m.name ' ."where m.type in ('table', 'index') and m.tbl_name not like 'sqlite_%' " .'group by m.tbl_name ' .'order by m.tbl_name' : "select name from sqlite_master where type = 'table' and name not like 'sqlite_%' order by name"; } /** * Compile the query to determine the views. * * @return string */ public function compileViews() { return "select name, sql as definition from sqlite_master where type = 'view' order by name"; } /** * 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", hidden as "extra" ' .'from pragma_table_xinfo(%s) order by cid asc', $this->quoteString(str_replace('.', '__', $table)) ); } /** * Compile the query to determine the indexes. * * @param string $table * @return string */ public function compileIndexes($table) { return sprintf( 'select \'primary\' as name, group_concat(col) as columns, 1 as "unique", 1 as "primary" ' .'from (select name as col from pragma_table_info(%s) where pk > 0 order by pk, cid) group by name ' .'union select name, group_concat(col) as columns, "unique", origin = \'pk\' as "primary" ' .'from (select il.*, ii.name as col from pragma_index_list(%s) il, pragma_index_info(il.name) ii order by il.seq, ii.seqno) ' .'group by name, "unique", "primary"', $table = $this->quoteString(str_replace('.', '__', $table)), $table ); } /** * Compile the query to determine the foreign keys. * * @param string $table * @return string */ public function compileForeignKeys($table) { return sprintf( 'select group_concat("from") as columns, "table" as foreign_table, ' .'group_concat("to") as foreign_columns, on_update, on_delete ' .'from (select * from pragma_foreign_key_list(%s) order by id desc, seq) ' .'group by id, "table", on_update, on_delete', $this->quoteString(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)), $this->addForeignKeys($this->getCommandsByName($blueprint, 'foreign')), $this->addPrimaryKeys($this->getCommandByName($blueprint, 'primary')) ); } /** * Get the foreign key syntax for a table creation statement. * * @param \Illuminate\Database\Schema\ForeignKeyDefinition[] $foreignKeys * @return string|null */ protected function addForeignKeys($foreignKeys) { return collect($foreignKeys)->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. return $sql.$this->getForeignKey($foreign); }, ''); } /** * 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. $sql = sprintf(', foreign key(%s) references %s(%s)', $this->columnize($foreign->columns), $this->wrapTable($foreign->on), $this->columnize((array) $foreign->references) ); 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 // this SQL so we can keep adding any other foreign constraints to this. if (! is_null($foreign->onUpdate)) { $sql .= " on update {$foreign->onUpdate}"; } return $sql; } /** * Get the primary key syntax for a table creation statement. * * @param \Illuminate\Support\Fluent|null $primary * @return string|null */ protected function addPrimaryKeys($primary) { if (! is_null($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)->map(function ($column) use ($blueprint) { return 'alter table '.$this->wrapTable($blueprint).' '.$column; })->all(); } /** * 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) { $schema = $connection->getSchemaBuilder(); $table = $blueprint->getTable(); $changedColumns = collect($blueprint->getChangedColumns()); $columnNames = []; $autoIncrementColumn = null; $columns = collect($schema->getColumns($table)) ->map(function ($column) use ($blueprint, $changedColumns, &$columnNames, &$autoIncrementColumn) { $column = $changedColumns->first(fn ($col) => $col->name === $column['name'], $column); if ($column instanceof Fluent) { $name = $this->wrap($column); $autoIncrementColumn = $column->autoIncrement ? $column->name : $autoIncrementColumn; if (is_null($column->virtualAs) && is_null($column->virtualAsJson) && is_null($column->storedAs) && is_null($column->storedAsJson)) { $columnNames[] = $name; } return $this->addModifiers($name.' '.$this->getType($column), $blueprint, $column); } else { $name = $this->wrap($column['name']); $autoIncrementColumn = $column['auto_increment'] ? $column['name'] : $autoIncrementColumn; $isGenerated = ! is_null($column['generation']); if (! $isGenerated) { $columnNames[] = $name; } return $this->addModifiers($name.' '.$column['type'], $blueprint, new ColumnDefinition([ 'change' => true, 'type' => $column['type_name'], 'nullable' => $column['nullable'], 'default' => $column['default'] ? new Expression($column['default']) : null, 'autoIncrement' => $column['auto_increment'], 'collation' => $column['collation'], 'comment' => $column['comment'], 'virtualAs' => $isGenerated && $column['generation']['type'] === 'virtual' ? $column['generation']['expression'] : null, 'storedAs' => $isGenerated && $column['generation']['type'] === 'stored' ? $column['generation']['expression'] : null, ]) ); } })->all(); $foreignKeys = collect($schema->getForeignKeys($table))->map(fn ($foreignKey) => new ForeignKeyDefinition([ 'columns' => $foreignKey['columns'], 'on' => $foreignKey['foreign_table'], 'references' => $foreignKey['foreign_columns'], 'onUpdate' => $foreignKey['on_update'], 'onDelete' => $foreignKey['on_delete'], ]))->all(); [$primary, $indexes] = collect($schema->getIndexes($table))->map(fn ($index) => new IndexDefinition([ 'name' => match (true) { $index['primary'] => 'primary', $index['unique'] => 'unique', default => 'index', }, 'index' => $index['name'], 'columns' => $index['columns'], ]))->partition(fn ($index) => $index->name === 'primary'); $indexes = collect($indexes)->reject(fn ($index) => str_starts_with('sqlite_', $index->index))->map( fn ($index) => $this->{'compile'.ucfirst($index->name)}($blueprint, $index) )->all(); $tempTable = $this->wrap('__temp__'.$blueprint->getPrefix().$table); $table = $this->wrapTable($blueprint); $columnNames = implode(', ', $columnNames); $foreignKeyConstraintsEnabled = $connection->scalar('pragma foreign_keys'); return array_filter(array_merge([ $foreignKeyConstraintsEnabled ? $this->compileDisableForeignKeyConstraints() : null, sprintf('create table %s (%s%s%s)', $tempTable, implode(', ', $columns), $this->addForeignKeys($foreignKeys), $autoIncrementColumn ? '' : $this->addPrimaryKeys($primary->first()) ), sprintf('insert into %s (%s) select %s from %s', $tempTable, $columnNames, $columnNames, $table), sprintf('drop table %s', $table), sprintf('alter table %s rename to %s', $tempTable, $table), ], $indexes, [$foreignKeyConstraintsEnabled ? $this->compileEnableForeignKeyConstraints() : null])); } /** * 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 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) { $table = $this->wrapTable($blueprint); $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns)); return collect($columns)->map(fn ($column) => 'alter table '.$table.' '.$column)->all(); } /** * 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) { $indexes = $connection->getSchemaBuilder()->getIndexes($blueprint->getTable()); $index = Arr::first($indexes, fn ($index) => $index['name'] === $command->from); if (! $index) { throw new RuntimeException("Index [{$command->from}] does not exist."); } if ($index['primary']) { throw new RuntimeException('SQLite does not support altering primary keys.'); } if ($index['unique']) { return [ $this->compileDropUnique($blueprint, new IndexDefinition(['index' => $index['name']])), $this->compileUnique($blueprint, new IndexDefinition(['index' => $command->to, 'columns' => $index['columns']]) ), ]; } return [ $this->compileDropIndex($blueprint, new IndexDefinition(['index' => $index['name']])), $this->compileIndex($blueprint, new IndexDefinition(['index' => $command->to, 'columns' => $index['columns']]) ), ]; } /** * 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 'double'; } /** * 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 */ protected function typeGeometry(Fluent $column) { return 'geometry'; } /** * Create the column definition for a spatial Geography type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeGeography(Fluent $column) { return $this->typeGeometry($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 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 ({$this->getValue($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 ({$this->getValue($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'; } } /** * 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}'"; } } /** * Wrap the given JSON selector. * * @param string $value * @return string */ protected function wrapJsonSelector($value) { [$field, $path] = $this->wrapJsonFieldAndPath($value); return 'json_extract('.$field.$path.')'; } } framework/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php 0000755 00000116754 15060132304 0021210 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\Database\Schema\ColumnDefinition; use Illuminate\Support\Fluent; use RuntimeException; class MySqlGrammar extends Grammar { /** * The possible column modifiers. * * @var string[] */ protected $modifiers = [ 'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable', '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) { $charset = $connection->getConfig('charset'); $collation = $connection->getConfig('collation'); if (! $charset || ! $collation) { return sprintf( 'create database %s', $this->wrapValue($name), ); } return sprintf( 'create database %s default character set %s default collate %s', $this->wrapValue($name), $this->wrapValue($charset), $this->wrapValue($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 tables. * * @param string $database * @return string */ public function compileTables($database) { return sprintf( 'select table_name as `name`, (data_length + index_length) as `size`, ' .'table_comment as `comment`, engine as `engine`, table_collation as `collation` ' ."from information_schema.tables where table_schema = %s and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') " .'order by table_name', $this->quoteString($database) ); } /** * Compile the query to determine the views. * * @param string $database * @return string */ public function compileViews($database) { return sprintf( 'select table_name as `name`, view_definition as `definition` ' .'from information_schema.views where table_schema = %s ' .'order by table_name', $this->quoteString($database) ); } /** * 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`, ' .'generation_expression as `expression`, 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 the query to determine the indexes. * * @param string $database * @param string $table * @return string */ public function compileIndexes($database, $table) { return sprintf( 'select index_name as `name`, group_concat(column_name order by seq_in_index) as `columns`, ' .'index_type as `type`, not non_unique as `unique` ' .'from information_schema.statistics where table_schema = %s and table_name = %s ' .'group by index_name, index_type, non_unique', $this->quoteString($database), $this->quoteString($table) ); } /** * Compile the query to determine the foreign keys. * * @param string $database * @param string $table * @return string */ public function compileForeignKeys($database, $table) { return sprintf( 'select kc.constraint_name as `name`, ' .'group_concat(kc.column_name order by kc.ordinal_position) as `columns`, ' .'kc.referenced_table_schema as `foreign_schema`, ' .'kc.referenced_table_name as `foreign_table`, ' .'group_concat(kc.referenced_column_name order by kc.ordinal_position) as `foreign_columns`, ' .'rc.update_rule as `on_update`, ' .'rc.delete_rule as `on_delete` ' .'from information_schema.key_column_usage kc join information_schema.referential_constraints rc ' .'on kc.constraint_schema = rc.constraint_schema and kc.constraint_name = rc.constraint_name ' .'where kc.table_schema = %s and kc.table_name = %s and kc.referenced_table_name is not null ' .'group by kc.constraint_name, kc.referenced_table_schema, kc.referenced_table_name, rc.update_rule, rc.delete_rule', $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) { $tableStructure = $this->getColumns($blueprint); if ($primaryKey = $this->getCommandByName($blueprint, 'primary')) { $tableStructure[] = sprintf( 'primary key %s(%s)', $primaryKey->algorithm ? 'using '.$primaryKey->algorithm : '', $this->columnize($primaryKey->columns) ); $primaryKey->shouldBeSkipped = true; } return sprintf('%s table %s (%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), implode(', ', $tableStructure) ); } /** * 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) { $version = $connection->getServerVersion(); if (($connection->isMaria() && version_compare($version, '10.5.2', '<')) || (! $connection->isMaria() && version_compare($version, '8.0.3', '<'))) { return $this->compileLegacyRenameColumn($blueprint, $command, $connection); } return parent::compileRenameColumn($blueprint, $command, $connection); } /** * Compile a rename column command for legacy versions of MySQL. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @param \Illuminate\Database\Connection $connection * @return string */ protected function compileLegacyRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { $column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable())) ->firstWhere('name', $command->from); $modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([ 'change' => true, 'type' => match ($column['type_name']) { 'bigint' => 'bigInteger', 'int' => 'integer', 'mediumint' => 'mediumInteger', 'smallint' => 'smallInteger', 'tinyint' => 'tinyInteger', default => $column['type_name'], }, 'nullable' => $column['nullable'], 'default' => $column['default'] && (str_starts_with(strtolower($column['default']), 'current_timestamp') || $column['default'] === 'NULL') ? new Expression($column['default']) : $column['default'], 'autoIncrement' => $column['auto_increment'], 'collation' => $column['collation'], 'comment' => $column['comment'], 'virtualAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'virtual' ? $column['generation']['expression'] : null, 'storedAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'stored' ? $column['generation']['expression'] : null, ])); return sprintf('alter table %s change %s %s %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to), $modifiers ); } /** * 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) { $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 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) { if ($column->precision) { return "float({$column->precision})"; } return 'float'; } /** * Create the column definition for a double type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeDouble(Fluent $column) { 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) { if ($column->length) { return $column->fixed ? "binary({$column->length})" : "varbinary({$column->length})"; } 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 */ protected function typeGeometry(Fluent $column) { $subtype = $column->subtype ? strtolower($column->subtype) : null; if (! in_array($subtype, ['point', 'linestring', 'polygon', 'geometrycollection', 'multipoint', 'multilinestring', 'multipolygon'])) { $subtype = null; } return sprintf('%s%s', $subtype ?? 'geometry', match (true) { $column->srid && $this->connection?->isMaria() => ' ref_system_id='.$column->srid, (bool) $column->srid => ' srid '.$column->srid, default => '', } ); } /** * Create the column definition for a spatial Geography type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeGeography(Fluent $column) { return $this->typeGeometry($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 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 ({$this->getValue($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 ({$this->getValue($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 $this->hasCommand($blueprint, 'primary') || ($column->change && ! $column->primary) ? ' auto_increment' : ' 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)."'"; } } /** * 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.'))'; } } framework/src/Illuminate/Database/Schema/Grammars/Grammar.php 0000755 00000025160 15060132304 0020210 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use BackedEnum; 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 sprintf('alter table %s rename column %s to %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to) ); } /** * 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) { throw new LogicException('This database driver does not support modifying 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) { 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 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) { throw new RuntimeException('This database driver does not support dropping foreign keys.'); } /** * 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 command with a given name 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; }); } /* * Determine if a command with a given name exists on the blueprint. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param string $name * @return bool */ protected function hasCommand(Blueprint $blueprint, $name) { foreach ($blueprint->getCommands() as $command) { if ($command->name === $name) { return true; } } return false; } /** * 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."'"; } /** * 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; } } framework/src/Illuminate/Database/Schema/Grammars/MariaDbGrammar.php 0000755 00000003260 15060132304 0021425 0 ustar 00 <?php namespace Illuminate\Database\Schema\Grammars; use Illuminate\Database\Connection; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; class MariaDbGrammar extends MySqlGrammar { /** * 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) { if (version_compare($connection->getServerVersion(), '10.5.2', '<')) { return $this->compileLegacyRenameColumn($blueprint, $command, $connection); } return parent::compileRenameColumn($blueprint, $command, $connection); } /** * 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 a spatial Geometry type. * * @param \Illuminate\Support\Fluent $column * @return string */ protected function typeGeometry(Fluent $column) { $subtype = $column->subtype ? strtolower($column->subtype) : null; if (! in_array($subtype, ['point', 'linestring', 'polygon', 'geometrycollection', 'multipoint', 'multilinestring', 'multipolygon'])) { $subtype = null; } return sprintf('%s%s', $subtype ?? 'geometry', $column->srid ? ' ref_system_id='.$column->srid : '' ); } } framework/src/Illuminate/Database/Schema/IndexDefinition.php 0000644 00000001072 15060132304 0020122 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 { // } framework/src/Illuminate/Database/Connection.php 0000755 00000121575 15060132304 0015757 0 ustar 00 <?php namespace Illuminate\Database; use Carbon\CarbonInterval; use Closure; use DateTimeInterface; 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 transaction is started. * * @var \Closure[] */ protected $beforeStartingTransaction = []; /** * All of the callbacks that should be invoked before a query is executed. * * @var \Closure[] */ protected $beforeExecutingCallbacks = []; /** * 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)) { 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); } /** * Register a hook to be run just before a database transaction is started. * * @param \Closure $callback * @return $this */ public function beforeStartingTransaction(Closure $callback) { $this->beforeStartingTransaction[] = $callback; return $this; } /** * 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; } /** * 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; } /** * Get the server version for the connection. * * @return string */ public function getServerVersion(): string { return $this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); } /** * 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; } } framework/src/Illuminate/Database/DatabaseServiceProvider.php 0000755 00000006303 15060132304 0020407 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; }); } } framework/src/Illuminate/Database/README.md 0000755 00000004240 15060132304 0014413 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). framework/src/Illuminate/Database/Grammar.php 0000755 00000017532 15060132304 0015243 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->getValue($table); } // If the table being wrapped has an alias we'll need to separate the pieces // so we can prefix the table and then wrap each of the segments on their // own and then join these both back together using the "as" connector. if (stripos($table, ' as ') !== false) { return $this->wrapAliasedTable($table); } // If the table being wrapped has a custom schema name specified, we need to // prefix the last segment as the table name then wrap each segment alone // and eventually join them both back together using the dot connector. if (str_contains($table, '.')) { $table = substr_replace($table, '.'.$this->tablePrefix, strrpos($table, '.'), 1); return collect(explode('.', $table)) ->map($this->wrapValue(...)) ->implode('.'); } return $this->wrapValue($this->tablePrefix.$table); } /** * Wrap a value in keyword identifiers. * * @param \Illuminate\Contracts\Database\Query\Expression|string $value * @return string */ public function wrap($value) { 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); } // 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 * @return string */ protected function wrapAliasedValue($value) { $segments = preg_split('/\s+as\s+/i', $value); return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[1]); } /** * Wrap a table that has an alias. * * @param string $value * @return string */ protected function wrapAliasedTable($value) { $segments = preg_split('/\s+as\s+/i', $value); return $this->wrapTable($segments[0]).' as '.$this->wrapValue($this->tablePrefix.$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; } } framework/src/Illuminate/Cache/CacheServiceProvider.php 0000755 00000002506 15060132304 0017206 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, ]; } } framework/src/Illuminate/Cache/MemcachedStore.php 0000755 00000014234 15060132304 0016033 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|false */ 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|false */ 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 = $prefix; } } framework/src/Illuminate/Cache/RateLimiting/GlobalLimit.php 0000644 00000000573 15060132304 0017735 0 ustar 00 <?php namespace Illuminate\Cache\RateLimiting; class GlobalLimit extends Limit { /** * Create a new limit instance. * * @param int $maxAttempts * @param int $decaySeconds * @return void */ public function __construct(int $maxAttempts, int $decaySeconds = 60) { parent::__construct('', $maxAttempts, $decaySeconds); } } framework/src/Illuminate/Cache/RateLimiting/Limit.php 0000644 00000006065 15060132304 0016616 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 seconds. * * @var int */ public $maxAttempts; /** * The number of seconds until the rate limit is reset. * * @var int */ public $decaySeconds; /** * The response generator callback. * * @var callable */ public $responseCallback; /** * Create a new limit instance. * * @param mixed $key * @param int $maxAttempts * @param int $decaySeconds * @return void */ public function __construct($key = '', int $maxAttempts = 60, int $decaySeconds = 60) { $this->key = $key; $this->maxAttempts = $maxAttempts; $this->decaySeconds = $decaySeconds; } /** * Create a new rate limit. * * @param int $maxAttempts * @param int $decaySeconds * @return static */ public static function perSecond($maxAttempts, $decaySeconds = 1) { return new static('', $maxAttempts, $decaySeconds); } /** * Create a new rate limit. * * @param int $maxAttempts * @param int $decayMinutes * @return static */ public static function perMinute($maxAttempts, $decayMinutes = 1) { return new static('', $maxAttempts, 60 * $decayMinutes); } /** * 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, 60 * $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 * 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 * 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; } } framework/src/Illuminate/Cache/RateLimiting/Unlimited.php 0000644 00000000400 15060132304 0017455 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); } } framework/src/Illuminate/Cache/ApcWrapper.php 0000755 00000002641 15060132304 0015213 0 ustar 00 <?php namespace Illuminate\Cache; class ApcWrapper { /** * Get an item from the cache. * * @param string $key * @return mixed */ public function get($key) { $fetchedValue = apcu_fetch($key, $success); return $success ? $fetchedValue : null; } /** * 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 apcu_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 apcu_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 apcu_dec($key, $value); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function delete($key) { return apcu_delete($key); } /** * Remove all items from the cache. * * @return bool */ public function flush() { return apcu_clear_cache(); } } framework/src/Illuminate/Cache/CacheLock.php 0000644 00000003455 15060132304 0014764 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); } } framework/src/Illuminate/Cache/DynamoDbLock.php 0000644 00000003056 15060132304 0015453 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); } } framework/src/Illuminate/Cache/RedisTagSet.php 0000644 00000006203 15060132304 0015320 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|null $ttl * @param string $updateWhen * @return void */ public function addEntry(string $key, ?int $ttl = null, $updateWhen = null) { $ttl = is_null($ttl) ? -1 : Carbon::now()->addSeconds($ttl)->getTimestamp(); 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"; } } framework/src/Illuminate/Cache/DatabaseLock.php 0000644 00000007465 15060132304 0015472 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', '<=', $this->currentTime()); })->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', '<=', $this->currentTime())->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 $this->currentTime() + $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(); } } framework/src/Illuminate/Cache/HasCacheLock.php 0000644 00000001251 15060132304 0015410 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); } } framework/src/Illuminate/Cache/Events/CacheMissed.php 0000644 00000000133 15060132304 0016552 0 ustar 00 <?php namespace Illuminate\Cache\Events; class CacheMissed extends CacheEvent { // } framework/src/Illuminate/Cache/Events/CacheHit.php 0000644 00000001054 15060132304 0016055 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|null $storeName * @param string $key * @param mixed $value * @param array $tags * @return void */ public function __construct($storeName, $key, $value, array $tags = []) { parent::__construct($storeName, $key, $tags); $this->value = $value; } } framework/src/Illuminate/Cache/Events/KeyForgotten.php 0000644 00000000134 15060132304 0017023 0 ustar 00 <?php namespace Illuminate\Cache\Events; class KeyForgotten extends CacheEvent { // } framework/src/Illuminate/Cache/Events/CacheEvent.php 0000644 00000001652 15060132304 0016416 0 ustar 00 <?php namespace Illuminate\Cache\Events; abstract class CacheEvent { /** * The name of the cache store. * * @var string|null */ public $storeName; /** * 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|null $storeName * @param string $key * @param array $tags * @return void */ public function __construct($storeName, $key, array $tags = []) { $this->storeName = $storeName; $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; } } framework/src/Illuminate/Cache/Events/KeyWritten.php 0000644 00000001364 15060132304 0016516 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|null $storeName * @param string $key * @param mixed $value * @param int|null $seconds * @param array $tags * @return void */ public function __construct($storeName, $key, $value, $seconds = null, $tags = []) { parent::__construct($storeName, $key, $tags); $this->value = $value; $this->seconds = $seconds; } } framework/src/Illuminate/Cache/RedisTaggedCache.php 0000644 00000006036 15060132304 0016254 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) { $seconds = null; if ($ttl !== null) { $seconds = $this->getSeconds($ttl); if ($seconds > 0) { $this->tags->addEntry( $this->itemKey($key), $seconds ); } } 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); } $seconds = $this->getSeconds($ttl); if ($seconds > 0) { $this->tags->addEntry( $this->itemKey($key), $seconds ); } 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; } } framework/src/Illuminate/Cache/LICENSE.md 0000644 00000002063 15060132304 0014035 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. framework/src/Illuminate/Cache/RedisLock.php 0000644 00000003356 15060132304 0015027 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(); } } framework/src/Illuminate/Cache/FileStore.php 0000755 00000023640 15060132304 0015045 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 $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 { if (is_null($contents = $this->files->get($path, true))) { return $this->emptyPayload(); } $expire = substr($contents, 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 working directory of the cache. * * @param string $directory * @return $this */ public function setDirectory($directory) { $this->directory = $directory; return $this; } /** * 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 ''; } } framework/src/Illuminate/Cache/TagSet.php 0000644 00000004725 15060132304 0014340 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; } } framework/src/Illuminate/Cache/RetrievesMultipleKeys.php 0000644 00000002203 15060132304 0017456 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; } } framework/src/Illuminate/Cache/RedisStore.php 0000755 00000024151 15060132304 0015232 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 $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 = $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); } } framework/src/Illuminate/Cache/TaggableStore.php 0000644 00000000645 15060132304 0015671 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())); } } framework/src/Illuminate/Cache/NoLock.php 0000644 00000001264 15060132304 0014331 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; } } framework/src/Illuminate/Cache/TaggedCache.php 0000644 00000005001 15060132304 0015254 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; } } framework/src/Illuminate/Cache/NullStore.php 0000755 00000004541 15060132304 0015077 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 false */ public function increment($key, $value = 1) { return false; } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return false */ 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 ''; } } framework/src/Illuminate/Cache/Lock.php 0000644 00000007354 15060132304 0014042 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 */ public function isOwnedByCurrentProcess() { return $this->isOwnedBy($this->owner); } /** * Determine whether this lock is owned by the given identifier. * * @param string|null $owner * @return bool */ public function isOwnedBy($owner) { return $this->getCurrentOwner() === $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; } } framework/src/Illuminate/Cache/RateLimiter.php 0000644 00000013231 15060132304 0015362 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 (by 1) 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) { return $this->increment($key, $decaySeconds); } /** * Increment the counter for a given key for a given decay time by a given amount. * * @param string $key * @param int $decaySeconds * @param int $amount * @return int */ public function increment($key, $decaySeconds = 60, $amount = 1) { $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, $amount); if (! $added && $hits == 1) { $this->cache->put($key, 1, $decaySeconds); } return $hits; } /** * Decrement the counter for a given key for a given decay time by a given amount. * * @param string $key * @param int $decaySeconds * @param int $amount * @return int */ public function decrement($key, $decaySeconds = 60, $amount = 1) { return $this->increment($key, $decaySeconds, $amount * -1); } /** * 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)); } } framework/src/Illuminate/Cache/composer.json 0000755 00000002761 15060132304 0015163 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.2", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0" }, "provide": { "psr/simple-cache-implementation": "1.0|2.0|3.0" }, "autoload": { "psr-4": { "Illuminate\\Cache\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.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 (^11.0).", "illuminate/filesystem": "Required to use the file cache driver (^11.0).", "illuminate/redis": "Required to use the redis cache driver (^11.0).", "symfony/cache": "Required to use PSR-6 cache bridge (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Cache/Repository.php 0000755 00000042372 15060132304 0015333 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, 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; /** * The cache store configuration options. * * @var array */ protected $config = []; /** * Create a new cache repository instance. * * @param \Illuminate\Contracts\Cache\Store $store * @param array $config * @return void */ public function __construct(Store $store, array $config = []) { $this->store = $store; $this->config = $config; } /** * 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($this->getName(), $key)); $value = value($default); } else { $this->event(new CacheHit($this->getName(), $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($this->getName(), $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($this->getName(), $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($this->getName(), $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($this->getName(), $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($this->getName(), $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($this->getName(), $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()); $cache->config = $this->config; 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()->diffInSeconds($duration, false); } return (int) ($duration > 0 ? $duration : 0); } /** * Get the name of the cache store. * * @return string|null */ protected function getName() { return $this->config['store'] ?? null; } /** * 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; } } framework/src/Illuminate/Cache/DynamoDbStore.php 0000644 00000035111 15060132305 0015655 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) $this->currentTime(), ], ], ]); 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|false */ 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) $this->currentTime(), ], ':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|false */ 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) $this->currentTime(), ], ':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) : $this->currentTime(); } /** * 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 = $prefix; } /** * Get the DynamoDb Client instance. * * @return \Aws\DynamoDb\DynamoDbClient */ public function getClient() { return $this->dynamo; } } framework/src/Illuminate/Cache/PhpRedisLock.php 0000644 00000001475 15060132305 0015500 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]) ); } } framework/src/Illuminate/Cache/ApcStore.php 0000755 00000005214 15060132305 0014667 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 $key * @return mixed */ public function get($key) { return $this->apc->get($this->prefix.$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 $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; } /** * Set the cache key prefix. * * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = $prefix; } } framework/src/Illuminate/Cache/CacheManager.php 0000755 00000025763 15060132305 0015460 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."); } $config = Arr::add($config, 'store', $name); 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), $config); } /** * 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), $config); } /** * 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), $config ); } /** * 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), $config); } /** * 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), $config ); } /** * 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) ), $config ); } /** * 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) ), $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 * @param array $config * @return \Illuminate\Cache\Repository */ public function repository(Store $store, array $config = []) { return tap(new Repository($store, Arr::only($config, ['store'])), function ($repository) use ($config) { if ($config['events'] ?? true) { $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); } } framework/src/Illuminate/Cache/MemcachedLock.php 0000644 00000002655 15060132305 0015631 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); } } framework/src/Illuminate/Cache/ArrayStore.php 0000644 00000011254 15060132305 0015240 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 $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); } } framework/src/Illuminate/Cache/MemcachedConnector.php 0000755 00000004516 15060132305 0016674 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); } } framework/src/Illuminate/Cache/Console/stubs/cache.stub 0000644 00000001521 15060132305 0017134 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'); } }; framework/src/Illuminate/Cache/Console/ForgetCommand.php 0000755 00000002434 15060132305 0017277 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.'); } } framework/src/Illuminate/Cache/Console/CacheTableCommand.php 0000644 00000002012 15060132305 0020011 0 ustar 00 <?php namespace Illuminate\Cache\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'make:cache-table', aliases: ['cache:table'])] class CacheTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:cache-table'; /** * The console command name aliases. * * @var array */ protected $aliases = ['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'; } } framework/src/Illuminate/Cache/Console/ClearCommand.php 0000755 00000006564 15060132305 0017107 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], ]; } } framework/src/Illuminate/Cache/Console/PruneStaleTagsCommand.php 0000644 00000002657 15060132305 0020756 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 int|null */ 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'], ]; } } framework/src/Illuminate/Cache/DatabaseStore.php 0000755 00000025727 15060132305 0015703 0 ustar 00 <?php namespace Illuminate\Cache; use Closure; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Contracts\Cache\Store; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\PostgresConnection; use Illuminate\Database\QueryException; use Illuminate\Database\SqlServerConnection; 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 $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->forgetIfExpired($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; return $this->table()->upsert(compact('key', 'value', 'expiration'), 'key') > 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) { if (! is_null($this->get($key))) { return false; } $key = $this->prefix.$key; $value = $this->serialize($value); $expiration = $this->getTime() + $seconds; if (! $this->getConnection() instanceof SqlServerConnection) { return $this->table()->insertOrIgnore(compact('key', 'value', 'expiration')) > 0; } try { return $this->table()->insert(compact('key', 'value', 'expiration')); } catch (QueryException) { // ... } return false; } /** * Increment the value of an item in the cache. * * @param string $key * @param int $value * @return int|false */ 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 int $value * @return int|false */ 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 int|float $value * @param \Closure $callback * @return int|false */ 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 an item from the cache if it is expired. * * @param string $key * @return bool */ public function forgetIfExpired($key) { $this->table() ->where('key', '=', $this->prefix.$key) ->where('expiration', '<=', $this->getTime()) ->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; } /** * Set the cache key prefix. * * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = $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); } } framework/src/Illuminate/Cache/FileLock.php 0000644 00000000417 15060132305 0014634 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); } } framework/src/Illuminate/Cache/LuaScripts.php 0000644 00000000716 15060132305 0015237 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; } } framework/src/Illuminate/Cache/ArrayLock.php 0000644 00000004145 15060132305 0015035 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() { if (! $this->exists()) { return null; } 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]); } } framework/src/Illuminate/Notifications/DatabaseNotification.php 0000644 00000005440 15060132305 0021026 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); } } framework/src/Illuminate/Notifications/RoutesNotifications.php 0000644 00000002421 15060132305 0020762 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, }; } } framework/src/Illuminate/Notifications/Channels/BroadcastChannel.php 0000644 00000003763 15060132305 0021707 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 toBroadcast / toArray method.'); } } framework/src/Illuminate/Notifications/Channels/DatabaseChannel.php 0000644 00000003665 15060132305 0021512 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.'); } } framework/src/Illuminate/Notifications/Channels/MailChannel.php 0000644 00000022156 15060132305 0020664 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; } } framework/src/Illuminate/Notifications/Events/NotificationSent.php 0000644 00000002157 15060132305 0021501 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; } } framework/src/Illuminate/Notifications/Events/NotificationSending.php 0000644 00000001674 15060132305 0022162 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; } } framework/src/Illuminate/Notifications/Events/NotificationFailed.php 0000644 00000002161 15060132305 0021747 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; } } framework/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php 0000644 00000006636 15060132305 0023770 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__; } } framework/src/Illuminate/Notifications/LICENSE.md 0000644 00000002063 15060132305 0015644 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. framework/src/Illuminate/Notifications/Messages/DatabaseMessage.php 0000644 00000000625 15060132305 0021533 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; } } framework/src/Illuminate/Notifications/Messages/MailMessage.php 0000644 00000021531 15060132305 0020710 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 plain text view for the mail message. * * @param string $textView * @param array $data * @return $this */ public function text($textView, array $data = []) { return $this->view([ 'html' => is_array($this->view) ? ($this->view['html'] ?? null) : $this->view, 'text' => $textView, ], $data); } /** * 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; } } framework/src/Illuminate/Notifications/Messages/SimpleMessage.php 0000644 00000013332 15060132305 0021257 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 ?? ''), ]; } } framework/src/Illuminate/Notifications/Messages/BroadcastMessage.php 0000644 00000001156 15060132305 0021731 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; } } framework/src/Illuminate/Notifications/DatabaseNotificationCollection.php 0000644 00000001172 15060132305 0023040 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(); } } framework/src/Illuminate/Notifications/AnonymousNotifiable.php 0000644 00000003153 15060132305 0020737 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() { // } } framework/src/Illuminate/Notifications/Action.php 0000644 00000000737 15060132305 0016174 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; } } framework/src/Illuminate/Notifications/NotificationSender.php 0000644 00000017315 15060132305 0020546 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; } } framework/src/Illuminate/Notifications/HasDatabaseNotifications.php 0000644 00000001414 15060132305 0021642 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(); } } framework/src/Illuminate/Notifications/NotificationServiceProvider.php 0000644 00000002150 15060132305 0022430 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 ); } } framework/src/Illuminate/Notifications/composer.json 0000644 00000002306 15060132305 0016762 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.2", "illuminate/broadcasting": "^11.0", "illuminate/bus": "^11.0", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/filesystem": "^11.0", "illuminate/mail": "^11.0", "illuminate/queue": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Notifications\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "illuminate/database": "Required to use the database transport (^11.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Notifications/resources/views/email.blade.php 0000644 00000002071 15060132305 0022254 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> framework/src/Illuminate/Notifications/Console/stubs/notifications.stub 0000644 00000001326 15060132305 0022553 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'); } }; framework/src/Illuminate/Notifications/Console/NotificationTableCommand.php 0000644 00000002110 15060132305 0023241 0 ustar 00 <?php namespace Illuminate\Notifications\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'make:notifications-table', aliases: ['notifications:table'])] class NotificationTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:notifications-table'; /** * The console command name aliases. * * @var array */ protected $aliases = ['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'; } } framework/src/Illuminate/Notifications/ChannelManager.php 0000644 00000007575 15060132305 0017631 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; } } framework/src/Illuminate/Notifications/Notification.php 0000644 00000001400 15060132305 0017371 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; } } framework/src/Illuminate/Notifications/Notifiable.php 0000644 00000000170 15060132305 0017022 0 ustar 00 <?php namespace Illuminate\Notifications; trait Notifiable { use HasDatabaseNotifications, RoutesNotifications; } framework/src/Illuminate/Notifications/SendQueuedNotifications.php 0000644 00000011536 15060132305 0021552 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; } } framework/src/Illuminate/Redis/RedisServiceProvider.php 0000755 00000001616 15060132305 0017316 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']; } } framework/src/Illuminate/Redis/Connections/PhpRedisConnection.php 0000644 00000033766 15060132305 0021244 0 ustar 00 <?php namespace Illuminate\Redis\Connections; use Closure; use Illuminate\Contracts\Redis\Connection as ConnectionContract; 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); } } framework/src/Illuminate/Redis/Connections/PredisConnection.php 0000644 00000003243 15060132305 0020737 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(); } } framework/src/Illuminate/Redis/Connections/PhpRedisClusterConnection.php 0000644 00000001122 15060132305 0022563 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]); } } } framework/src/Illuminate/Redis/Connections/Connection.php 0000644 00000012130 15060132305 0017563 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); } } framework/src/Illuminate/Redis/Connections/PacksPhpRedisValues.php 0000644 00000011766 15060132305 0021362 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, '>='); } } framework/src/Illuminate/Redis/Connections/PredisClusterConnection.php 0000644 00000001130 15060132305 0022272 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())); } } } framework/src/Illuminate/Redis/Connectors/PredisConnector.php 0000644 00000003027 15060132305 0020425 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 ))); } } framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php 0000644 00000016417 15060132305 0020724 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']; } /** * 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 (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']; } } framework/src/Illuminate/Redis/Events/CommandExecuted.php 0000644 00000002245 15060132305 0017521 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(); } } framework/src/Illuminate/Redis/LICENSE.md 0000644 00000002063 15060132305 0014101 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. framework/src/Illuminate/Redis/Limiters/ConcurrencyLimiterBuilder.php 0000644 00000006032 15060132305 0022125 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; } } } framework/src/Illuminate/Redis/Limiters/DurationLimiterBuilder.php 0000644 00000005727 15060132305 0021432 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; } } } framework/src/Illuminate/Redis/Limiters/DurationLimiter.php 0000644 00000011154 15060132305 0020112 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; } } framework/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php 0000644 00000007463 15060132305 0020627 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; } } framework/src/Illuminate/Redis/composer.json 0000755 00000002066 15060132305 0015225 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.2", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Redis\\": "" } }, "suggest": { "ext-redis": "Required to use the phpredis connector (^4.0|^5.0|^6.0).", "predis/predis": "Required to use the predis connector (^2.0.2)." }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Redis/RedisManager.php 0000644 00000014761 15060132305 0015557 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 $key !== 'driver'; }, 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); } } framework/src/Illuminate/View/AnonymousComponent.php 0000644 00000002215 15060132305 0016724 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] ); } } framework/src/Illuminate/View/InvokableComponentVariable.php 0000644 00000004005 15060132305 0020313 0 ustar 00 <?php namespace Illuminate\View; use ArrayIterator; use Closure; use Illuminate\Contracts\Support\DeferringDisplayableValue; use Illuminate\Support\Enumerable; use IteratorAggregate; use Stringable; use Traversable; class InvokableComponentVariable implements DeferringDisplayableValue, IteratorAggregate, Stringable { /** * 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 string */ public function __toString() { return (string) $this->__invoke(); } } framework/src/Illuminate/View/DynamicComponent.php 0000644 00000011240 15060132305 0016316 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; } } framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php 0000644 00000002622 15060132305 0021557 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); } } framework/src/Illuminate/View/View.php 0000755 00000026407 15060132305 0013777 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 Stringable; use Throwable; class View implements ArrayAccess, Htmlable, Stringable, 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 or return all fragments. * * @param array|null $fragments * @return string */ public function fragments(?array $fragments = null) { return is_null($fragments) ? $this->allFragments() : 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|null $fragments * @return string */ public function fragmentsIf($boolean, ?array $fragments = null) { if (value($boolean)) { return $this->fragments($fragments); } return $this->render(); } /** * Get all fragments as a single string. * * @return string */ protected function allFragments() { return collect($this->render(fn () => $this->factory->getFragments()))->implode(''); } /** * 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(); } } framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php 0000644 00000063116 15060132305 0021106 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): ?> <?php $attributes = $attributes->except(\\'.$class.'::ignoredParameterNames()); ?> <?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; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php 0000644 00000021734 15060132305 0022711 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 else-if push statements into valid PHP. * * @param string $expression * @return string */ protected function compileElsePushIf($expression) { $parts = explode(',', $this->stripParentheses($expression), 2); return "<?php \$__env->stopPush(); elseif({$parts[0]}): \$__env->startPush({$parts[1]}); ?>"; } /** * Compile the else push statements into valid PHP. * * @param string $expression * @return string */ protected function compileElsePush($expression) { return "<?php \$__env->stopPush(); else: \$__env->startPush{$expression}; ?>"; } /** * Compile the end-push statements into valid PHP. * * @return string */ protected function compileEndPushIf() { return '<?php $__env->stopPush(); endif; ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php 0000644 00000001154 15060132305 0021456 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}; ?>"; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesClasses.php 0000644 00000000676 15060132305 0021662 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}; ?>\""; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php 0000644 00000004601 15060132305 0022023 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(); ?>"; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesStacks.php 0000644 00000005351 15060132305 0021510 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; ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesEchos.php 0000644 00000011000 15060132305 0021305 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); } if (is_iterable($value) && isset($this->echoHandlers['iterable'])) { return call_user_func($this->echoHandlers['iterable'], $value); } return $value; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesHelpers.php 0000644 00000003076 15060132305 0021664 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(); ?>"; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesJson.php 0000644 00000001325 15060132305 0021166 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) ?>"; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesComments.php 0000644 00000000635 15060132305 0022045 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); } } framework/src/Illuminate/View/Compilers/Concerns/CompilesErrors.php 0000644 00000001645 15060132305 0021536 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); ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesInjections.php 0000644 00000000771 15060132305 0022366 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}); ?>"; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesFragments.php 0000644 00000001331 15060132305 0022200 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(); ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesStyles.php 0000644 00000000673 15060132305 0021545 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} ?>\""; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesLoops.php 0000644 00000011471 15060132305 0021354 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; ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesTranslations.php 0000644 00000002035 15060132305 0022735 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}; ?>"; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php 0000644 00000014732 15060132305 0022410 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString; use Illuminate\Support\Str; use Illuminate\View\AnonymousComponent; 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 === AnonymousComponent::class ? $component.':'.trim($alias, '\'"') : $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 if (isset($attributes)) { $__attributesOriginal'.$hash.' = $attributes; } ?>', '<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>', '<?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($__attributesOriginal'.$hash.')): ?>', '<?php $attributes = $__attributesOriginal'.$hash.'; ?>', '<?php unset($__attributesOriginal'.$hash.'); ?>', '<?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; \$__newAttributes = []; \$__propNames = \Illuminate\View\ComponentAttributeBag::extractPropNames({$expression}); foreach (\$attributes->all() as \$__key => \$__value) { if (in_array(\$__key, \$__propNames)) { \$\$__key = \$\$__key ?? \$__value; } else { \$__newAttributes[\$__key] = \$__value; } } \$attributes = new \Illuminate\View\ComponentAttributeBag(\$__newAttributes); unset(\$__propNames); unset(\$__newAttributes); foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) { \$\$__key = \$\$__key ?? \$__value; } \$__defined_vars = get_defined_vars(); foreach (\$attributes->all() as \$__key => \$__value) { if (array_key_exists(\$__key, \$__defined_vars)) unset(\$\$__key); } 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; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesLayouts.php 0000644 00000006013 15060132305 0021714 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(); ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php 0000644 00000001024 15060132305 0023055 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesUseStatements { /** * Compile the use statements into valid PHP. * * @param string $expression * @return string */ protected function compileUse($expression) { $segments = explode(',', preg_replace("/[\(\)]/", '', $expression)); $use = ltrim(trim($segments[0], " '\""), '\\'); $as = isset($segments[1]) ? ' as '.trim($segments[1], " '\"") : ''; return "<?php use \\{$use}{$as}; ?>"; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesAuthorizations.php 0000644 00000004706 15060132305 0023306 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; ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesSessions.php 0000644 00000001765 15060132305 0022073 0 ustar 00 <?php namespace Illuminate\View\Compilers\Concerns; trait CompilesSessions { /** * Compile the session statements into valid PHP. * * @param string $expression * @return string */ protected function compileSession($expression) { $expression = $this->stripParentheses($expression); return '<?php $__sessionArgs = ['.$expression.']; if (session()->has($__sessionArgs[0])) : if (isset($value)) { $__sessionPrevious[] = $value; } $value = session()->get($__sessionArgs[0]); ?>'; } /** * Compile the endsession statements into valid PHP. * * @param string $expression * @return string */ protected function compileEndsession($expression) { return '<?php unset($value); if (isset($__sessionPrevious) && !empty($__sessionPrevious)) { $value = array_pop($__sessionPrevious); } if (isset($__sessionPrevious) && empty($__sessionPrevious)) { unset($__sessionPrevious); } endif; unset($__sessionArgs); ?>'; } } framework/src/Illuminate/View/Compilers/Concerns/CompilesJs.php 0000644 00000000677 15060132305 0020642 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) ); } } framework/src/Illuminate/View/Compilers/CompilerInterface.php 0000755 00000001060 15060132305 0020401 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); } framework/src/Illuminate/View/Compilers/BladeCompiler.php 0000644 00000064656 15060132305 0017531 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\CompilesSessions, Concerns\CompilesStacks, Concerns\CompilesStyles, Concerns\CompilesTranslations, Concerns\CompilesUseStatements, 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(\s*)(.*?)@endverbatim/s', function ($matches) { return $matches[1].$this->storeRawBlock($matches[2]); }, $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; } } framework/src/Illuminate/View/Compilers/Compiler.php 0000755 00000006341 15060132305 0016567 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); } } } framework/src/Illuminate/View/Concerns/ManagesLayouts.php 0000644 00000013435 15060132305 0017565 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 = []; } } framework/src/Illuminate/View/Concerns/ManagesFragments.php 0000644 00000003320 15060132305 0020043 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 = []; } } framework/src/Illuminate/View/Concerns/ManagesLoops.php 0000644 00000004433 15060132305 0017217 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; } } framework/src/Illuminate/View/Concerns/ManagesStacks.php 0000644 00000010333 15060132305 0017347 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 = []; } } framework/src/Illuminate/View/Concerns/ManagesEvents.php 0000644 00000011766 15060132305 0017376 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) { if ($this->events->hasListeners($event = 'composing: '.$view->name())) { $this->events->dispatch($event, [$view]); } } /** * Call the creator for a given view. * * @param \Illuminate\Contracts\View\View $view * @return void */ public function callCreator(ViewContract $view) { if ($this->events->hasListeners($event = 'creating: '.$view->name())) { $this->events->dispatch($event, [$view]); } } } framework/src/Illuminate/View/Concerns/ManagesTranslations.php 0000644 00000001373 15060132305 0020604 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 ); } } framework/src/Illuminate/View/Concerns/ManagesComponents.php 0000644 00000012523 15060132305 0020247 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 = []; } } framework/src/Illuminate/View/Component.php 0000644 00000031330 15060132305 0015013 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 = []; /** * The cache of ignored parameter names. * * @var array */ protected static $ignoredParameterNames = []; /** * 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 ($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; } /** * Get the cached set of anonymous component constructor parameter names to exclude. * * @return array */ public static function ignoredParameterNames() { if (! isset(static::$ignoredParameterNames[static::class])) { $constructor = (new ReflectionClass( static::class ))->getConstructor(); if (! $constructor) { return static::$ignoredParameterNames[static::class] = []; } static::$ignoredParameterNames[static::class] = collect($constructor->getParameters()) ->map->getName() ->all(); } return static::$ignoredParameterNames[static::class]; } /** * 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; } } framework/src/Illuminate/View/LICENSE.md 0000644 00000002063 15060132305 0013745 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. framework/src/Illuminate/View/ViewFinderInterface.php 0000755 00000002653 15060132305 0016745 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(); } framework/src/Illuminate/View/ComponentSlot.php 0000644 00000004322 15060132305 0015656 0 ustar 00 <?php namespace Illuminate\View; use Illuminate\Contracts\Support\Htmlable; use InvalidArgumentException; use Stringable; class ComponentSlot implements Htmlable, Stringable { /** * 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(); } /** * Determine if the slot has non-comment content. * * @param callable|string|null $callable * @return bool */ public function hasActualContent(callable|string|null $callable = null) { if (is_string($callable) && ! function_exists($callable)) { throw new InvalidArgumentException('Callable does not exist.'); } return filter_var( $this->contents, FILTER_CALLBACK, ['options' => $callable ?? fn ($input) => trim(preg_replace("/<!--([\s\S]*?)-->/", '', $input))] ) !== ''; } /** * Get the slot's HTML string. * * @return string */ public function __toString() { return $this->toHtml(); } } framework/src/Illuminate/View/Engines/PhpEngine.php 0000755 00000003531 15060132305 0016323 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; } } framework/src/Illuminate/View/Engines/Engine.php 0000755 00000000552 15060132305 0015653 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; } } framework/src/Illuminate/View/Engines/FileEngine.php 0000644 00000001403 15060132305 0016444 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); } } framework/src/Illuminate/View/Engines/EngineResolver.php 0000755 00000002702 15060132305 0017374 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]); } } framework/src/Illuminate/View/Engines/CompilerEngine.php 0000755 00000010025 15060132305 0017342 0 ustar 00 <?php namespace Illuminate\View\Engines; use Illuminate\Database\RecordsNotFoundException; 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 || $e instanceof RecordsNotFoundException) { 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 = []; } } framework/src/Illuminate/View/ViewException.php 0000644 00000001607 15060132305 0015646 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); } } } framework/src/Illuminate/View/ComponentAttributeBag.php 0000644 00000030564 15060132305 0017321 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 Stringable; use Traversable; class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSerializable, Htmlable, Stringable { 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 all of the attribute values. * * @return array */ public function all() { return $this->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(static::extractPropNames($keys)); } /** * Exclude the given attribute from the attribute array. * * @param mixed|array $keys * @return static */ public function exceptProps($keys) { return $this->except(static::extractPropNames($keys)); } /** * 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; } /** * Determine if the attribute bag is empty. * * @return bool */ public function isEmpty() { return trim((string) $this) === ''; } /** * Determine if the attribute bag is not empty. * * @return bool */ public function isNotEmpty() { return ! $this->isEmpty(); } /** * 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; } /** * Extract "prop" names from given keys. * * @param array $keys * @return array */ public static function extractPropNames(array $keys) { $props = []; foreach ($keys as $key => $default) { $key = is_numeric($key) ? $default : $key; $props[] = $key; $props[] = Str::kebab($key); } return $props; } /** * 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) { $value = $key === 'x-data' || str_starts_with($key, 'wire:') ? '' : $key; } $string .= ' '.$key.'="'.str_replace('"', '\\"', trim($value)).'"'; } return trim($string); } } framework/src/Illuminate/View/composer.json 0000644 00000002027 15060132305 0015063 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.2", "ext-tokenizer": "*", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/events": "^11.0", "illuminate/filesystem": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\View\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/View/AppendableAttributeValue.php 0000644 00000001057 15060132305 0017770 0 ustar 00 <?php namespace Illuminate\View; use Stringable; class AppendableAttributeValue implements Stringable { /** * 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; } } framework/src/Illuminate/View/FileViewFinder.php 0000755 00000016241 15060132305 0015722 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) { $viewPath = $path.'/'.$file; if (strlen($viewPath) <= PHP_MAXPATHLEN && $this->files->exists($viewPath)) { 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; } } framework/src/Illuminate/View/Factory.php 0000755 00000036146 15060132305 0014475 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 = []; /** * The cached array of engines for paths. * * @var array */ protected $pathEngineCache = []; /** * The cache of normalized names for views. * * @var array */ protected $normalizedNameCache = []; /** * 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 $this->normalizedNameCache[$name] ??= 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 (isset($this->pathEngineCache[$path])) { return $this->engines->resolve($this->pathEngineCache[$path]); } if (! $extension = $this->getExtension($path)) { throw new InvalidArgumentException("Unrecognized extension in file: {$path}."); } return $this->engines->resolve( $this->pathEngineCache[$path] = $this->extensions[$extension] ); } /** * 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); $this->pathEngineCache = []; } /** * 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; } } framework/src/Illuminate/View/ViewServiceProvider.php 0000755 00000012424 15060132305 0017025 0 ustar 00 <?php namespace Illuminate\View; use Illuminate\Container\Container; 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(Container::getInstance()->make('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(Container::getInstance()->make('files')); }); } /** * Register the Blade engine implementation. * * @param \Illuminate\View\Engines\EngineResolver $resolver * @return void */ public function registerBladeEngine($resolver) { $resolver->register('blade', function () { $app = Container::getInstance(); $compiler = new CompilerEngine( $app->make('blade.compiler'), $app->make('files'), ); $app->terminating(static function () use ($compiler) { $compiler->forgetCompiledOrNotExpired(); }); return $compiler; }); } } framework/src/Illuminate/View/ViewName.php 0000644 00000001011 15060132305 0014555 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); } } framework/src/Illuminate/Auth/Authenticatable.php 0000644 00000004173 15060132305 0016142 0 ustar 00 <?php namespace Illuminate\Auth; trait Authenticatable { /** * The column name of the password field using during authentication. * * @var string */ protected $authPasswordName = 'password'; /** * 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 name of the password attribute for the user. * * @return string */ public function getAuthPasswordName() { return $this->authPasswordName; } /** * Get the password for the user. * * @return string */ public function getAuthPassword() { return $this->{$this->getAuthPasswordName()}; } /** * 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; } } framework/src/Illuminate/Auth/Middleware/RequirePassword.php 0000644 00000005531 15060132305 0020260 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); } } framework/src/Illuminate/Auth/Middleware/Authenticate.php 0000644 00000006410 15060132305 0017534 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; /** * The callback that should be used to generate the authentication redirect path. * * @var callable */ protected static $redirectToCallback; /** * 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, $request->expectsJson() ? null : $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) { if (static::$redirectToCallback) { return call_user_func(static::$redirectToCallback, $request); } } /** * Specify the callback that should be used to generate the redirect path. * * @param callable $redirectToCallback * @return void */ public static function redirectUsing(callable $redirectToCallback) { static::$redirectToCallback = $redirectToCallback; } } framework/src/Illuminate/Auth/Middleware/AuthenticateWithBasicAuth.php 0000644 00000002531 15060132305 0022154 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); } } framework/src/Illuminate/Auth/Middleware/EnsureEmailIsVerified.php 0000644 00000002257 15060132305 0021306 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); } } framework/src/Illuminate/Auth/Middleware/RedirectIfAuthenticated.php 0000644 00000004104 15060132305 0021637 0 ustar 00 <?php namespace Illuminate\Auth\Middleware; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Route; use Symfony\Component\HttpFoundation\Response; class RedirectIfAuthenticated { /** * The callback that should be used to generate the authentication redirect path. * * @var callable|null */ protected static $redirectToCallback; /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next, string ...$guards): Response { $guards = empty($guards) ? [null] : $guards; foreach ($guards as $guard) { if (Auth::guard($guard)->check()) { return redirect($this->redirectTo($request)); } } return $next($request); } /** * Get the path the user should be redirected to when they are authenticated. */ protected function redirectTo(Request $request): ?string { return static::$redirectToCallback ? call_user_func(static::$redirectToCallback, $request) : $this->defaultRedirectUri(); } /** * Get the default URI the user should be redirected to when they are authenticated. */ protected function defaultRedirectUri(): string { foreach (['dashboard', 'home'] as $uri) { if (Route::has($uri)) { return route($uri); } } $routes = Route::getRoutes()->get('GET'); foreach (['dashboard', 'home'] as $uri) { if (isset($routes[$uri])) { return '/'.$uri; } } return '/'; } /** * Specify the callback that should be used to generate the redirect path. * * @param callable $redirectToCallback * @return void */ public static function redirectUsing(callable $redirectToCallback) { static::$redirectToCallback = $redirectToCallback; } } framework/src/Illuminate/Auth/Middleware/Authorize.php 0000644 00000005067 15060132305 0017077 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 array */ 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, '\\'); } } framework/src/Illuminate/Auth/Listeners/SendEmailVerificationNotification.php 0000644 00000001016 15060132305 0023561 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(); } } } framework/src/Illuminate/Auth/Access/Events/GateEvaluated.php 0000644 00000001735 15060132305 0020226 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; } } framework/src/Illuminate/Auth/Access/HandlesAuthorization.php 0000644 00000002506 15060132305 0020403 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); } } framework/src/Illuminate/Auth/Access/AuthorizationException.php 0000644 00000004273 15060132305 0020766 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); } } framework/src/Illuminate/Auth/Access/Response.php 0000644 00000010440 15060132305 0016036 0 ustar 00 <?php namespace Illuminate\Auth\Access; use Illuminate\Contracts\Support\Arrayable; use Stringable; class Response implements Arrayable, Stringable { /** * 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(); } } framework/src/Illuminate/Auth/Access/Gate.php 0000644 00000062175 15060132305 0015134 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 all of the given abilities should be granted for the current user. * * @param iterable|string $ability * @param array|mixed $arguments * @return bool */ public function allows($ability, $arguments = []) { return $this->check($ability, $arguments); } /** * Determine if any of the given abilities should be denied for the current user. * * @param iterable|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; } } framework/src/Illuminate/Auth/Passwords/PasswordBrokerManager.php 0000644 00000007212 15060132305 0021271 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), $this->app['events'] ?? 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); } } framework/src/Illuminate/Auth/Passwords/PasswordResetServiceProvider.php 0000755 00000001762 15060132305 0022677 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']; } } framework/src/Illuminate/Auth/Passwords/DatabaseTokenRepository.php 0000755 00000014075 15060132305 0021644 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; } } framework/src/Illuminate/Auth/Passwords/PasswordBroker.php 0000755 00000013663 15060132305 0020010 0 ustar 00 <?php namespace Illuminate\Auth\Passwords; use Closure; use Illuminate\Auth\Events\PasswordResetLinkSent; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Events\Dispatcher; 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; /** * The event dispatcher instance. * * @var \Illuminate\Contracts\Events\Dispatcher */ protected $events; /** * Create a new password broker instance. * * @param \Illuminate\Auth\Passwords\TokenRepositoryInterface $tokens * @param \Illuminate\Contracts\Auth\UserProvider $users * @param \Illuminate\Contracts\Events\Dispatcher $users * @return void */ public function __construct(TokenRepositoryInterface $tokens, UserProvider $users, ?Dispatcher $dispatcher = null) { $this->users = $users; $this->tokens = $tokens; $this->events = $dispatcher; } /** * 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); if ($this->events) { $this->events->dispatch(new PasswordResetLinkSent($user)); } 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; } } framework/src/Illuminate/Auth/Passwords/CanResetPassword.php 0000644 00000001132 15060132305 0020251 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)); } } framework/src/Illuminate/Auth/Passwords/TokenRepositoryInterface.php 0000755 00000002252 15060132305 0022032 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(); } framework/src/Illuminate/Auth/MustVerifyEmail.php 0000644 00000001723 15060132305 0016130 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; } } framework/src/Illuminate/Auth/Events/Logout.php 0000644 00000001207 15060132305 0015555 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; } } framework/src/Illuminate/Auth/Events/Lockout.php 0000644 00000000654 15060132305 0015731 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; } } framework/src/Illuminate/Auth/Events/Authenticated.php 0000644 00000001216 15060132305 0017066 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; } } framework/src/Illuminate/Auth/Events/PasswordReset.php 0000644 00000000727 15060132305 0017117 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; } } framework/src/Illuminate/Auth/Events/Failed.php 0000644 00000001475 15060132305 0015477 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; } } framework/src/Illuminate/Auth/Events/Attempting.php 0000644 00000001352 15060132305 0016421 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; } } framework/src/Illuminate/Auth/Events/PasswordResetLinkSent.php 0000644 00000000752 15060132305 0020565 0 ustar 00 <?php namespace Illuminate\Auth\Events; use Illuminate\Queue\SerializesModels; class PasswordResetLinkSent { use SerializesModels; /** * The user instance. * * @var \Illuminate\Contracts\Auth\CanResetPassword */ public $user; /** * Create a new event instance. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @return void */ public function __construct($user) { $this->user = $user; } } framework/src/Illuminate/Auth/Events/Login.php 0000644 00000001511 15060132305 0015352 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; } } framework/src/Illuminate/Auth/Events/CurrentDeviceLogout.php 0000644 00000001224 15060132305 0020237 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; } } framework/src/Illuminate/Auth/Events/Verified.php 0000644 00000000733 15060132305 0016044 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; } } framework/src/Illuminate/Auth/Events/Registered.php 0000644 00000000742 15060132305 0016404 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; } } framework/src/Illuminate/Auth/Events/OtherDeviceLogout.php 0000644 00000001222 15060132305 0017674 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; } } framework/src/Illuminate/Auth/Events/Validated.php 0000644 00000001253 15060132305 0016202 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; } } framework/src/Illuminate/Auth/RequestGuard.php 0000644 00000004113 15060132305 0015452 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; } } framework/src/Illuminate/Auth/EloquentUserProvider.php 0000755 00000015424 15060132305 0017217 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()); } /** * Rehash the user's password if required and supported. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @param bool $force * @return void */ public function rehashPasswordIfRequired(UserContract $user, array $credentials, bool $force = false) { if (! $this->hasher->needsRehash($user->getAuthPassword()) && ! $force) { return; } $user->forceFill([ $user->getAuthPasswordName() => $this->hasher->make($credentials['password']), ])->save(); } /** * 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; } } framework/src/Illuminate/Auth/LICENSE.md 0000644 00000002063 15060132305 0013734 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. framework/src/Illuminate/Auth/SessionGuard.php 0000644 00000066623 15060132305 0015463 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 passwords should be rehashed on login if needed. * * @var bool */ protected $rehashOnLogin; /** * 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 * @param bool $rehashOnLogin * @return void */ public function __construct($name, UserProvider $provider, Session $session, ?Request $request = null, ?Timebox $timebox = null, bool $rehashOnLogin = true) { $this->name = $name; $this->session = $session; $this->request = $request; $this->provider = $provider; $this->timebox = $timebox ?: new Timebox; $this->rehashOnLogin = $rehashOnLogin; } /** * 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->rehashPasswordIfRequired($this->lastAttempted, $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->rehashPasswordIfRequired($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->rehashPasswordIfRequired($user, $credentials); $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; } /** * Rehash the user's password if enabled and required. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @return void */ protected function rehashPasswordIfRequired(AuthenticatableContract $user, array $credentials) { if ($this->rehashOnLogin) { $this->provider->rehashPasswordIfRequired($user, $credentials); } } /** * 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 * @return \Illuminate\Contracts\Auth\Authenticatable|null * * @throws \Illuminate\Auth\AuthenticationException */ public function logoutOtherDevices($password) { if (! $this->user()) { return; } $result = $this->rehashUserPasswordForDeviceLogout($password); if ($this->recaller() || $this->getCookieJar()->hasQueued($this->getRecallerName())) { $this->queueRecallerCookie($this->user()); } $this->fireOtherDeviceLogoutEvent($this->user()); return $result; } /** * Rehash the current user's password for logging out other devices via AuthenticateSession. * * @param string $password * @return \Illuminate\Contracts\Auth\Authenticatable|null * * @throws \InvalidArgumentException */ protected function rehashUserPasswordForDeviceLogout($password) { $user = $this->user(); if (! Hash::check($password, $user->getAuthPassword())) { throw new InvalidArgumentException('The given password does not match the current password.'); } $this->provider->rehashPasswordIfRequired( $user, ['password' => $password], force: true ); } /** * 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; } } framework/src/Illuminate/Auth/DatabaseUserProvider.php 0000755 00000012433 15060132305 0017124 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() ); } /** * Rehash the user's password if required and supported. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @param bool $force * @return void */ public function rehashPasswordIfRequired(UserContract $user, array $credentials, bool $force = false) { if (! $this->hasher->needsRehash($user->getAuthPassword()) && ! $force) { return; } $this->connection->table($this->table) ->where($user->getAuthIdentifierName(), $user->getAuthIdentifier()) ->update([$user->getAuthPasswordName() => $this->hasher->make($credentials['password'])]); } } framework/src/Illuminate/Auth/GuardHelpers.php 0000644 00000004754 15060132305 0015437 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() { return $this->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; } } framework/src/Illuminate/Auth/Notifications/VerifyEmail.php 0000644 00000006063 15060132305 0020072 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; } } framework/src/Illuminate/Auth/Notifications/ResetPassword.php 0000644 00000006762 15060132305 0020471 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|\Illuminate\Contracts\Mail\Mailable)|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|\Illuminate\Contracts\Mail\Mailable) $callback * @return void */ public static function toMailUsing($callback) { static::$toMailCallback = $callback; } } framework/src/Illuminate/Auth/AuthenticationException.php 0000644 00000003473 15060132305 0017705 0 ustar 00 <?php namespace Illuminate\Auth; use Exception; use Illuminate\Http\Request; 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; /** * The callback that should be used to generate the authentication redirect path. * * @var callable */ protected static $redirectToCallback; /** * 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. * * @param \Illuminate\Http\Request $request * @return string|null */ public function redirectTo(Request $request) { if ($this->redirectTo) { return $this->redirectTo; } if (static::$redirectToCallback) { return call_user_func(static::$redirectToCallback, $request); } } /** * Specify the callback that should be used to generate the redirect path. * * @param callable $redirectToCallback * @return void */ public static function redirectUsing(callable $redirectToCallback) { static::$redirectToCallback = $redirectToCallback; } } framework/src/Illuminate/Auth/GenericUser.php 0000755 00000005307 15060132305 0015263 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 name of the password attribute for the user. * * @return string */ public function getAuthPasswordName() { return 'password'; } /** * Get the password for the user. * * @return string */ public function getAuthPassword() { return $this->attributes[$this->getAuthPasswordName()]; } /** * 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]); } } framework/src/Illuminate/Auth/AuthManager.php 0000755 00000020533 15060132305 0015242 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'], rehashOnLogin: $this->app['config']->get('hashing.rehash_on_login', true), ); // 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); } } framework/src/Illuminate/Auth/composer.json 0000644 00000002362 15060132305 0015054 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.2", "ext-hash": "*", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/http": "^11.0", "illuminate/macroable": "^11.0", "illuminate/queue": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Auth\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "illuminate/console": "Required to use the auth:clear-resets command (^11.0).", "illuminate/queue": "Required to fire login / logout events (^11.0).", "illuminate/session": "Required to use the session based guard (^11.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Auth/AuthServiceProvider.php 0000755 00000006046 15060132305 0017006 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); } }); } } framework/src/Illuminate/Auth/Console/stubs/make/views/layouts/app.stub 0000644 00000006443 15060132305 0022331 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> framework/src/Illuminate/Auth/Console/ClearResetsCommand.php 0000644 00000001532 15060132305 0020156 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.'); } } framework/src/Illuminate/Auth/Recaller.php 0000644 00000003526 15060132305 0014577 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); } } framework/src/Illuminate/Auth/TokenGuard.php 0000644 00000006570 15060132305 0015113 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|null */ 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; } } framework/src/Illuminate/Auth/CreatesUserProviders.php 0000644 00000004652 15060132305 0017172 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']; } } framework/src/Illuminate/Collections/LICENSE.md 0000644 00000002063 15060132305 0015311 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. framework/src/Illuminate/Collections/ItemNotFoundException.php 0000644 00000000166 15060132305 0020652 0 ustar 00 <?php namespace Illuminate\Support; use RuntimeException; class ItemNotFoundException extends RuntimeException { } framework/src/Illuminate/Collections/LazyCollection.php 0000644 00000141500 15060132305 0017351 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 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($items, callable $callback) { 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; } } } } }); } /** * Select specific values from the items within the collection. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys * @return static */ public function select($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 { foreach ($this as $item) { $result = []; foreach ($keys as $key) { if (Arr::accessible($item) && Arr::exists($item, $key)) { $result[$key] = $item[$key]; } elseif (is_object($item) && isset($item->{$key})) { $result[$key] = $item->{$key}; } } yield $result; } } }); } /** * Push all of the given items onto the collection. * * @template TConcatKey of array-key * @template TConcatValue * * @param iterable<TConcatKey, TConcatValue> $source * @return static<TKey|TConcatKey, TValue|TConcatValue> */ 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. * * @return static */ public function shuffle() { return $this->passthru('shuffle', []); } /** * 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; } }); } /** * Throttle the values, releasing them at most once per the given seconds. * * @return static<TKey, TValue> */ public function throttle(float $seconds) { return new static(function () use ($seconds) { $microseconds = $seconds * 1_000_000; foreach ($this as $key => $value) { $fetchedAt = $this->preciseNow(); yield $key => $value; $sleep = $microseconds - ($this->preciseNow() - $fetchedAt); $this->usleep((int) $sleep); } }); } /** * 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 class_exists(Carbon::class) ? Carbon::now()->timestamp : time(); } /** * Get the precise current time. * * @return float */ protected function preciseNow() { return class_exists(Carbon::class) ? Carbon::now()->getPreciseTimestamp() : microtime(true) * 1_000_000; } /** * Sleep for the given amount of microseconds. * * @return void */ protected function usleep(int $microseconds) { if ($microseconds <= 0) { return; } class_exists(Sleep::class) ? Sleep::usleep($microseconds) : usleep($microseconds); } } framework/src/Illuminate/Collections/Traits/EnumeratesValues.php 0000644 00000077212 15060132305 0021164 0 ustar 00 <?php namespace Illuminate\Support\Traits; use BackedEnum; 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 InvalidArgumentException; use JsonSerializable; use Traversable; use UnexpectedValueException; use UnitEnum; use WeakMap; /** * @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); } /** * 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); $reduced = $this->reduce(static function (&$reduce, $value) use ($callback) { if (! is_null($resolved = $callback($value))) { $reduce[0] += $resolved; $reduce[1]++; } return $reduce; }, [0, 0]); return $reduced[1] ? $reduced[0] / $reduced[1] : null; } /** * 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 given arguments and terminate execution. * * @param mixed ...$args * @return never */ public function dd(...$args) { $this->dump(...$args); dd(); } /** * Dump the items. * * @param mixed ...$args * @return $this */ public function dump(...$args) { dump($this->all(), ...$args); 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>|array<array-key, class-string<TEnsureOfType>> $type * @return static<TKey, TEnsureOfType> * * @throws \UnexpectedValueException */ public function ensure($type) { $allowedTypes = is_array($type) ? $type : [$type]; return $this->each(function ($item, $index) use ($allowedTypes) { $itemType = get_debug_type($item); foreach ($allowedTypes as $allowedType) { if ($itemType === $allowedType || $item instanceof $allowedType) { return true; } } throw new UnexpectedValueException( sprintf("Collection should only include [%s] items, but '%s' found at position %d.", implode(', ', $allowedTypes), $itemType, $index) ); }); } /** * 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) { if (is_subclass_of($class, BackedEnum::class)) { return $this->map(fn ($value, $key) => $class::from($value)); } 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 WeakMap => throw new InvalidArgumentException('Collections can not be created using instances of WeakMap.'), $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; } } framework/src/Illuminate/Collections/Arr.php 0000644 00000056440 15060132305 0015152 0 ustar 00 <?php namespace Illuminate\Support; use ArgumentCountError; use ArrayAccess; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use Random\Randomizer; 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. * * @template TKey * @template TValue * @template TFirstDefault * * @param iterable<TKey, TValue> $array * @param (callable(TValue, TKey): bool)|null $callback * @param TFirstDefault|(\Closure(): TFirstDefault) $default * @return TValue|TFirstDefault */ 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; } return value($default); } 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); } /** * Take the first or last {$limit} items from an array. * * @param array $array * @param int $limit * @return array */ public static function take($array, $limit) { if ($limit < 0) { return array_slice($array, $limit, abs($limit)); } return array_slice($array, 0, $limit); } /** * 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 static::mapWithKeys($array, fn ($item, $key) => [$prependWith.$key => $item]); } /** * 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)); } /** * Select an array of values from an array. * * @param array $array * @param array|string $keys * @return array */ public static function select($array, $keys) { $keys = static::wrap($keys); return static::map($array, function ($item) use ($keys) { $result = []; foreach ($keys as $key) { if (Arr::accessible($item) && Arr::exists($item, $key)) { $result[$key] = $item[$key]; } elseif (is_object($item) && isset($item->{$key})) { $result[$key] = $item->{$key}; } } return $result; }); } /** * 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; } /** * Run a map over each nested chunk of items. * * @template TMapSpreadValue * * @param array $array * @param callable(mixed...): TMapSpreadValue $callback * @return array<TKey, TMapSpreadValue> */ public static function mapSpread(array $array, callable $callback) { return static::map($array, function ($chunk, $key) use ($callback) { $chunk[] = $key; return $callback(...$chunk); }); } /** * 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 (empty($array) || (! is_null($number) && $number <= 0)) { return is_null($number) ? null : []; } $keys = (new Randomizer)->pickArrayKeys($array, $requested); if (is_null($number)) { return $array[$keys[0]]; } $results = []; if ($preserveKeys) { foreach ($keys as $key) { $results[$key] = $array[$key]; } } else { foreach ($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 * @return array */ public static function shuffle($array) { return (new Randomizer)->shuffleArray($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]; } } framework/src/Illuminate/Collections/composer.json 0000644 00000002004 15060132305 0016422 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.2", "illuminate/conditionable": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" }, "files": [ "helpers.php" ] }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "symfony/var-dumper": "Required to use the dump method (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Collections/helpers.php 0000644 00000014646 15060132305 0016072 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; } $segment = match ($segment) { '\*' => '*', '\{first}' => '{first}', '{first}' => array_key_first(is_array($target) ? $target : collect($target)->all()), '\{last}' => '{last}', '{last}' => array_key_last(is_array($target) ? $target : collect($target)->all()), default => $segment, }; 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; } } framework/src/Illuminate/Collections/Collection.php 0000644 00000135773 15060132305 0016530 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 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>|null $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)); } /** * Select specific values from the items within the collection. * * @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string|null $keys * @return static */ public function select($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::select($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; } /** * Prepend one or more items to the beginning of the collection. * * @param TValue ...$values * @return $this */ public function unshift(...$values) { array_unshift($this->items, ...$values); return $this; } /** * Push all of the given items onto the collection. * * @template TConcatKey of array-key * @template TConcatValue * * @param iterable<TConcatKey, TConcatValue> $source * @return static<TKey|TConcatKey, TValue|TConcatValue> */ 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. * * @return static */ public function shuffle() { return new static(Arr::shuffle($this->items)); } /** * 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, $options); } $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 * @param int $options * @return static */ protected function sortByMany(array $comparisons = [], int $options = SORT_REGULAR) { $items = $this->items; uasort($items, function ($a, $b) use ($comparisons, $options) { 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); } if (($options & SORT_FLAG_CASE) === SORT_FLAG_CASE) { if (($options & SORT_NATURAL) === SORT_NATURAL) { $result = strnatcasecmp($values[0], $values[1]); } else { $result = strcasecmp($values[0], $values[1]); } } else { $result = match ($options) { SORT_NUMERIC => intval($values[0]) <=> intval($values[1]), SORT_STRING => strcmp($values[0], $values[1]), SORT_NATURAL => strnatcmp($values[0], $values[1]), SORT_LOCALE_STRING => strcoll($values[0], $values[1]), default => $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) { if (is_array($callback) && ! is_callable($callback)) { foreach ($callback as $index => $key) { $comparison = Arr::wrap($key); $comparison[1] = 'desc'; $callback[$index] = $comparison; } } 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]); } } framework/src/Illuminate/Collections/Enumerable.php 0000644 00000106707 15060132305 0016507 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. * * @param mixed ...$args * @return $this */ public function dump(...$args); /** * 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, 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); /** * 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); /** * 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); /** * 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. * * @template TConcatKey of array-key * @template TConcatValue * * @param iterable<TConcatKey, TConcatValue> $source * @return static<TKey|TConcatKey, TValue|TConcatValue> */ 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. * * @return static */ public function shuffle(); /** * 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); } framework/src/Illuminate/Collections/MultipleItemsFoundException.php 0000644 00000001340 15060132305 0022063 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; } } framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php 0000644 00000002600 15060132305 0021673 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); }); } } framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php 0000644 00000001712 15060132305 0022626 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; } } framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php 0000644 00000014661 15060132305 0020376 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 Illuminate\Support\Arr; 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 = []; /** * The globally ignored cookies that should not be encrypted. * * @var array */ protected static $neverEncrypt = []; /** * 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->getAllKeys()); } /** * 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 $cookie->withValue($value); } /** * Determine whether encryption has been disabled for the given cookie. * * @param string $name * @return bool */ public function isDisabled($name) { return in_array($name, array_merge($this->except, static::$neverEncrypt)); } /** * Indicate that the given cookies should never be encrypted. * * @param array|string $cookies * @return void */ public static function except($cookies) { static::$neverEncrypt = array_values(array_unique( array_merge(static::$neverEncrypt, Arr::wrap($cookies)) )); } /** * Determine if the cookie contents should be serialized. * * @param string $name * @return bool */ public static function serialized($name) { return static::$serialize; } /** * Flush the middleware's global state. * * @return void */ public static function flushState() { static::$neverEncrypt = []; static::$serialize = false; } } framework/src/Illuminate/Cookie/CookieJar.php 0000755 00000014354 15060132305 0015230 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; } } framework/src/Illuminate/Cookie/LICENSE.md 0000644 00000002063 15060132305 0014244 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. framework/src/Illuminate/Cookie/CookieValuePrefix.php 0000644 00000002273 15060132305 0016740 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 array $keys * @return string|null */ public static function validate($cookieName, $cookieValue, array $keys) { foreach ($keys as $key) { $hasValidPrefix = str_starts_with($cookieValue, static::create($cookieName, $key)); if ($hasValidPrefix) { return static::remove($cookieValue); } } } } framework/src/Illuminate/Cookie/composer.json 0000755 00000001761 15060132305 0015371 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.2", "ext-hash": "*", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0", "symfony/http-foundation": "^7.0", "symfony/http-kernel": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Cookie\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Cookie/CookieServiceProvider.php 0000755 00000001065 15060132305 0017622 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 ); }); } } framework/src/Illuminate/Filesystem/functions.php 0000644 00000001166 15060132305 0016277 0 ustar 00 <?php namespace Illuminate\Filesystem; if (! function_exists('Illuminate\Filesystem\join_paths')) { /** * Join the given paths together. * * @param string|null $basePath * @param string ...$paths * @return string */ function join_paths($basePath, ...$paths) { foreach ($paths as $index => $path) { if (empty($path) && $path !== '0') { unset($paths[$index]); } else { $paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR); } } return $basePath.implode('', $paths); } } framework/src/Illuminate/Filesystem/Filesystem.php 0000644 00000046737 15060132305 0016430 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, 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 * @param bool $lock * @return int */ public function append($path, $data, $lock = false) { return file_put_contents($path, $data, FILE_APPEND | ($lock ? LOCK_EX : 0)); } /** * 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 bool|null */ 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_equals($hash, (string) @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); } } framework/src/Illuminate/Filesystem/AwsS3V3Adapter.php 0000644 00000010236 15060132305 0016737 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; } } framework/src/Illuminate/Filesystem/LICENSE.md 0000644 00000002063 15060132305 0015157 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. framework/src/Illuminate/Filesystem/FilesystemManager.php 0000644 00000026631 15060132305 0017712 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', 'retain_visibility', '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); } } framework/src/Illuminate/Filesystem/LockableFile.php 0000644 00000007022 15060132305 0016600 0 ustar 00 <?php namespace Illuminate\Filesystem; 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); } /** * 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 $this * * @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); } } framework/src/Illuminate/Filesystem/FilesystemAdapter.php 0000644 00000062532 15060132305 0017720 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 to the file that exists at the given relative 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; $headers['Content-Type'] ??= $this->mimeType($path); $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); } } framework/src/Illuminate/Filesystem/composer.json 0000644 00000003601 15060132305 0016274 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.2", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0", "symfony/finder": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Filesystem\\": "" }, "files": [ "functions.php" ] }, "extra": { "branch-alias": { "dev-master": "11.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 (^7.0).", "symfony/mime": "Required to enable support for guessing extensions (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Filesystem/FilesystemServiceProvider.php 0000644 00000003322 15060132305 0021443 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']; } } framework/src/Illuminate/Process/ProcessResult.php 0000644 00000006140 15060132305 0016373 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 * * @throws \Illuminate\Process\Exceptions\ProcessFailedException */ 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 * * @throws \Throwable */ public function throwIf(bool $condition, ?callable $callback = null) { if ($condition) { return $this->throw($callback); } return $this; } } framework/src/Illuminate/Process/InvokedProcess.php 0000644 00000005146 15060132305 0016521 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 * * @throws \Illuminate\Process\Exceptions\ProcessTimedOutException */ 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)); } } } framework/src/Illuminate/Process/FakeProcessSequence.php 0000644 00000006635 15060132305 0017465 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); } } framework/src/Illuminate/Process/FakeProcessResult.php 0000644 00000010703 15060132305 0017162 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 * * @throws \Illuminate\Process\Exceptions\ProcessFailedException */ 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 * * @throws \Throwable */ public function throwIf(bool $condition, ?callable $callback = null) { if ($condition) { return $this->throw($callback); } return $this; } } framework/src/Illuminate/Process/LICENSE.md 0000644 00000002063 15060132305 0014451 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. framework/src/Illuminate/Process/ProcessPoolResults.php 0000644 00000003047 15060132305 0017413 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]); } } framework/src/Illuminate/Process/FakeProcessDescription.php 0000644 00000012040 15060132305 0020163 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" : ''; } } framework/src/Illuminate/Process/FakeInvokedProcess.php 0000644 00000016260 15060132305 0017307 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 $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 $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 $outputHandler * @return $this */ public function withOutputHandler(?callable $outputHandler) { $this->outputHandler = $outputHandler; return $this; } } framework/src/Illuminate/Process/PendingProcess.php 0000644 00000026215 15060132305 0016506 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 * * @throws \Illuminate\Process\Exceptions\ProcessTimedOutException * @throws \RuntimeException */ public function run(array|string|null $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|null $output * @return \Illuminate\Process\InvokedProcess * * @throws \RuntimeException */ public function start(array|string|null $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) => $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 * * @throw \LogicException */ 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.'); } } framework/src/Illuminate/Process/composer.json 0000644 00000001654 15060132305 0015574 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.2", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0", "symfony/process": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Process\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Process/Pipe.php 0000644 00000005432 15060132305 0014456 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; }); } } framework/src/Illuminate/Process/Factory.php 0000644 00000020574 15060132305 0015174 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|null $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); } } framework/src/Illuminate/Process/Pool.php 0000644 00000005713 15060132305 0014474 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; }); } } framework/src/Illuminate/Process/InvokedProcessPool.php 0000644 00000002667 15060132305 0017360 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); } } framework/src/Illuminate/Process/Exceptions/ProcessTimedOutException.php 0000644 00000001613 15060132305 0022647 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); } } framework/src/Illuminate/Process/Exceptions/ProcessFailedException.php 0000644 00000002063 15060132305 0022301 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); } } framework/src/Illuminate/Validation/Rule.php 0000644 00000013722 15060132305 0015145 0 ustar 00 <?php namespace Illuminate\Validation; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Traits\Macroable; use Illuminate\Validation\Rules\ArrayRule; 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); } /** * Apply the given rules if the given condition is truthy. * * @param callable|bool $condition * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string $rules * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string $defaultRules * @return \Illuminate\Validation\ConditionalRules */ public static function when($condition, $rules, $defaultRules = []) { return new ConditionalRules($condition, $rules, $defaultRules); } /** * Apply the given rules if the given condition is falsy. * * @param callable|bool $condition * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string $rules * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string $defaultRules * @return \Illuminate\Validation\ConditionalRules */ public static function unless($condition, $rules, $defaultRules = []) { return new ConditionalRules($condition, $defaultRules, $rules); } /** * Get an array rule builder instance. * * @param array|null $keys * @return \Illuminate\Validation\Rules\ArrayRule */ public static function array($keys = null) { return new ArrayRule(...func_get_args()); } /** * 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 rule builder instance. * * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|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 rule builder instance. * * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|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 rule builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\RequiredIf */ public static function requiredIf($callback) { return new RequiredIf($callback); } /** * Get a exclude_if rule builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\ExcludeIf */ public static function excludeIf($callback) { return new ExcludeIf($callback); } /** * Get a prohibited_if rule builder instance. * * @param callable|bool $callback * @return \Illuminate\Validation\Rules\ProhibitedIf */ public static function prohibitedIf($callback) { return new ProhibitedIf($callback); } /** * Get an enum rule builder instance. * * @param class-string $type * @return \Illuminate\Validation\Rules\Enum */ public static function enum($type) { return new Enum($type); } /** * Get a file rule builder instance. * * @return \Illuminate\Validation\Rules\File */ public static function file() { return new File; } /** * Get an image file rule builder instance. * * @return \Illuminate\Validation\Rules\ImageFile */ public static function imageFile() { return new ImageFile; } /** * Get a dimensions rule builder instance. * * @param array $constraints * @return \Illuminate\Validation\Rules\Dimensions */ public static function dimensions(array $constraints = []) { return new Dimensions($constraints); } } framework/src/Illuminate/Validation/ClosureValidationRule.php 0000644 00000003670 15060132305 0020516 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); }, $this->validator); 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; } } framework/src/Illuminate/Validation/Concerns/ReplacesAttributes.php 0000644 00000064767 15060132305 0021634 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 extensions rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replaceExtensions($message, $attribute, $rule, $parameters) { return str_replace(':values', implode(', ', $parameters), $message); } /** * 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 present_if rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replacePresentIf($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 present_unless rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replacePresentUnless($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 present_with rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replacePresentWith($message, $attribute, $rule, $parameters) { return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message); } /** * Replace all place-holders for the present_with_all rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ protected function replacePresentWithAll($message, $attribute, $rule, $parameters) { return $this->replacePresentWith($message, $attribute, $rule, $parameters); } /** * 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_if_declined rule. * * @param string $message * @param string $attribute * @param string $rule * @param array<int,string> $parameters * @return string */ public function replaceRequiredIfDeclined($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); } } framework/src/Illuminate/Validation/Concerns/FormatsMessages.php 0000644 00000041105 15060132305 0021107 0 ustar 00 <?php namespace Illuminate\Validation\Concerns; use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\File\File; 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. return match (true) { $this->hasRule($attribute, $this->numericRules) => 'numeric', $this->hasRule($attribute, ['Array']) => 'array', $this->getValue($attribute) instanceof UploadedFile, $this->getValue($attribute) instanceof File => 'file', default => '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)); } } framework/src/Illuminate/Validation/Concerns/FilterEmailValidation.php 0000644 00000003206 15060132305 0022214 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 []; } } framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php 0000644 00000227106 15060132305 0021776 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\Container\Container; 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 attribute is a list. * * @param string $attribute * @param mixed $value * @return bool */ public function validateList($attribute, $value) { return is_array($value) && array_is_list($value); } /** * 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 an attribute contains a list of values. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateContains($attribute, $value, $parameters) { if (! is_array($value)) { return false; } foreach ($parameters as $parameter) { if (! in_array($parameter, $value)) { return false; } } return true; } /** * 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) / 2, $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]; $emailValidator = Container::getInstance()->make(EmailValidator::class); return $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 extension of a file upload attribute is in a set of defined extensions. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validateExtensions($attribute, $value, $parameters) { if (! $this->isValidFileInstance($value)) { return false; } if ($this->shouldBlockPhpUpload($value, $parameters)) { return false; } return in_array(strtolower($value->getClientOriginalExtension()), $parameters); } /** * 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 that an attribute is a valid HEX color. * * @param string $attribute * @param mixed $value * @return bool */ public function validateHexColor($attribute, $value) { return preg_match('/^#(?:(?:[0-9a-f]{3}){1,2}|(?:[0-9a-f]{4}){1,2})$/i', $value) === 1; } /** * 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) || is_null($value)) { return false; } if (! is_scalar($value) && ! method_exists($value, '__toString')) { return false; } if (function_exists('json_validate')) { return json_validate($value); } json_decode($value); return json_last_error() === JSON_ERROR_NONE; } /** * Validate the size of an attribute is less than or equal to 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 or equal to 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 is present when another attribute has a given value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validatePresentIf($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'present_if'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validatePresent($attribute, $value); } return true; } /** * Validate that an attribute is present unless another attribute has a given value. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validatePresentUnless($attribute, $value, $parameters) { $this->requireParameterCount(2, $parameters, 'present_unless'); [$values, $other] = $this->parseDependentRuleParameters($parameters); if (! in_array($other, $values, is_bool($other) || is_null($other))) { return $this->validatePresent($attribute, $value); } return true; } /** * Validate that an attribute is present when any given attribute is present. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validatePresentWith($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'present_with'); if (Arr::hasAny($this->data, $parameters)) { return $this->validatePresent($attribute, $value); } return true; } /** * Validate that an attribute is present when all given attributes are present. * * @param string $attribute * @param mixed $value * @param array<int, int|string> $parameters * @return bool */ public function validatePresentWithAll($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'present_with_all'); if (Arr::has($this->data, $parameters)) { return $this->validatePresent($attribute, $value); } return true; } /** * 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 exists when another attribute was "declined". * * @param string $attribute * @param mixed $value * @param mixed $parameters * @return bool */ public function validateRequiredIfDeclined($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'required_if_declined'); if ($this->validateDeclined($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; } } framework/src/Illuminate/Validation/Validator.php 0000755 00000122340 15060132305 0016163 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', 'Extensions', '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', 'PresentIf', 'PresentUnless', 'PresentWith', 'PresentWithAll', 'Required', 'RequiredIf', 'RequiredIfAccepted', 'RequiredIfDeclined', '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', 'RequiredIfDeclined', 'RequiredUnless', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll', 'PresentIf', 'PresentUnless', 'PresentWith', 'PresentWithAll', 'Prohibited', 'ProhibitedIf', 'ProhibitedUnless', 'Prohibits', 'MissingIf', 'MissingUnless', 'MissingWith', 'MissingWithAll', '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 default numeric related validation rules. * * @var string[] */ protected $defaultNumericRules = ['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() { if (! $this->messages) { $this->passes(); } throw_if($this->messages->isNotEmpty(), $this->exception, $this); $results = []; $missingValue = new stdClass; foreach ($this->getRules() as $key => $rules) { $value = data_get($this->getData(), $key, $missingValue); if ($this->excludeUnvalidatedArrayKeys && in_array('array', $rules) && $value !== null && ! empty(preg_grep('/^'.preg_quote($key, '/').'\.+/', array_keys($this->getRules())))) { continue; } 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}"; $this->numericRules = $this->defaultNumericRules; 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 */ public 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 )); } } framework/src/Illuminate/Validation/LICENSE.md 0000644 00000002063 15060132305 0015125 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. framework/src/Illuminate/Validation/DatabasePresenceVerifier.php 0000755 00000007144 15060132305 0021127 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; } } framework/src/Illuminate/Validation/NestedRules.php 0000644 00000002442 15060132305 0016470 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 * @param mixed $context * @return \stdClass */ public function compile($attribute, $value, $data = null, $context = null) { $rules = call_user_func($this->callback, $value, $attribute, $data, $context); $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)); } } framework/src/Illuminate/Validation/InvokableValidationRule.php 0000644 00000007537 15060132305 0021022 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; } } framework/src/Illuminate/Validation/DatabasePresenceVerifierInterface.php 0000755 00000000437 15060132305 0022746 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); } framework/src/Illuminate/Validation/Rules/Password.php 0000644 00000021130 15060132305 0017122 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; /** * The maximum size of the password. * * @var int */ protected $max; /** * 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; } /** * Set the minimum size of the password. * * @param int $size * @return $this */ public static function min($size) { return new static($size); } /** * Set the maximum size of the password. * * @param int $size * @return $this */ public function max($size) { $this->max = $size; return $this; } /** * 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 \Closure|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 => [ 'string', 'min:'.$this->min, ...($this->max ? ['max:'.$this->max] : []), ...$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->addFailure($attribute, 'password.mixed'); } if ($this->letters && ! preg_match('/\pL/u', $value)) { $validator->addFailure($attribute, 'password.letters'); } if ($this->symbols && ! preg_match('/\p{Z}|\p{S}|\p{P}/u', $value)) { $validator->addFailure($attribute, 'password.symbols'); } if ($this->numbers && ! preg_match('/\pN/u', $value)) { $validator->addFailure($attribute, '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, ])) { $validator->addFailure($attribute, 'password.uncompromised'); return $this->fail($validator->messages()->all()); } return true; } /** * Get the validation error message. * * @return array */ public function message() { return $this->messages; } /** * Adds the given failures, and return false. * * @param array|string $messages * @return bool */ protected function fail($messages) { $this->messages = array_merge($this->messages, Arr::wrap($messages)); return false; } } framework/src/Illuminate/Validation/Rules/Dimensions.php 0000644 00000004642 15060132305 0017441 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Support\Traits\Conditionable; use Stringable; class Dimensions implements Stringable { 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); } } framework/src/Illuminate/Validation/Rules/Can.php 0000644 00000003460 15060132305 0016027 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Support\Facades\Gate; class Can implements Rule, ValidatorAwareRule { /** * The ability to check. * * @var string */ protected $ability; /** * The arguments to pass to the authorization check. * * @var array */ protected $arguments; /** * The current validator instance. * * @var \Illuminate\Validation\Validator */ protected $validator; /** * 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 = $this->validator->getTranslator()->get('validation.can'); return $message === 'validation.can' ? ['The :attribute field contains an unauthorized value.'] : $message; } /** * Set the current validator. * * @param \Illuminate\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } } framework/src/Illuminate/Validation/Rules/In.php 0000644 00000002551 15060132305 0015674 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use BackedEnum; use Illuminate\Contracts\Support\Arrayable; use Stringable; use UnitEnum; class In implements Stringable { /** * The name of the rule. * * @var string */ protected $rule = 'in'; /** * The accepted values. * * @var array */ protected $values; /** * Create a new in rule instance. * * @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|array|string $values * @return void */ public function __construct($values) { if ($values instanceof Arrayable) { $values = $values->toArray(); } $this->values = is_array($values) ? $values : func_get_args(); } /** * 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); } } framework/src/Illuminate/Validation/Rules/RequiredIf.php 0000644 00000001747 15060132305 0017373 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use InvalidArgumentException; use Stringable; class RequiredIf implements Stringable { /** * 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' : ''; } } framework/src/Illuminate/Validation/Rules/ExcludeIf.php 0000644 00000002075 15060132305 0017177 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Closure; use InvalidArgumentException; use Stringable; class ExcludeIf implements Stringable { /** * 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' : ''; } } framework/src/Illuminate/Validation/Rules/ArrayRule.php 0000644 00000002153 15060132305 0017232 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use BackedEnum; use Illuminate\Contracts\Support\Arrayable; use Stringable; use UnitEnum; class ArrayRule implements Stringable { /** * The accepted keys. * * @var array */ protected $keys; /** * Create a new array rule instance. * * @param array|null $keys * @return void */ public function __construct($keys = null) { if ($keys instanceof Arrayable) { $keys = $keys->toArray(); } $this->keys = is_array($keys) ? $keys : func_get_args(); } /** * Convert the rule to a validation string. * * @return string */ public function __toString() { if (empty($this->keys)) { return 'array'; } $keys = array_map( static fn ($key) => match (true) { $key instanceof BackedEnum => $key->value, $key instanceof UnitEnum => $key->name, default => $key, }, $this->keys, ); return 'array:'.implode(',', $keys); } } framework/src/Illuminate/Validation/Rules/NotIn.php 0000644 00000002446 15060132305 0016360 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use BackedEnum; use Illuminate\Contracts\Support\Arrayable; use Stringable; use UnitEnum; class NotIn implements Stringable { /** * 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 \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\UnitEnum|array|string $values * @return void */ public function __construct($values) { if ($values instanceof Arrayable) { $values = $values->toArray(); } $this->values = is_array($values) ? $values : func_get_args(); } /** * 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); } } framework/src/Illuminate/Validation/Rules/Exists.php 0000644 00000000734 15060132305 0016606 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Support\Traits\Conditionable; use Stringable; class Exists implements Stringable { 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() ), ','); } } framework/src/Illuminate/Validation/Rules/Unique.php 0000644 00000003173 15060132305 0016575 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Traits\Conditionable; use Stringable; class Unique implements Stringable { 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() ), ','); } } framework/src/Illuminate/Validation/Rules/Enum.php 0000644 00000006244 15060132305 0016235 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Conditionable; use TypeError; class Enum implements Rule, ValidatorAwareRule { use Conditionable; /** * The type of the enum. * * @var class-string */ protected $type; /** * The current validator instance. * * @var \Illuminate\Validation\Validator */ protected $validator; /** * The cases that should be considered valid. * * @var array */ protected $only = []; /** * The cases that should be considered invalid. * * @var array */ protected $except = []; /** * Create a new rule instance. * * @param class-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 $this->isDesirable($value); } if (is_null($value) || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) { return false; } try { $value = $this->type::tryFrom($value); return ! is_null($value) && $this->isDesirable($value); } catch (TypeError) { return false; } } /** * Specify the cases that should be considered valid. * * @param \UnitEnum[]|\UnitEnum $values * @return $this */ public function only($values) { $this->only = Arr::wrap($values); return $this; } /** * Specify the cases that should be considered invalid. * * @param \UnitEnum[]|\UnitEnum $values * @return $this */ public function except($values) { $this->except = Arr::wrap($values); return $this; } /** * Determine if the given case is a valid case based on the only / except values. * * @param mixed $value * @return bool */ protected function isDesirable($value) { return match (true) { ! empty($this->only) => in_array(needle: $value, haystack: $this->only, strict: true), ! empty($this->except) => ! in_array(needle: $value, haystack: $this->except, strict: true), default => true, }; } /** * Get the validation error message. * * @return array */ public function message() { $message = $this->validator->getTranslator()->get('validation.enum'); return $message === 'validation.enum' ? ['The selected :attribute is invalid.'] : $message; } /** * Set the current validator. * * @param \Illuminate\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } } framework/src/Illuminate/Validation/Rules/DatabaseRule.php 0000644 00000012630 15060132305 0017661 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(','); } } framework/src/Illuminate/Validation/Rules/ImageFile.php 0000644 00000000774 15060132305 0017155 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; } } framework/src/Illuminate/Validation/Rules/File.php 0000644 00000022346 15060132305 0016211 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, Macroable; /** * The MIME types that the given file should match. This array may also contain file extensions. * * @var array */ protected $allowedMimetypes = []; /** * The extensions that the given file should match. * * @var array */ protected $allowedExtensions = []; /** * 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); } /** * Limit the uploaded file to the given file extensions. * * @param string|array<int, string> $extensions * @return $this */ public function extensions($extensions) { $this->allowedExtensions = (array) $extensions; return $this; } /** * 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()); if (! empty($this->allowedExtensions)) { $rules[] = 'extensions:'.implode(',', array_map('strtolower', $this->allowedExtensions)); } $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; } } framework/src/Illuminate/Validation/Rules/ProhibitedIf.php 0000644 00000002111 15060132305 0017666 0 ustar 00 <?php namespace Illuminate\Validation\Rules; use Closure; use InvalidArgumentException; use Stringable; class ProhibitedIf implements Stringable { /** * 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' : ''; } } framework/src/Illuminate/Validation/NotPwnedVerifier.php 0000644 00000005030 15060132305 0017461 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, ':'); }); } } framework/src/Illuminate/Validation/ValidationServiceProvider.php 0000755 00000004215 15060132305 0021364 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]; } } framework/src/Illuminate/Validation/ConditionalRules.php 0000644 00000004674 15060132305 0017522 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 \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string */ protected $rules; /** * The rules to be added to the attribute if the condition fails. * * @var \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string */ protected $defaultRules; /** * Create a new conditional rules instance. * * @param callable|bool $condition * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string $rules * @param \Illuminate\Contracts\Validation\ValidationRule|\Illuminate\Contracts\Validation\InvokableRule|\Illuminate\Contracts\Validation\Rule|\Closure|array|string $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)); } } framework/src/Illuminate/Validation/composer.json 0000755 00000002500 15060132305 0016242 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.2", "ext-filter": "*", "ext-mbstring": "*", "brick/math": "^0.9.3|^0.10.2|^0.11|^0.12", "egulias/email-validator": "^3.2.5|^4.0", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0", "illuminate/translation": "^11.0", "symfony/http-foundation": "^7.0", "symfony/mime": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Validation\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "illuminate/database": "Required to use the database presence verifier (^11.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Validation/UnauthorizedException.php 0000644 00000000200 15060132305 0020561 0 ustar 00 <?php namespace Illuminate\Validation; use RuntimeException; class UnauthorizedException extends RuntimeException { // } framework/src/Illuminate/Validation/PresenceVerifierInterface.php 0000755 00000001516 15060132305 0021320 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 = []); } framework/src/Illuminate/Validation/ValidationData.php 0000644 00000005554 15060132305 0017126 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; } } framework/src/Illuminate/Validation/ValidationException.php 0000644 00000007257 15060132305 0020215 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()->choice("(and :count more $pluralized)", $count, 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; } } framework/src/Illuminate/Validation/Factory.php 0000755 00000021625 15060132305 0015651 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; } } framework/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php 0000644 00000004237 15060132305 0021505 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; } } framework/src/Illuminate/Validation/ValidationRuleParser.php 0000644 00000022657 15060132305 0020344 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), $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) { $context = Arr::get($this->data, Str::beforeLast($key, '.')); $compiled = $rule->compile($key, $value, $data, $context); $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(); } } framework/src/Illuminate/Routing/CreatesRegularExpressionRouteConstraints.php 0000644 00000005061 15060132305 0024027 0 ustar 00 <?php namespace Illuminate\Routing; use BackedEnum; 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, collect($values) ->map(fn ($value) => $value instanceof BackedEnum ? $value->value : $value) ->implode('|') ); } /** * 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()); } } framework/src/Illuminate/Routing/Router.php 0000644 00000117454 15060132305 0015062 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 = []; /** * The registered custom implicit binding callback. * * @var array */ protected $implicitBindingCallback; /** * 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) { $default = fn () => ImplicitRouteBinding::resolveForRoute($this->container, $route); return call_user_func( $this->implicitBindingCallback ?? $default, $this->container, $route, $default ); } /** * Register a callback to run after implicit bindings are substituted. * * @param callable $callback * @return $this */ public function substituteImplicitBindingsUsing($callback) { $this->implicitBindingCallback = $callback; return $this; } /** * 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); } } framework/src/Illuminate/Routing/Middleware/ThrottleRequestsWithRedis.php 0000644 00000006633 15060132305 0023037 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->decaySeconds)) { 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 $decaySeconds * @return mixed */ protected function tooManyAttempts($key, $maxAttempts, $decaySeconds) { $limiter = new DurationLimiter( $this->getRedisConnection(), $key, $maxAttempts, $decaySeconds ); 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(); } } framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php 0000644 00000002250 15060132305 0021473 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); } } framework/src/Illuminate/Routing/Middleware/ValidateSignature.php 0000644 00000005211 15060132305 0021255 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 = [ // ]; /** * The globally ignored parameters. * * @var array */ protected static $neverValidate = []; /** * 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, array_merge($ignore, static::$neverValidate)]; } /** * Indicate that the given parameters should be ignored during signature validation. * * @param array|string $parameters * @return void */ public static function except($parameters) { static::$neverValidate = array_values(array_unique( array_merge(static::$neverValidate, Arr::wrap($parameters)) )); } } framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php 0000644 00000024603 15060132305 0021211 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\Routing\Exceptions\MissingRateLimiterException; 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 * @throws \Illuminate\Routing\Exceptions\MissingRateLimiterException */ 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), 'decaySeconds' => 60 * $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, 'decaySeconds' => $limit->decaySeconds, '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->decaySeconds); } $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 * * @throws \Illuminate\Routing\Exceptions\MissingRateLimiterException */ protected function resolveMaxAttempts($request, $maxAttempts) { if (str_contains($maxAttempts, '|')) { $maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0]; } if (! is_numeric($maxAttempts) && $request->user()?->hasAttribute($maxAttempts) ) { $maxAttempts = $request->user()->{$maxAttempts}; } // If we still don't have a numeric value, there was no matching rate limiter... if (! is_numeric($maxAttempts)) { is_null($request->user()) ? throw MissingRateLimiterException::forLimiter($maxAttempts) : throw MissingRateLimiterException::forLimiterAndUser($maxAttempts, get_class($request->user())); } 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; } } framework/src/Illuminate/Routing/RouteParameterBinder.php 0000644 00000006056 15060132305 0017660 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; } } framework/src/Illuminate/Routing/ViewController.php 0000644 00000002645 15060132305 0016553 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); } } framework/src/Illuminate/Routing/CompiledRouteCollection.php 0000644 00000021444 15060132305 0020362 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; } } framework/src/Illuminate/Routing/RoutingServiceProvider.php 0000755 00000015706 15060132305 0020265 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 () { $config = $this->app->make('config'); return [$config->get('app.key'), ...($config->get('app.previous_keys') ?? [])]; }); // 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')), function (ServerRequestInterface $request) use ($illuminateRequest) { if ($illuminateRequest->getContentTypeFormat() !== 'json' && $illuminateRequest->request->count() === 0) { return $request; } return $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); }); } } framework/src/Illuminate/Routing/Controllers/Middleware.php 0000644 00000001702 15060132305 0020151 0 ustar 00 <?php namespace Illuminate\Routing\Controllers; use Closure; use Illuminate\Support\Arr; class Middleware { /** * Create a new controller middleware definition. * * @param \Closure|string|array $middleware * @return void */ public function __construct(public Closure|string|array $middleware, public ?array $only = null, public ?array $except = null) { } /** * 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; } } framework/src/Illuminate/Routing/Controllers/HasMiddleware.php 0000644 00000000417 15060132305 0020607 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[] */ public static function middleware(); } framework/src/Illuminate/Routing/FiltersControllerMiddleware.php 0000644 00000001014 15060132305 0021234 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'])); } } framework/src/Illuminate/Routing/RouteBinding.php 0000644 00000006015 15060132305 0016161 0 ustar 00 <?php namespace Illuminate\Routing; use Closure; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\SoftDeletes; 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, $route) 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); $routeBindingMethod = $route->allowsTrashedBindings() && in_array(SoftDeletes::class, class_uses_recursive($instance)) ? 'resolveSoftDeletableRouteBinding' : 'resolveRouteBinding'; if ($model = $instance->{$routeBindingMethod}($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); }; } } framework/src/Illuminate/Routing/MiddlewareNameResolver.php 0000644 00000006152 15060132305 0020172 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; } } framework/src/Illuminate/Routing/RouteCollectionInterface.php 0000644 00000004434 15060132305 0020526 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(); } framework/src/Illuminate/Routing/Events/PreparingResponse.php 0000644 00000001142 15060132305 0020476 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; } } framework/src/Illuminate/Routing/Events/Routing.php 0000644 00000000610 15060132305 0016456 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; } } framework/src/Illuminate/Routing/Events/RouteMatched.php 0000644 00000001121 15060132305 0017411 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; } } framework/src/Illuminate/Routing/Events/ResponsePrepared.php 0000644 00000001253 15060132305 0020314 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; } } framework/src/Illuminate/Routing/LICENSE.md 0000644 00000002063 15060132305 0014462 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. framework/src/Illuminate/Routing/ResponseFactory.php 0000644 00000021017 15060132305 0016715 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\StreamedJsonResponse; 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. * * @param array $data * @param int $status * @param array $headers * @param int $encodingOptions * @return \Symfony\Component\HttpFoundation\StreamedJsonResponse */ public function streamJson($data, $status = 200, $headers = [], $encodingOptions = JsonResponse::DEFAULT_ENCODING_OPTIONS) { return new StreamedJsonResponse($data, $status, $headers, $encodingOptions); } /** * 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 * * @throws \Illuminate\Routing\Exceptions\StreamedResponseException */ 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); } } framework/src/Illuminate/Routing/AbstractRouteCollection.php 0000644 00000021031 15060132305 0020361 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()); } } framework/src/Illuminate/Routing/UrlGenerator.php 0000755 00000055670 15060132305 0016217 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() { return $this->getSession()?->previousUrl(); } /** * 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 an absolute URL with the given query parameters. * * @param string $path * @param array $query * @param mixed $extra * @param bool|null $secure * @return string */ public function query($path, $query = [], $extra = [], $secure = null) { [$path, $existingQueryString] = $this->extractQueryString($path); parse_str(Str::after($existingQueryString, '?'), $existingQueryArray); return rtrim($this->to($path.'?'.Arr::query( array_merge($existingQueryArray, $query) ), $extra, $secure), '?'); } /** * 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), is_array($key) ? $key[0] : $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, '?'); $keys = call_user_func($this->keyResolver); $keys = is_array($keys) ? $keys : [$keys]; foreach ($keys as $key) { if (hash_equals( hash_hmac('sha256', $original, $key), (string) $request->query('signature', '') )) { return true; } } return false; } /** * 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) { return $value instanceof UrlRoutable && $route->bindingFieldFor($key) ? $value->{$route->bindingFieldFor($key)} : $value; })->all(); array_walk_recursive($parameters, function (&$item) { if ($item instanceof BackedEnum) { $item = $item->value; } }); 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; } } framework/src/Illuminate/Routing/RouteRegistrar.php 0000644 00000020607 15060132305 0016554 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 missing(\Closure $missing) * @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', 'missing', '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 )); } } framework/src/Illuminate/Routing/RouteUri.php 0000644 00000002444 15060132305 0015350 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); } } framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php 0000644 00000000207 15060132305 0021410 0 ustar 00 <?php namespace Illuminate\Routing; /** * @deprecated */ trait RouteDependencyResolverTrait { use ResolvesRouteDependencies; } framework/src/Illuminate/Routing/SortedMiddleware.php 0000644 00000007405 15060132305 0017032 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; } } framework/src/Illuminate/Routing/RouteSignatureParameters.php 0000644 00000003146 15060132305 0020576 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(); } } framework/src/Illuminate/Routing/ResolvesRouteDependencies.php 0000644 00000007015 15060132305 0020721 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] ); } } framework/src/Illuminate/Routing/CallableDispatcher.php 0000644 00000002532 15060132305 0017276 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)); } } framework/src/Illuminate/Routing/Route.php 0000755 00000077304 15060132305 0014702 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\Controllers\Middleware; 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->isControllerAction()) { return null; } 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|null */ public function parentOfParameter($parameter) { $key = array_search($parameter, array_keys($this->parameters)); if ($key === 0 || $key === false) { 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())->map(function ($middleware) { return $middleware instanceof Middleware ? $middleware : new Middleware($middleware); })->reject(function ($middleware) use ($method) { return static::methodExcludedByOptions( $method, ['only' => $middleware->only, 'except' => $middleware->except] ); })->map->middleware->flatten()->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); } } framework/src/Illuminate/Routing/RouteFileRegistrar.php 0000644 00000001201 15060132305 0017341 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; } } framework/src/Illuminate/Routing/RouteCollection.php 0000644 00000015613 15060132305 0016706 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); } } framework/src/Illuminate/Routing/RouteAction.php 0000644 00000006610 15060132305 0016025 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; } } framework/src/Illuminate/Routing/Matching/ValidatorInterface.php 0000644 00000000621 15060132305 0021065 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); } framework/src/Illuminate/Routing/Matching/UriValidator.php 0000644 00000001100 15060132305 0017715 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)); } } framework/src/Illuminate/Routing/Matching/HostValidator.php 0000644 00000001161 15060132305 0020102 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()); } } framework/src/Illuminate/Routing/Matching/MethodValidator.php 0000644 00000000766 15060132305 0020417 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()); } } framework/src/Illuminate/Routing/Matching/SchemeValidator.php 0000644 00000001153 15060132305 0020372 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; } } framework/src/Illuminate/Routing/composer.json 0000644 00000002731 15060132305 0015602 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.2", "ext-filter": "*", "ext-hash": "*", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/http": "^11.0", "illuminate/macroable": "^11.0", "illuminate/pipeline": "^11.0", "illuminate/session": "^11.0", "illuminate/support": "^11.0", "symfony/http-foundation": "^7.0", "symfony/http-kernel": "^7.0", "symfony/routing": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Routing\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "illuminate/console": "Required to use the make commands (^11.0).", "nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).", "symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Routing/PendingResourceRegistration.php 0000644 00000014005 15060132305 0021255 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(); } } } framework/src/Illuminate/Routing/RedirectController.php 0000644 00000002264 15060132305 0017377 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); } } framework/src/Illuminate/Routing/ResourceRegistrar.php 0000644 00000051173 15060132305 0017247 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); } } framework/src/Illuminate/Routing/Pipeline.php 0000644 00000002763 15060132305 0015343 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); } } framework/src/Illuminate/Routing/Controller.php 0000644 00000003174 15060132305 0015716 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 )); } } framework/src/Illuminate/Routing/Console/stubs/controller.invokable.stub 0000644 00000000430 15060132305 0022647 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) { // } } framework/src/Illuminate/Routing/Console/stubs/controller.nested.api.stub 0000644 00000002174 15060132305 0022736 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 }}) { // } } framework/src/Illuminate/Routing/Console/stubs/middleware.stub 0000644 00000000646 15060132305 0020641 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); } } framework/src/Illuminate/Routing/Console/stubs/controller.model.stub 0000644 00000002241 15060132305 0021777 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 }}) { // } } framework/src/Illuminate/Routing/Console/stubs/controller.model.api.stub 0000644 00000001616 15060132305 0022554 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 }}) { // } } framework/src/Illuminate/Routing/Console/stubs/controller.singleton.api.stub 0000644 00000001220 15060132305 0023445 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); } } framework/src/Illuminate/Routing/Console/stubs/controller.nested.singleton.api.stub 0000644 00000001600 15060132305 0024730 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); } } framework/src/Illuminate/Routing/Console/stubs/controller.stub 0000644 00000002027 15060132305 0020702 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) { // } } framework/src/Illuminate/Routing/Console/stubs/controller.api.stub 0000644 00000001432 15060132305 0021451 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) { // } } framework/src/Illuminate/Routing/Console/stubs/controller.nested.singleton.stub 0000644 00000002323 15060132305 0024163 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); } } framework/src/Illuminate/Routing/Console/stubs/controller.nested.stub 0000644 00000002751 15060132305 0022167 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 }}) { // } } framework/src/Illuminate/Routing/Console/stubs/controller.plain.stub 0000644 00000000245 15060132305 0022004 0 ustar 00 <?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { // } framework/src/Illuminate/Routing/Console/stubs/controller.singleton.stub 0000644 00000001607 15060132305 0022706 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); } } framework/src/Illuminate/Routing/Console/ControllerMakeCommand.php 0000755 00000026500 15060132305 0021416 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) { $rootNamespace = $this->rootNamespace(); $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);'] = '//'; } $baseControllerExists = file_exists($this->getPath("{$rootNamespace}Http\Controllers\Controller")); if ($baseControllerExists) { $replace["use {$controllerNamespace}\Controller;\n"] = ''; } else { $replace[' extends Controller'] = ''; $replace["use {$rootNamespace}Http\Controllers\Controller;\n"] = ''; } $class = str_replace( array_keys($replace), array_values($replace), parent::buildClass($name) ); return $class; } /** * 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); } } } } framework/src/Illuminate/Routing/Console/MiddlewareMakeCommand.php 0000644 00000002706 15060132305 0021347 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'; } } framework/src/Illuminate/Routing/Redirector.php 0000755 00000016271 15060132305 0015702 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; } } framework/src/Illuminate/Routing/PendingSingletonResourceRegistration.php 0000644 00000012435 15060132305 0023145 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(); } } } framework/src/Illuminate/Routing/Contracts/CallableDispatcher.php 0000644 00000000526 15060132305 0021237 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); } framework/src/Illuminate/Routing/Contracts/ControllerDispatcher.php 0000644 00000001205 15060132305 0021656 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); } framework/src/Illuminate/Routing/RouteGroup.php 0000644 00000005262 15060132305 0015706 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; } } framework/src/Illuminate/Routing/RouteUrlGenerator.php 0000644 00000022064 15060132305 0017222 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 ); } } framework/src/Illuminate/Routing/ImplicitRouteBinding.php 0000644 00000010730 15060132305 0017653 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]; if ($parameterValue === null) { continue; } $backedEnumClass = $parameter->getType()?->getName(); $backedEnum = $parameterValue instanceof $backedEnumClass ? $parameterValue : $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; } } } framework/src/Illuminate/Routing/ControllerDispatcher.php 0000644 00000004317 15060132305 0017725 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(); } } framework/src/Illuminate/Routing/Exceptions/BackedEnumCaseNotFoundException.php 0000644 00000000706 15060132305 0024040 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}]."); } } framework/src/Illuminate/Routing/Exceptions/InvalidSignatureException.php 0000644 00000000537 15060132305 0023043 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.'); } } framework/src/Illuminate/Routing/Exceptions/StreamedResponseException.php 0000644 00000001751 15060132305 0023055 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; } } framework/src/Illuminate/Routing/Exceptions/UrlGenerationException.php 0000644 00000001643 15060132305 0022350 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); } } framework/src/Illuminate/Routing/Exceptions/MissingRateLimiterException.php 0000644 00000001401 15060132305 0023335 0 ustar 00 <?php namespace Illuminate\Routing\Exceptions; use Exception; class MissingRateLimiterException extends Exception { /** * Create a new exception for invalid named rate limiter. * * @param string $limiter * @return static */ public static function forLimiter(string $limiter) { return new static("Rate limiter [{$limiter}] is not defined."); } /** * Create a new exception for an invalid rate limiter based on a model property. * * @param string $limiter * @param class-string $model * @return static */ public static function forLimiterAndUser(string $limiter, string $model) { return new static("Rate limiter [{$model}::{$limiter}] is not defined."); } } framework/src/Illuminate/Routing/ControllerMiddlewareOptions.php 0000644 00000001761 15060132305 0021270 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; } } framework/src/Illuminate/Queue/InteractsWithQueue.php 0000644 00000010333 15060132305 0017020 0 ustar 00 <?php namespace Illuminate\Queue; use DateTimeInterface; use Illuminate\Contracts\Queue\Job as JobContract; use Illuminate\Queue\Jobs\FakeJob; use Illuminate\Support\InteractsWithTime; use InvalidArgumentException; use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; 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); } } /** * Indicate that queue interactions like fail, delete, and release should be faked. * * @return $this */ public function withFakeQueueInteractions() { $this->job = new FakeJob; return $this; } /** * Assert that the job was deleted from the queue. * * @return $this */ public function assertDeleted() { $this->ensureQueueInteractionsHaveBeenFaked(); PHPUnit::assertTrue( $this->job->isDeleted(), 'Job was expected to be deleted, but was not.' ); return $this; } /** * Assert that the job was manually failed. * * @return $this */ public function assertFailed() { $this->ensureQueueInteractionsHaveBeenFaked(); PHPUnit::assertTrue( $this->job->hasFailed(), 'Job was expected to be manually failed, but was not.' ); return $this; } /** * Assert that the job was released back onto the queue. * * @param \DateTimeInterface|\DateInterval|int $delay * @return $this */ public function assertReleased($delay = null) { $this->ensureQueueInteractionsHaveBeenFaked(); $delay = $delay instanceof DateTimeInterface ? $this->secondsUntil($delay) : $delay; PHPUnit::assertTrue( $this->job->isReleased(), 'Job was expected to be released, but was not.' ); if (! is_null($delay)) { PHPUnit::assertSame( $delay, $this->job->releaseDelay, "Expected job to be released with delay of [{$delay}] seconds, but was released with delay of [{$this->job->releaseDelay}] seconds." ); } return $this; } /** * Ensure that queue interactions have been faked. * * @return void */ private function ensureQueueInteractionsHaveBeenFaked() { if (! $this->job instanceof FakeJob) { throw new RuntimeException('Queue interactions have not been faked.'); } } /** * Set the base queue job instance. * * @param \Illuminate\Contracts\Queue\Job $job * @return $this */ public function setJob(JobContract $job) { $this->job = $job; return $this; } } framework/src/Illuminate/Queue/Middleware/SkipIfBatchCancelled.php 0000644 00000000577 15060132305 0021233 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); } } framework/src/Illuminate/Queue/Middleware/ThrottlesExceptionsWithRedis.php 0000644 00000003173 15060132305 0023161 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->decaySeconds ); 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; } if ($this->reportCallback && call_user_func($this->reportCallback, $throwable)) { report($throwable); } $this->limiter->acquire(); return $job->release($this->retryAfterMinutes * 60); } } } framework/src/Illuminate/Queue/Middleware/RateLimited.php 0000644 00000006362 15060132305 0017512 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, 'decaySeconds' => $limit->decaySeconds, ]; })->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->decaySeconds); } 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); } } framework/src/Illuminate/Queue/Middleware/WithoutOverlapping.php 0000644 00000006720 15060132305 0021157 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; } } framework/src/Illuminate/Queue/Middleware/ThrottlesExceptions.php 0000644 00000011644 15060132305 0021340 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 seconds until the maximum attempts are reset. * * @var int */ protected $decaySeconds; /** * The number of minutes to wait before retrying the job after an exception. * * @var int */ protected $retryAfterMinutes = 0; /** * The callback that determines if the exception should be reported. * * @var callable */ protected $reportCallback; /** * 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 $decaySeconds * @return void */ public function __construct($maxAttempts = 10, $decaySeconds = 600) { $this->maxAttempts = $maxAttempts; $this->decaySeconds = $decaySeconds; } /** * 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; } if ($this->reportCallback && call_user_func($this->reportCallback, $throwable)) { report($throwable); } $this->limiter->hit($jobKey, $this->decaySeconds); 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; } /** * Report exceptions and optionally specify a callback that determines if the exception should be reported. * * @param callable|null $callback * @return $this */ public function report(?callable $callback = null) { $this->reportCallback = $callback ?? fn () => 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; } } framework/src/Illuminate/Queue/Middleware/RateLimitedWithRedis.php 0000644 00000004713 15060132305 0021333 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->decaySeconds)) { 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 $decaySeconds * @return bool */ protected function tooManyAttempts($key, $maxAttempts, $decaySeconds) { $limiter = new DurationLimiter( $this->redis, $key, $maxAttempts, $decaySeconds ); 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); } } framework/src/Illuminate/Queue/SerializesModels.php 0000644 00000005236 15060132305 0016507 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); } } framework/src/Illuminate/Queue/MaxAttemptsExceededException.php 0000644 00000001113 15060132305 0020774 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; }); } } framework/src/Illuminate/Queue/Connectors/NullConnector.php 0000644 00000000546 15060132305 0020132 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; } } framework/src/Illuminate/Queue/Connectors/ConnectorInterface.php 0000755 00000000407 15060132305 0021117 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); } framework/src/Illuminate/Queue/Connectors/BeanstalkdConnector.php 0000755 00000002143 15060132305 0021266 0 ustar 00 <?php namespace Illuminate\Queue\Connectors; use Illuminate\Queue\BeanstalkdQueue; use Pheanstalk\Contract\SocketFactoryInterface; use Pheanstalk\Pheanstalk; use Pheanstalk\Values\Timeout; 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'] ?? SocketFactoryInterface::DEFAULT_PORT, isset($config['timeout']) ? new Timeout($config['timeout']) : null, ); } } framework/src/Illuminate/Queue/Connectors/DatabaseConnector.php 0000644 00000002100 15060132305 0020710 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 ); } } framework/src/Illuminate/Queue/Connectors/SqsConnector.php 0000755 00000002371 15060132305 0017767 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); } } framework/src/Illuminate/Queue/Connectors/RedisConnector.php 0000644 00000002371 15060132305 0020264 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 ); } } framework/src/Illuminate/Queue/Connectors/SyncConnector.php 0000755 00000000607 15060132305 0020135 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($config['after_commit'] ?? null); } } framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php 0000644 00000007432 15060132305 0022664 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); } } framework/src/Illuminate/Queue/NullQueue.php 0000644 00000002622 15060132305 0015144 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) { // } } framework/src/Illuminate/Queue/Events/JobProcessed.php 0000644 00000001115 15060132305 0017047 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; } } framework/src/Illuminate/Queue/Events/JobExceptionOccurred.php 0000644 00000001423 15060132305 0020547 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; } } framework/src/Illuminate/Queue/Events/JobQueued.php 0000644 00000002617 15060132305 0016360 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobQueued { /** * The connection name. * * @var string */ public $connectionName; /** * The queue name. * * @var string */ public $queue; /** * The job ID. * * @var string|int|null */ public $id; /** * The job instance. * * @var \Closure|string|object */ public $job; /** * The job payload. * * @var string */ public $payload; /** * The amount of time the job was delayed. * * @var int|null */ public $delay; /** * Create a new event instance. * * @param string $connectionName * @param string $queue * @param string|int|null $id * @param \Closure|string|object $job * @param string $payload * @param int|null $delay * @return void */ public function __construct($connectionName, $queue, $id, $job, $payload, $delay) { $this->connectionName = $connectionName; $this->queue = $queue; $this->id = $id; $this->job = $job; $this->payload = $payload; $this->delay = $delay; } /** * Get the decoded job payload. * * @return array */ public function payload() { return json_decode($this->payload, true, flags: JSON_THROW_ON_ERROR); } } framework/src/Illuminate/Queue/Events/JobRetryRequested.php 0000644 00000001315 15060132305 0020111 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; } } framework/src/Illuminate/Queue/Events/JobReleasedAfterException.php 0000644 00000001132 15060132305 0021504 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; } } framework/src/Illuminate/Queue/Events/JobTimedOut.php 0000644 00000001114 15060132305 0016651 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; } } framework/src/Illuminate/Queue/Events/JobProcessing.php 0000644 00000001116 15060132305 0017235 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; } } framework/src/Illuminate/Queue/Events/JobPopping.php 0000644 00000000607 15060132305 0016541 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; } } framework/src/Illuminate/Queue/Events/QueueBusy.php 0000644 00000001242 15060132305 0016415 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; } } framework/src/Illuminate/Queue/Events/JobQueueing.php 0000644 00000002373 15060132305 0016711 0 ustar 00 <?php namespace Illuminate\Queue\Events; class JobQueueing { /** * The connection name. * * @var string */ public $connectionName; /** * The queue name. * * @var string */ public $queue; /** * The job instance. * * @var \Closure|string|object */ public $job; /** * The job payload. * * @var string */ public $payload; /** * The number of seconds the job was delayed. * * @var int|null */ public $delay; /** * Create a new event instance. * * @param string $connectionName * @param string $queue * @param \Closure|string|object $job * @param string $payload * @param int|null $delay * @return void */ public function __construct($connectionName, $queue, $job, $payload, $delay) { $this->connectionName = $connectionName; $this->queue = $queue; $this->job = $job; $this->payload = $payload; $this->delay = $delay; } /** * Get the decoded job payload. * * @return array */ public function payload() { return json_decode($this->payload, true, flags: JSON_THROW_ON_ERROR); } } framework/src/Illuminate/Queue/Events/WorkerStopping.php 0000644 00000001155 15060132305 0017466 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; } } framework/src/Illuminate/Queue/Events/JobFailed.php 0000644 00000001433 15060132305 0016307 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; } } framework/src/Illuminate/Queue/Events/JobPopped.php 0000644 00000001124 15060132305 0016347 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; } } framework/src/Illuminate/Queue/Events/Looping.php 0000644 00000001036 15060132305 0016076 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; } } framework/src/Illuminate/Queue/LICENSE.md 0000644 00000002063 15060132305 0014117 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. framework/src/Illuminate/Queue/SyncQueue.php 0000755 00000011342 15060132305 0015150 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 { /** * Create a new sync queue instance. * * @param bool $dispatchAfterCommit * @return void */ public function __construct($dispatchAfterCommit = false) { $this->dispatchAfterCommit = $dispatchAfterCommit; } /** * 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) { if ($this->shouldDispatchAfterCommit($job) && $this->container->bound('db.transactions')) { return $this->container->make('db.transactions')->addCallback( fn () => $this->executeJob($job, $data, $queue) ); } return $this->executeJob($job, $data, $queue); } /** * Execute a given job synchronously. * * @param string $job * @param mixed $data * @param string|null $queue * @return int * * @throws \Throwable */ protected function executeJob($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) { // } } framework/src/Illuminate/Queue/Listener.php 0000755 00000013224 15060132305 0015015 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 never */ public function stop() { exit; } /** * Set the output handler callback. * * @param \Closure $outputHandler * @return void */ public function setOutputHandler(Closure $outputHandler) { $this->outputHandler = $outputHandler; } } framework/src/Illuminate/Queue/SqsQueue.php 0000755 00000014050 15060132305 0015001 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; } } framework/src/Illuminate/Queue/Attributes/WithoutRelations.php 0000644 00000000244 15060132305 0020675 0 ustar 00 <?php namespace Illuminate\Queue\Attributes; use Attribute; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] class WithoutRelations { // } framework/src/Illuminate/Queue/Attributes/DeleteWhenMissingModels.php 0000644 00000000216 15060132305 0022072 0 ustar 00 <?php namespace Illuminate\Queue\Attributes; use Attribute; #[Attribute(Attribute::TARGET_CLASS)] class DeleteWhenMissingModels { // } framework/src/Illuminate/Queue/InvalidPayloadException.php 0000644 00000001050 15060132305 0017776 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; } } framework/src/Illuminate/Queue/QueueServiceProvider.php 0000755 00000023352 15060132305 0017353 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) { $app['log']->flushSharedContext(); if (method_exists($app['log'], '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', ]; } } framework/src/Illuminate/Queue/Capsule/Manager.php 0000644 00000011261 15060132305 0016172 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); } } framework/src/Illuminate/Queue/Failed/FileFailedJobProvider.php 0000644 00000013231 15060132305 0020541 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 the IDs of all of the failed jobs. * * @param string|null $queue * @return array */ public function ids($queue = null) { return collect($this->all()) ->when(! is_null($queue), fn ($collect) => $collect->where('queue', $queue)) ->pluck('id') ->all(); } /** * 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(); } } framework/src/Illuminate/Queue/Failed/CountableFailedJobProvider.php 0000644 00000000445 15060132305 0021601 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); } framework/src/Illuminate/Queue/Failed/PrunableFailedJobProvider.php 0000644 00000000461 15060132305 0021433 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); } framework/src/Illuminate/Queue/Failed/NullFailedJobProvider.php 0000644 00000003040 15060132305 0020571 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 the IDs of all of the failed jobs. * * @param string|null $queue * @return array */ public function ids($queue = null) { return []; } /** * 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; } } framework/src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php 0000644 00000010712 15060132305 0022216 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 the IDs of all of the failed jobs. * * @param string|null $queue * @return array */ public function ids($queue = null) { return $this->getTable() ->when(! is_null($queue), fn ($query) => $query->where('queue', $queue)) ->orderBy('id', 'desc') ->pluck('uuid') ->all(); } /** * 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); } } framework/src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php 0000644 00000010064 15060132305 0021367 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 the IDs of all of the failed jobs. * * @param string|null $queue * @return array */ public function ids($queue = null) { return $this->getTable() ->when(! is_null($queue), fn ($query) => $query->where('queue', $queue)) ->orderBy('id', 'desc') ->pluck('id') ->all(); } /** * 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); } } framework/src/Illuminate/Queue/Failed/FailedJobProviderInterface.php 0000644 00000001756 15060132305 0021573 0 ustar 00 <?php namespace Illuminate\Queue\Failed; /** * @method array ids(string $queue = null) */ 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); } framework/src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php 0000644 00000012304 15060132305 0021357 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 the IDs of all of the failed jobs. * * @param string|null $queue * @return array */ public function ids($queue = null) { return collect($this->all()) ->when(! is_null($queue), fn ($collect) => $collect->where('queue', $queue)) ->pluck('id') ->all(); } /** * 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'], date_default_timezone_get() )->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'], date_default_timezone_get() )->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."); } } framework/src/Illuminate/Queue/ListenerOptions.php 0000644 00000001606 15060132305 0016367 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); } } framework/src/Illuminate/Queue/Jobs/SyncJob.php 0000755 00000003270 15060132305 0015474 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'; } } framework/src/Illuminate/Queue/Jobs/Job.php 0000755 00000017650 15060132305 0014646 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Bus\Batchable; use Illuminate\Bus\BatchRepository; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\ManuallyFailedException; use Illuminate\Queue\TimeoutExceededException; use Illuminate\Support\InteractsWithTime; use Throwable; 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; } $commandName = $this->payload()['data']['commandName'] ?? false; // If the exception is due to a job timing out, we need to rollback the current // database transaction so that the failed job count can be incremented with // the proper value. Otherwise, the current transaction will never commit. if ($e instanceof TimeoutExceededException && $commandName && in_array(Batchable::class, class_uses_recursive($commandName))) { $batchRepository = $this->resolve(BatchRepository::class); try { $batchRepository->rollBack(); } catch (Throwable $e) { // ... } } 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; } } framework/src/Illuminate/Queue/Jobs/BeanstalkdJob.php 0000755 00000006133 15060132305 0016631 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Container\Container; use Illuminate\Contracts\Queue\Job as JobContract; use Pheanstalk\Contract\JobIdInterface; use Pheanstalk\Pheanstalk; class BeanstalkdJob extends Job implements JobContract { /** * The Pheanstalk instance. * * @var \Pheanstalk\Contract\PheanstalkManagerInterface&\Pheanstalk\Contract\PheanstalkPublisherInterface&\Pheanstalk\Contract\PheanstalkSubscriberInterface */ protected $pheanstalk; /** * The Pheanstalk job instance. * * @var \Pheanstalk\Job */ protected $job; /** * Create a new job instance. * * @param \Illuminate\Container\Container $container * @param \Pheanstalk\Contract\PheanstalkManagerInterface&\Pheanstalk\Contract\PheanstalkPublisherInterface&\Pheanstalk\Contract\PheanstalkSubscriberInterface $pheanstalk * @param \Pheanstalk\Contract\JobIdInterface $job * @param string $connectionName * @param string $queue * @return void */ public function __construct(Container $container, $pheanstalk, JobIdInterface $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\Contract\PheanstalkManagerInterface&\Pheanstalk\Contract\PheanstalkPublisherInterface&\Pheanstalk\Contract\PheanstalkSubscriberInterface */ public function getPheanstalk() { return $this->pheanstalk; } /** * Get the underlying Pheanstalk job. * * @return \Pheanstalk\Contract\JobIdInterface */ public function getPheanstalkJob() { return $this->job; } } framework/src/Illuminate/Queue/Jobs/DatabaseJob.php 0000644 00000004325 15060132305 0016263 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; } } framework/src/Illuminate/Queue/Jobs/DatabaseJobRecord.php 0000644 00000002150 15060132305 0017414 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}; } } framework/src/Illuminate/Queue/Jobs/SqsJob.php 0000755 00000005003 15060132305 0015322 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; } } framework/src/Illuminate/Queue/Jobs/FakeJob.php 0000644 00000002557 15060132305 0015432 0 ustar 00 <?php namespace Illuminate\Queue\Jobs; use Illuminate\Support\Str; class FakeJob extends Job { /** * The number of seconds the released job was delayed. * * @var int */ public $releaseDelay; /** * The exception the job failed with. * * @var \Throwable */ public $failedWith; /** * Get the job identifier. * * @return string */ public function getJobId() { return once(fn () => (string) Str::uuid()); } /** * Get the raw body of the job. * * @return string */ public function getRawBody() { return ''; } /** * Release the job back into the queue after (n) seconds. * * @param \DateTimeInterface|\DateInterval|int $delay * @return void */ public function release($delay = 0) { $this->released = true; $this->releaseDelay = $delay; } /** * Delete the job from the queue. * * @return void */ public function delete() { $this->deleted = true; } /** * Delete the job, call the "failed" method, and raise the failed job event. * * @param \Throwable|null $exception * @return void */ public function fail($exception = null) { $this->failed = true; $this->failedWith = $exception; } } framework/src/Illuminate/Queue/Jobs/RedisJob.php 0000644 00000005745 15060132305 0015634 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; } } framework/src/Illuminate/Queue/Jobs/JobName.php 0000644 00000001236 15060132305 0015435 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; } } framework/src/Illuminate/Queue/WorkerOptions.php 0000644 00000004574 15060132305 0016062 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; } } framework/src/Illuminate/Queue/ManuallyFailedException.php 0000644 00000000175 15060132305 0017774 0 ustar 00 <?php namespace Illuminate\Queue; use RuntimeException; class ManuallyFailedException extends RuntimeException { // } framework/src/Illuminate/Queue/composer.json 0000644 00000003437 15060132305 0015243 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.2", "illuminate/collections": "^11.0", "illuminate/console": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/database": "^11.0", "illuminate/filesystem": "^11.0", "illuminate/pipeline": "^11.0", "illuminate/support": "^11.0", "laravel/serializable-closure": "^1.2.2", "ramsey/uuid": "^4.7", "symfony/process": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Queue\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.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 (^11.0).", "pda/pheanstalk": "Required to use the Beanstalk queue driver (^5.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Queue/CallQueuedHandler.php 0000644 00000020365 15060132305 0016553 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 Illuminate\Queue\Attributes\DeleteWhenMissingModels; 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 { $reflectionClass = new ReflectionClass($class); $shouldDelete = $reflectionClass->getDefaultProperties()['deleteWhenMissingModels'] ?? count($reflectionClass->getAttributes(DeleteWhenMissingModels::class)) !== 0; } 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); } } } framework/src/Illuminate/Queue/DatabaseQueue.php 0000644 00000027241 15060132305 0015742 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', '>=')) || ($databaseEngine === 'vitess' && version_compare($databaseVersion, '19.0', '>='))) { 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; } } framework/src/Illuminate/Queue/TimeoutExceededException.php 0000644 00000000665 15060132305 0020166 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; }); } } framework/src/Illuminate/Queue/RedisQueue.php 0000644 00000023430 15060132305 0015300 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 * @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; } } framework/src/Illuminate/Queue/Console/stubs/jobs.stub 0000644 00000001470 15060132305 0017112 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}}'); } }; framework/src/Illuminate/Queue/Console/stubs/failed_jobs.stub 0000644 00000001374 15060132305 0020421 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}}'); } }; framework/src/Illuminate/Queue/Console/stubs/batches.stub 0000644 00000001663 15060132305 0017572 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}}'); } }; framework/src/Illuminate/Queue/Console/WorkCommand.php 0000644 00000022417 15060132305 0017054 0 ustar 00 <?php namespace Illuminate\Queue\Console; 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 Illuminate\Support\InteractsWithTime; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Terminal; use function Termwind\terminal; #[AsCommand(name: 'queue:work')] class WorkCommand extends Command { use InteractsWithTime; /** * 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->runTimeForHumans($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(); } /** * 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(); } } framework/src/Illuminate/Queue/Console/RetryCommand.php 0000644 00000012727 15060132305 0017242 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') { $failer = $this->laravel['queue.failer']; return method_exists($failer, 'ids') ? $failer->ids() : Arr::pluck($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) { $failer = $this->laravel['queue.failer']; $ids = method_exists($failer, 'ids') ? $failer->ids($queue) : collect($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); } } framework/src/Illuminate/Queue/Console/PruneBatchesCommand.php 0000644 00000003772 15060132305 0020520 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."); } } } framework/src/Illuminate/Queue/Console/FailedTableCommand.php 0000644 00000003510 15060132305 0020257 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use function Illuminate\Filesystem\join_paths; #[AsCommand(name: 'make:queue-failed-table', aliases: ['queue:failed-table'])] class FailedTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:queue-failed-table'; /** * The console command name aliases. * * @var array */ protected $aliases = ['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'; } /** * Determine whether a migration for the table already exists. * * @param string $table * @return bool */ protected function migrationExists($table) { if ($table !== 'failed_jobs') { return parent::migrationExists($table); } foreach ([ join_paths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php'), join_paths($this->laravel->databasePath('migrations'), '0001_01_01_000002_create_jobs_table.php'), ] as $path) { if (count($this->files->glob($path)) !== 0) { return true; } } return false; } } framework/src/Illuminate/Queue/Console/TableCommand.php 0000644 00000003437 15060132305 0017162 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use function Illuminate\Filesystem\join_paths; #[AsCommand(name: 'make:queue-table', aliases: ['queue:table'])] class TableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:queue-table'; /** * The console command name aliases. * * @var array */ protected $aliases = ['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'; } /** * Determine whether a migration for the table already exists. * * @param string $table * @return bool */ protected function migrationExists($table) { if ($table !== 'jobs') { return parent::migrationExists($table); } foreach ([ join_paths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php'), join_paths($this->laravel->databasePath('migrations'), '0001_01_01_000002_create_jobs_table.php'), ] as $path) { if (count($this->files->glob($path)) !== 0) { return true; } } return false; } } framework/src/Illuminate/Queue/Console/ForgetFailedCommand.php 0000644 00000001613 15060132305 0020460 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 int|null */ 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.'); return 1; } } } framework/src/Illuminate/Queue/Console/ClearCommand.php 0000644 00000005353 15060132305 0017160 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 1; } 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'], ]; } } framework/src/Illuminate/Queue/Console/PruneFailedJobsCommand.php 0000644 00000002323 15060132305 0021140 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."); } } framework/src/Illuminate/Queue/Console/RetryBatchCommand.php 0000644 00000003224 15060132305 0020174 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'); } } framework/src/Illuminate/Queue/Console/ListFailedCommand.php 0000644 00000005624 15060132305 0020153 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]) ), ); } } framework/src/Illuminate/Queue/Console/BatchesTableCommand.php 0000644 00000003522 15060132305 0020447 0 ustar 00 <?php namespace Illuminate\Queue\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use function Illuminate\Filesystem\join_paths; #[AsCommand(name: 'make:queue-batches-table', aliases: ['queue:batches-table'])] class BatchesTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:queue-batches-table'; /** * The console command name aliases. * * @var array */ protected $aliases = ['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'; } /** * Determine whether a migration for the table already exists. * * @param string $table * @return bool */ protected function migrationExists($table) { if ($table !== 'job_batches') { return parent::migrationExists($table); } foreach ([ join_paths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php'), join_paths($this->laravel->databasePath('migrations'), '0001_01_01_000002_create_jobs_table.php'), ] as $path) { if (count($this->files->glob($path)) !== 0) { return true; } } return false; } } framework/src/Illuminate/Queue/Console/RestartCommand.php 0000644 00000002411 15060132305 0017546 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.'); } } framework/src/Illuminate/Queue/Console/FlushFailedCommand.php 0000644 00000001760 15060132305 0020316 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.'); } } framework/src/Illuminate/Queue/Console/MonitorCommand.php 0000644 00000007377 15060132305 0017571 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'], ) ); } } } framework/src/Illuminate/Queue/Console/ListenCommand.php 0000755 00000010042 15060132305 0017362 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); }); } } framework/src/Illuminate/Queue/QueueManager.php 0000755 00000015616 15060132305 0015616 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); } } framework/src/Illuminate/Queue/Queue.php 0000755 00000030365 15060132305 0014321 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\Queue\Events\JobQueueing; 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 ('.json_last_error().'): '.json_last_error_msg(), $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' => $this->getJobTries($job) ?? 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 maximum number of attempts for an object-based queue handler. * * @param mixed $job * @return mixed */ public function getJobTries($job) { if (! method_exists($job, 'tries') && ! isset($job->tries)) { return; } if (isset($job->tries)) { return $job->tries; } if (method_exists($job, 'tries') && ! is_null($job->tries())) { return $job->tries(); } } /** * 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 ($queue, $job, $payload, $delay, $callback) { $this->raiseJobQueueingEvent($queue, $job, $payload, $delay); return tap($callback($payload, $queue, $delay), function ($jobId) use ($queue, $job, $payload, $delay) { $this->raiseJobQueuedEvent($queue, $jobId, $job, $payload, $delay); }); } ); } $this->raiseJobQueueingEvent($queue, $job, $payload, $delay); return tap($callback($payload, $queue, $delay), function ($jobId) use ($queue, $job, $payload, $delay) { $this->raiseJobQueuedEvent($queue, $jobId, $job, $payload, $delay); }); } /** * 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 ($job instanceof ShouldQueueAfterCommit) { return true; } if (! $job instanceof Closure && is_object($job) && isset($job->afterCommit)) { return $job->afterCommit; } return $this->dispatchAfterCommit ?? false; } /** * Raise the job queueing event. * * @param string $queue * @param \Closure|string|object $job * @param string $payload * @param \DateTimeInterface|\DateInterval|int|null $delay * @return void */ protected function raiseJobQueueingEvent($queue, $job, $payload, $delay) { if ($this->container->bound('events')) { $delay = ! is_null($delay) ? $this->secondsUntil($delay) : $delay; $this->container['events']->dispatch(new JobQueueing($this->connectionName, $queue, $job, $payload, $delay)); } } /** * Raise the job queued event. * * @param string $queue * @param string|int|null $jobId * @param \Closure|string|object $job * @param string $payload * @param \DateTimeInterface|\DateInterval|int|null $delay * @return void */ protected function raiseJobQueuedEvent($queue, $jobId, $job, $payload, $delay) { if ($this->container->bound('events')) { $delay = ! is_null($delay) ? $this->secondsUntil($delay) : $delay; $this->container['events']->dispatch(new JobQueued($this->connectionName, $queue, $jobId, $job, $payload, $delay)); } } /** * 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; } } framework/src/Illuminate/Queue/Worker.php 0000644 00000063133 15060132305 0014502 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; } } framework/src/Illuminate/Queue/BeanstalkdQueue.php 0000755 00000013457 15060132305 0016315 0 ustar 00 <?php namespace Illuminate\Queue; use Illuminate\Contracts\Queue\Queue as QueueContract; use Illuminate\Queue\Jobs\BeanstalkdJob; use Pheanstalk\Contract\JobIdInterface; use Pheanstalk\Pheanstalk; use Pheanstalk\Values\Job; use Pheanstalk\Values\JobId; use Pheanstalk\Values\TubeName; class BeanstalkdQueue extends Queue implements QueueContract { /** * The Pheanstalk instance. * * @var \Pheanstalk\Contract\PheanstalkManagerInterface&\Pheanstalk\Contract\PheanstalkPublisherInterface&\Pheanstalk\Contract\PheanstalkSubscriberInterface */ 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\Contract\PheanstalkManagerInterface&\Pheanstalk\Contract\PheanstalkPublisherInterface&\Pheanstalk\Contract\PheanstalkSubscriberInterface $pheanstalk * @param string $default * @param int $timeToRun * @param int $blockFor * @param bool $dispatchAfterCommit * @return void */ public function __construct($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) { return (int) $this->pheanstalk->statsTube(new TubeName($this->getQueue($queue)))->currentJobsReady; } /** * 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 = []) { $this->pheanstalk->useTube(new TubeName($this->getQueue($queue))); return $this->pheanstalk->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) { $this->pheanstalk->useTube(new TubeName($this->getQueue($queue))); return $this->pheanstalk->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); $this->pheanstalk->watch(new TubeName($queue)); $job = $this->pheanstalk->reserveWithTimeout($this->blockFor); if ($job instanceof JobIdInterface) { 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) { $this->pheanstalk->useTube(new TubeName($this->getQueue($queue))); $this->pheanstalk->delete(new Job(new JobId($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\Contract\PheanstalkManagerInterface&\Pheanstalk\Contract\PheanstalkPublisherInterface&\Pheanstalk\Contract\PheanstalkSubscriberInterface */ public function getPheanstalk() { return $this->pheanstalk; } } framework/src/Illuminate/Queue/LuaScripts.php 0000644 00000010530 15060132305 0015313 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; } } framework/src/Illuminate/Queue/README.md 0000644 00000002301 15060132305 0013765 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). framework/src/Illuminate/Queue/CallQueuedClosure.php 0000644 00000005000 15060132305 0016577 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().')'; } } framework/src/Illuminate/Encryption/Encrypter.php 0000755 00000024017 15060132305 0016253 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 previous / legacy encryption keys. * * @var array */ protected $previousKeys = []; /** * 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, $this->key); $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']) ); $foundValidMac = false; // 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. foreach ($this->getAllKeys() as $key) { if ( $this->shouldValidateMac() && ! ($foundValidMac = $foundValidMac || $this->validMacForKey($payload, $key)) ) { continue; } $decrypted = \openssl_decrypt( $payload['value'], strtolower($this->cipher), $key, 0, $iv, $tag ?? '' ); if ($decrypted !== false) { break; } } if ($this->shouldValidateMac() && ! $foundValidMac) { throw new DecryptException('The MAC is invalid.'); } if (($decrypted ?? false) === 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 * @param string $key * @return string */ protected function hash($iv, $value, $key) { return hash_hmac('sha256', $iv.$value, $key); } /** * Get the JSON array from the given payload. * * @param string $payload * @return array * * @throws \Illuminate\Contracts\Encryption\DecryptException */ protected function getJsonPayload($payload) { if (! is_string($payload)) { throw new DecryptException('The payload is invalid.'); } $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.'); } 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 for the primary key. * * @param array $payload * @return bool */ protected function validMac(array $payload) { return $this->validMacForKey($payload, $this->key); } /** * Determine if the MAC is valid for the given payload and key. * * @param array $payload * @param string $key * @return bool */ protected function validMacForKey($payload, $key) { return hash_equals( $this->hash($payload['iv'], $payload['value'], $key), $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.'); } } /** * Determine if we should validate the MAC while decrypting. * * @return bool */ protected function shouldValidateMac() { return ! self::$supportedCiphers[strtolower($this->cipher)]['aead']; } /** * Get the encryption key that the encrypter is currently using. * * @return string */ public function getKey() { return $this->key; } /** * Get the current encryption key and all previous encryption keys. * * @return array */ public function getAllKeys() { return [$this->key, ...$this->previousKeys]; } /** * Get the previous encryption keys. * * @return array */ public function getPreviousKeys() { return $this->previousKeys; } /** * Set the previous / legacy encryption keys that should be utilized if decryption fails. * * @param array $keys * @return $this */ public function previousKeys(array $keys) { foreach ($keys as $key) { if (! static::supported($key, $this->cipher)) { $ciphers = implode(', ', array_keys(self::$supportedCiphers)); throw new RuntimeException("Unsupported cipher or incorrect key length. Supported ciphers are: {$ciphers}."); } } $this->previousKeys = $keys; return $this; } } framework/src/Illuminate/Encryption/LICENSE.md 0000644 00000002063 15060132305 0015165 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. framework/src/Illuminate/Encryption/composer.json 0000644 00000001620 15060132305 0016301 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.2", "ext-hash": "*", "ext-mbstring": "*", "ext-openssl": "*", "illuminate/contracts": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Encryption\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Encryption/EncryptionServiceProvider.php 0000755 00000004141 15060132305 0021462 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'])) ->previousKeys(array_map( fn ($key) => $this->parseKey(['key' => $key]), $config['previous_keys'] ?? [] )); }); } /** * 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; } }); } } framework/src/Illuminate/Encryption/MissingAppKeyException.php 0000644 00000000600 15060132305 0020667 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); } } framework/src/Illuminate/Pipeline/PipelineServiceProvider.php 0000644 00000001456 15060132305 0020513 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', ]; } } framework/src/Illuminate/Pipeline/LICENSE.md 0000644 00000002063 15060132305 0014600 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. framework/src/Illuminate/Pipeline/Hub.php 0000644 00000004104 15060132305 0014421 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; } } framework/src/Illuminate/Pipeline/composer.json 0000644 00000001470 15060132305 0015717 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.2", "illuminate/contracts": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Pipeline\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Pipeline/Pipeline.php 0000644 00000015446 15060132305 0015463 0 ustar 00 <?php namespace Illuminate\Pipeline; use Closure; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract; use Illuminate\Support\Traits\Conditionable; use RuntimeException; use Throwable; class Pipeline implements PipelineContract { use Conditionable; /** * 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; } } framework/src/Illuminate/Mail/Transport/LogTransport.php 0000644 00000004642 15060132305 0017461 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Illuminate\Support\Str; use Psr\Log\LoggerInterface; use Stringable; use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Component\Mime\RawMessage; class LogTransport implements Stringable, 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 = Str::of($message->toString()); if ($string->contains('Content-Type: multipart/')) { $boundary = $string ->after('boundary=') ->before("\r\n") ->prepend('--') ->append("\r\n"); $string = $string ->explode($boundary) ->map($this->decodeQuotedPrintableContent(...)) ->implode($boundary); } elseif ($string->contains('Content-Transfer-Encoding: quoted-printable')) { $string = $this->decodeQuotedPrintableContent($string); } $this->logger->debug((string) $string); return new SentMessage($message, $envelope ?? Envelope::create($message)); } /** * Decode the given quoted printable content. * * @param string $part * @return string */ protected function decodeQuotedPrintableContent(string $part) { if (! str_contains($part, 'Content-Transfer-Encoding: quoted-printable')) { return $part; } [$headers, $content] = explode("\r\n\r\n", $part, 2); return implode("\r\n\r\n", [ $headers, quoted_printable_decode($content), ]); } /** * 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'; } } framework/src/Illuminate/Mail/Transport/SesTransport.php 0000644 00000010421 15060132305 0017462 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Aws\Exception\AwsException; use Aws\Ses\SesClient; use Stringable; 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 implements Stringable { /** * 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) { if ($listManagementOptions = $this->listManagementOptions($message)) { $options['ListManagementOptions'] = $listManagementOptions; } 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); } /** * Extract the SES list management options, if applicable. * * @param \Symfony\Component\Mailer\SentMessage $message * @return array|null */ protected function listManagementOptions(SentMessage $message) { if ($header = $message->getOriginalMessage()->getHeaders()->get('X-SES-LIST-MANAGEMENT-OPTIONS')) { if (preg_match("/^(contactListName=)*(?<ContactListName>[^;]+)(;\s?topicName=(?<TopicName>.+))?$/ix", $header->getBodyAsString(), $listManagementOptions)) { return array_filter($listManagementOptions, fn ($e) => in_array($e, ['ContactListName', 'TopicName']), ARRAY_FILTER_USE_KEY); } } } /** * 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'; } } framework/src/Illuminate/Mail/Transport/ResendTransport.php 0000644 00000010550 15060132305 0020153 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Exception; use Resend\Contracts\Client; use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\AbstractTransport; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; use Symfony\Component\Mime\MessageConverter; /* MIT License Copyright (c) 2023 Jayan Ratna 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. */ class ResendTransport extends AbstractTransport { /** * Create a new Resend transport instance. */ public function __construct(protected Client $resend) { parent::__construct(); } /** * {@inheritDoc} */ protected function doSend(SentMessage $message): void { $email = MessageConverter::toEmail($message->getOriginalMessage()); $envelope = $message->getEnvelope(); $headers = []; $headersToBypass = ['from', 'to', 'cc', 'bcc', 'reply-to', 'sender', 'subject', 'content-type']; foreach ($email->getHeaders()->all() as $name => $header) { if (in_array($name, $headersToBypass, true)) { continue; } $headers[$header->getName()] = $header->getBodyAsString(); } $attachments = []; if ($email->getAttachments()) { foreach ($email->getAttachments() as $attachment) { $headers = $attachment->getPreparedHeaders(); $filename = $headers->getHeaderParameter('Content-Disposition', 'filename'); $item = [ 'content' => str_replace("\r\n", '', $attachment->bodyToString()), 'filename' => $filename, ]; $attachments[] = $item; } } try { $result = $this->resend->emails->send([ 'from' => $envelope->getSender()->toString(), 'to' => $this->stringifyAddresses($this->getRecipients($email, $envelope)), 'cc' => $this->stringifyAddresses($email->getCc()), 'bcc' => $this->stringifyAddresses($email->getBcc()), 'reply_to' => $this->stringifyAddresses($email->getReplyTo()), 'headers' => $headers, 'subject' => $email->getSubject(), 'html' => $email->getHtmlBody(), 'text' => $email->getTextBody(), 'attachments' => $attachments, ]); } catch (Exception $exception) { throw new TransportException( sprintf('Request to Resend API failed. Reason: %s.', $exception->getMessage()), is_int($exception->getCode()) ? $exception->getCode() : 0, $exception ); } $messageId = $result->id; $email->getHeaders()->addHeader('X-Resend-Email-ID', $messageId); } /** * Get the recipients without CC or BCC. */ protected function getRecipients(Email $email, Envelope $envelope): array { return array_filter($envelope->getRecipients(), function (Address $address) use ($email) { return in_array($address, array_merge($email->getCc(), $email->getBcc()), true) === false; }); } /** * Get the string representation of the transport. */ public function __toString(): string { return 'resend'; } } framework/src/Illuminate/Mail/Transport/SesV2Transport.php 0000644 00000010717 15060132305 0017702 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Aws\Exception\AwsException; use Aws\SesV2\SesV2Client; use Stringable; 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 implements Stringable { /** * 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) { if ($listManagementOptions = $this->listManagementOptions($message)) { $options['ListManagementOptions'] = $listManagementOptions; } foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) { if ($header instanceof MetadataHeader) { $options['EmailTags'][] = ['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); } /** * Extract the SES list managenent options, if applicable. * * @param \Illuminate\Mail\SentMessage $message * @return array|null */ protected function listManagementOptions(SentMessage $message) { if ($header = $message->getOriginalMessage()->getHeaders()->get('X-SES-LIST-MANAGEMENT-OPTIONS')) { if (preg_match("/^(contactListName=)*(?<ContactListName>[^;]+)(;\s?topicName=(?<TopicName>.+))?$/ix", $header->getBodyAsString(), $listManagementOptions)) { return array_filter($listManagementOptions, fn ($e) => in_array($e, ['ContactListName', 'TopicName']), ARRAY_FILTER_USE_KEY); } } } /** * 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'; } } framework/src/Illuminate/Mail/Transport/ArrayTransport.php 0000644 00000002712 15060132305 0020012 0 ustar 00 <?php namespace Illuminate\Mail\Transport; use Illuminate\Support\Collection; use Stringable; use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Component\Mime\RawMessage; class ArrayTransport implements Stringable, 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'; } } framework/src/Illuminate/Mail/Mailable.php 0000644 00000133763 15060132305 0014544 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->isInitialized($this) && $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 'to' 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 'cc' 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 'bcc' 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); } } framework/src/Illuminate/Mail/Message.php 0000755 00000023563 15060132305 0014421 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); } } framework/src/Illuminate/Mail/Events/MessageSending.php 0000644 00000001145 15060132305 0017162 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; } } framework/src/Illuminate/Mail/Events/MessageSent.php 0000644 00000003527 15060132305 0016512 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); } } framework/src/Illuminate/Mail/LICENSE.md 0000644 00000002063 15060132305 0013715 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. framework/src/Illuminate/Mail/Markdown.php 0000644 00000011071 15060132305 0014603 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; } } framework/src/Illuminate/Mail/SendQueuedMailable.php 0000644 00000007143 15060132305 0016517 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; } } framework/src/Illuminate/Mail/TextMessage.php 0000644 00000002612 15060132305 0015253 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); } } framework/src/Illuminate/Mail/MailManager.php 0000644 00000043133 15060132305 0015202 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\ResendTransport; 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 Resend; 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\RoundRobinTransport; 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 Resend Transport driver. * * @param array $config * @return \Illuminate\Mail\Transport\ResendTransprot */ protected function createResendTransport(array $config) { return new ResendTransport( Resend::client($config['key'] ?? $this->app['config']->get('services.resend.key')), ); } /** * 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 Symfony Roundrobin Transport driver. * * @param array $config * @return \Symfony\Component\Mailer\Transport\RoundRobinTransport */ protected function createRoundrobinTransport(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 RoundRobinTransport($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); } } framework/src/Illuminate/Mail/Mailer.php 0000755 00000045127 15060132305 0014246 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); } /** * Send a new message synchronously using a view. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $mailable * @param array $data * @param \Closure|string|null $callback * @return \Illuminate\Mail\SentMessage|null */ public function sendNow($mailable, array $data = [], $callback = null) { return $mailable instanceof MailableContract ? $mailable->mailer($this->name)->send($this) : $this->send($mailable, $data, $callback); } /** * 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 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 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 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 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 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 = []) { $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; } } framework/src/Illuminate/Mail/composer.json 0000755 00000003077 15060132305 0015044 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.2", "illuminate/collections": "^11.0", "illuminate/container": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0", "league/commonmark": "^2.2", "psr/log": "^1.0|^2.0|^3.0", "symfony/mailer": "^7.0", "tijsverkoyen/css-to-inline-styles": "^2.2.5" }, "autoload": { "psr-4": { "Illuminate\\Mail\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "aws/aws-sdk-php": "Required to use the SES mail driver (^3.235.5).", "resend/resend-php": "Required to enable support for the Resend mail transport (^0.10.0).", "symfony/http-client": "Required to use the Symfony API mail transports (^7.0).", "symfony/mailgun-mailer": "Required to enable support for the Mailgun mail transport (^7.0).", "symfony/postmark-mailer": "Required to enable support for the Postmark mail transport (^7.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Mail/MailServiceProvider.php 0000755 00000003345 15060132305 0016747 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, ]; } } framework/src/Illuminate/Mail/resources/views/html/themes/default.css 0000644 00000011013 15060132305 0022042 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; } framework/src/Illuminate/Mail/resources/views/html/header.blade.php 0000644 00000000402 15060132305 0021426 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> framework/src/Illuminate/Mail/resources/views/html/layout.blade.php 0000644 00000002525 15060132305 0021523 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> framework/src/Illuminate/Mail/resources/views/html/table.blade.php 0000644 00000000110 15060132305 0021261 0 ustar 00 <div class="table"> {{ Illuminate\Mail\Markdown::parse($slot) }} </div> framework/src/Illuminate/Mail/resources/views/html/button.blade.php 0000644 00000001100 15060132305 0021505 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> framework/src/Illuminate/Mail/resources/views/html/message.blade.php 0000644 00000000757 15060132305 0021637 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') }}. {{ __('All rights reserved.') }} </x-mail::footer> </x-slot:footer> </x-mail::layout> framework/src/Illuminate/Mail/resources/views/html/subcopy.blade.php 0000644 00000000245 15060132305 0021667 0 ustar 00 <table class="subcopy" width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td> {{ Illuminate\Mail\Markdown::parse($slot) }} </td> </tr> </table> framework/src/Illuminate/Mail/resources/views/html/panel.blade.php 0000644 00000000465 15060132305 0021306 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> framework/src/Illuminate/Mail/resources/views/html/footer.blade.php 0000644 00000000354 15060132305 0021502 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> framework/src/Illuminate/Mail/resources/views/text/header.blade.php 0000644 00000000030 15060132305 0021443 0 ustar 00 {{ $slot }}: {{ $url }} framework/src/Illuminate/Mail/resources/views/text/layout.blade.php 0000644 00000000231 15060132305 0021533 0 ustar 00 {!! strip_tags($header ?? '') !!} {!! strip_tags($slot) !!} @isset($subcopy) {!! strip_tags($subcopy) !!} @endisset {!! strip_tags($footer ?? '') !!} framework/src/Illuminate/Mail/resources/views/text/table.blade.php 0000644 00000000014 15060132305 0021304 0 ustar 00 {{ $slot }} framework/src/Illuminate/Mail/resources/views/text/button.blade.php 0000644 00000000030 15060132305 0021526 0 ustar 00 {{ $slot }}: {{ $url }} framework/src/Illuminate/Mail/resources/views/text/message.blade.php 0000644 00000001210 15060132305 0021640 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> framework/src/Illuminate/Mail/resources/views/text/subcopy.blade.php 0000644 00000000014 15060132305 0021701 0 ustar 00 {{ $slot }} framework/src/Illuminate/Mail/resources/views/text/panel.blade.php 0000644 00000000014 15060132305 0021314 0 ustar 00 {{ $slot }} framework/src/Illuminate/Mail/resources/views/text/footer.blade.php 0000644 00000000014 15060132305 0021513 0 ustar 00 {{ $slot }} framework/src/Illuminate/Mail/Mailables/Address.php 0000644 00000001052 15060132305 0016275 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; } } framework/src/Illuminate/Mail/Mailables/Headers.php 0000644 00000003750 15060132305 0016272 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 $text * @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(' '); } } framework/src/Illuminate/Mail/Mailables/Content.php 0000644 00000006050 15060132305 0016325 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; } } framework/src/Illuminate/Mail/Mailables/Envelope.php 0000644 00000022512 15060132305 0016471 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<int, \Illuminate\Mail\Mailables\Address|string> $to * @param array<int, \Illuminate\Mail\Mailables\Address|string> $cc * @param array<int, \Illuminate\Mail\Mailables\Address|string> $bcc * @param array<int, \Illuminate\Mail\Mailables\Address|string> $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|null $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<int, \Illuminate\Mail\Mailables\Address|string> $addresses * @return array<int, \Illuminate\Mail\Mailables\Address> */ 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<int, \Illuminate\Mail\Mailables\Address|string>|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<int, \Illuminate\Mail\Mailables\Address|string>|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<int, \Illuminate\Mail\Mailables\Address|string>|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<int, \Illuminate\Mail\Mailables\Address|string>|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; } } framework/src/Illuminate/Mail/Mailables/Attachment.php 0000644 00000000265 15060132305 0017005 0 ustar 00 <?php namespace Illuminate\Mail\Mailables; use Illuminate\Mail\Attachment as BaseAttachment; class Attachment extends BaseAttachment { // Here for namespace consistency... } framework/src/Illuminate/Mail/PendingMail.php 0000644 00000007440 15060132305 0015215 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)); } /** * Send a new mailable message instance synchronously. * * @param \Illuminate\Contracts\Mail\Mailable $mailable * @return \Illuminate\Mail\SentMessage|null */ public function sendNow(MailableContract $mailable) { return $this->mailer->sendNow($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); } }); } } framework/src/Illuminate/Mail/Attachment.php 0000644 00000011651 15060132305 0015115 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], )); } } framework/src/Illuminate/Mail/SentMessage.php 0000644 00000003744 15060132305 0015247 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']; } } framework/src/Illuminate/Console/Scheduling/SchedulingMutex.php 0000644 00000001222 15060132305 0020733 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); } framework/src/Illuminate/Console/Scheduling/CacheEventMutex.php 0000644 00000005660 15060132305 0020665 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; } } framework/src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php 0000644 00000002470 15060132305 0022025 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)); }); } } framework/src/Illuminate/Console/Scheduling/ScheduleClearCacheCommand.php 0000644 00000002233 15060132305 0022554 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'schedule:clear-cache')] 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.'); } } } framework/src/Illuminate/Console/Scheduling/CommandBuilder.php 0000644 00000004275 15060132305 0020523 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; } } framework/src/Illuminate/Console/Scheduling/CallbackEvent.php 0000644 00000011416 15060132305 0020327 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(); } } } framework/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php 0000644 00000016303 15060132305 0021351 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 void */ protected function clearInterruptSignal() { $this->cache->forget('illuminate:schedule:interrupt'); } } framework/src/Illuminate/Console/Scheduling/ScheduleTestCommand.php 0000644 00000006527 15060132305 0021533 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 ); } } } framework/src/Illuminate/Console/Scheduling/CacheAware.php 0000644 00000000356 15060132305 0017615 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); } framework/src/Illuminate/Console/Scheduling/ScheduleInterruptCommand.php 0000644 00000002406 15060132305 0022600 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.'); } } framework/src/Illuminate/Console/Scheduling/ScheduleWorkCommand.php 0000644 00000004431 15060132305 0021526 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]); } } } } } framework/src/Illuminate/Console/Scheduling/Schedule.php 0000644 00000024351 15060132305 0017367 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) { $jobName = $job; if (! is_string($job)) { $jobName = method_exists($job, 'displayName') ? $job->displayName() : $job::class; } 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($jobName); } /** * 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; } } framework/src/Illuminate/Console/Scheduling/ScheduleListCommand.php 0000644 00000022214 15060132305 0021516 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 $event * @param int $terminalWidth * @param array $expressionSpacing * @param int $repeatExpressionSpacing * @param \DateTimeZone $timezone * @return array */ 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; } } framework/src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php 0000644 00000003273 15060132305 0021667 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; } } framework/src/Illuminate/Console/Scheduling/ManagesFrequencies.php 0000644 00000034755 15060132305 0021411 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 string $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)); } } framework/src/Illuminate/Console/Scheduling/Event.php 0000644 00000061210 15060132305 0016707 0 ustar 00 <?php namespace Illuminate\Console\Scheduling; use Closure; use Cron\CronExpression; use GuzzleHttp\Client as HttpClient; use GuzzleHttp\ClientInterface as HttpClientInterface; 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 Illuminate\Support\Traits\Tappable; use Psr\Http\Client\ClientExceptionInterface; use Symfony\Component\Process\Process; use Throwable; class Event { use Macroable, ManagesFrequencies, ReflectsClosures, Tappable; /** * 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) use ($url) { try { $this->getHttpClient($container)->request('GET', $url); } catch (ClientExceptionInterface|TransferException $e) { $container->make(ExceptionHandler::class)->report($e); } }; } /** * Get the Guzzle HTTP client to use to send pings. * * @param \Illuminate\Contracts\Container\Container $container * @return \GuzzleHttp\ClientInterface */ protected function getHttpClient(Container $container) { return match (true) { $container->bound(HttpClientInterface::class) => $container->make(HttpClientInterface::class), $container->bound(HttpClient::class) => $container->make(HttpClient::class), default => new HttpClient([ 'connect_timeout' => 10, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, 'timeout' => 30, ]), }; } /** * 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); } } } framework/src/Illuminate/Console/Scheduling/EventMutex.php 0000644 00000001255 15060132305 0017735 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); } framework/src/Illuminate/Console/Concerns/InteractsWithIO.php 0000644 00000026247 15060132305 0020346 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; } } framework/src/Illuminate/Console/Concerns/InteractsWithSignals.php 0000644 00000002351 15060132305 0021425 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. * * @template TSignals of iterable<array-key, int>|int * * @param (\Closure():(TSignals))|TSignals $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(value($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; } } } framework/src/Illuminate/Console/Concerns/CallsCommands.php 0000644 00000005740 15060132305 0020041 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(); } } framework/src/Illuminate/Console/Concerns/PromptsForMissingInput.php 0000644 00000007404 15060132305 0022005 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Closure; use Illuminate\Contracts\Console\PromptsForMissingInput as PromptsForMissingInputContract; use Illuminate\Support\Arr; use Symfony\Component\Console\Input\InputArgument; 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()) ->reject(fn (InputArgument $argument) => $argument->getName() === 'command') ->filter(fn (InputArgument $argument) => $argument->isRequired() && match (true) { $argument->isArray() => empty($input->getArgument($argument->getName())), default => is_null($input->getArgument($argument->getName())), }) ->each(function (InputArgument $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(), $argument->isArray() ? Arr::wrap($label()) : $label()); } if (is_array($label)) { [$label, $placeholder] = $label; } $answer = text( label: $label, placeholder: $placeholder ?? '', validate: fn ($value) => empty($value) ? "The {$argument->getName()} is required." : null, ); $input->setArgument($argument->getName(), $argument->isArray() ? [$answer] : $answer); }) ->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(); } } framework/src/Illuminate/Console/Concerns/ConfiguresPrompts.php 0000644 00000021210 15060132305 0021000 0 ustar 00 <?php namespace Illuminate\Console\Concerns; use Illuminate\Console\PromptValidationException; 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\TextareaPrompt; use Laravel\Prompts\TextPrompt; use stdClass; 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::validateUsing(fn (Prompt $prompt) => $this->validatePrompt($prompt->value(), $prompt->validate)); 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 )); TextareaPrompt::fallbackUsing(fn (TextareaPrompt $prompt) => $this->promptUntilValid( fn () => $this->components->ask($prompt->label, $prompt->default ?: null, multiline: true) ?? '', $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->selectFallback($prompt->label, $prompt->options, $prompt->default), false, $prompt->validate )); MultiSelectPrompt::fallbackUsing(fn (MultiSelectPrompt $prompt) => $this->promptUntilValid( fn () => $this->multiselectFallback($prompt->label, $prompt->options, $prompt->default, $prompt->required), $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->selectFallback($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); return $this->multiselectFallback($prompt->label, $options, required: $prompt->required); }, $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.'); if ($this->laravel->runningUnitTests()) { throw new PromptValidationException; } else { continue; } } $error = is_callable($validate) ? $validate($result) : $this->validatePrompt($result, $validate); if (is_string($error) && strlen($error) > 0) { $this->components->error($error); if ($this->laravel->runningUnitTests()) { throw new PromptValidationException; } else { continue; } } return $result; } } /** * Validate the given prompt value using the validator. * * @param mixed $value * @param mixed $rules * @return ?string */ protected function validatePrompt($value, $rules) { if ($rules instanceof stdClass) { $messages = $rules->messages ?? []; $attributes = $rules->attributes ?? []; $rules = $rules->rules ?? null; } if (! $rules) { return; } $field = 'answer'; if (is_array($rules) && ! array_is_list($rules)) { [$field, $rules] = [key($rules), current($rules)]; } return $this->getPromptValidatorInstance( $field, $value, $rules, $messages ?? [], $attributes ?? [] )->errors()->first(); } /** * Get the validator instance that should be used to validate prompts. * * @param mixed $field * @param mixed $value * @param mixed $rules * @param array $messages * @param array $attributes * @return \Illuminate\Validation\Validator */ protected function getPromptValidatorInstance($field, $value, $rules, array $messages = [], array $attributes = []) { return $this->laravel['validator']->make( [$field => $value], [$field => $rules], empty($messages) ? $this->validationMessages() : $messages, empty($attributes) ? $this->validationAttributes() : $attributes, ); } /** * Get the validation messages that should be used during prompt validation. * * @return array */ protected function validationMessages() { return []; } /** * Get the validation attributes that should be used during prompt validation. * * @return array */ protected function validationAttributes() { return []; } /** * Restore the prompts output. * * @return void */ protected function restorePrompts() { Prompt::setOutput($this->output); } /** * Select fallback. * * @param string $label * @param array $options * @param string|int|null $default * @return string|int */ private function selectFallback($label, $options, $default = null) { $answer = $this->components->choice($label, $options, $default); if (! array_is_list($options) && $answer === (string) (int) $answer) { return (int) $answer; } return $answer; } /** * Multi-select fallback. * * @param string $label * @param array $options * @param array $default * @param bool|string $required * @return array */ private function multiselectFallback($label, $options, $default = [], $required = false) { $default = $default !== [] ? implode(',', $default) : null; if ($required === false && ! $this->laravel->runningUnitTests()) { $options = array_is_list($options) ? ['None', ...$options] : ['' => 'None'] + $options; if ($default === null) { $default = 'None'; } } $answers = $this->components->choice($label, $options, $default, null, true); if (! array_is_list($options)) { $answers = array_map(fn ($value) => $value === (string) (int) $value ? (int) $value : $value, $answers); } if ($required === false) { return array_is_list($options) ? array_values(array_filter($answers, fn ($value) => $value !== 'None')) : array_filter($answers, fn ($value) => $value !== ''); } return $answers; } } framework/src/Illuminate/Console/Concerns/HasParameters.php 0000644 00000002603 15060132305 0020053 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 []; } } framework/src/Illuminate/Console/Concerns/CreatesMatchingTest.php 0000644 00000002377 15060132305 0021225 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' => 'Test', 'pest' => 'Pest', 'phpunit' => 'PHPUnit'] 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') && ! $this->option('phpunit')) { return false; } return $this->call('make:test', [ 'name' => Str::of($path)->after($this->laravel['path'])->beforeLast('.php')->append('Test')->replace('\\', '/'), '--pest' => $this->option('pest'), '--phpunit' => $this->option('phpunit'), ]) == 0; } } framework/src/Illuminate/Console/CommandMutex.php 0000644 00000001207 15060132305 0016142 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); } framework/src/Illuminate/Console/Signals.php 0000644 00000007051 15060132305 0015144 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) $resolver * @return void */ public static function resolveAvailabilityUsing($resolver) { static::$availabilityResolver = $resolver; } } framework/src/Illuminate/Console/Events/ScheduledTaskStarting.php 0000644 00000000730 15060132305 0021244 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; } } framework/src/Illuminate/Console/Events/ScheduledTaskFinished.php 0000644 00000001215 15060132305 0021201 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; } } framework/src/Illuminate/Console/Events/CommandStarting.php 0000644 00000002002 15060132305 0020071 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; } } framework/src/Illuminate/Console/Events/CommandFinished.php 0000644 00000002254 15060132305 0020040 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; } } framework/src/Illuminate/Console/Events/ScheduledTaskSkipped.php 0000644 00000000727 15060132305 0021056 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; } } framework/src/Illuminate/Console/Events/ScheduledBackgroundTaskFinished.php 0000644 00000000741 15060132305 0023204 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; } } framework/src/Illuminate/Console/Events/ArtisanStarting.php 0000644 00000000652 15060132305 0020125 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; } } framework/src/Illuminate/Console/Events/ScheduledTaskFailed.php 0000644 00000001266 15060132305 0020642 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; } } framework/src/Illuminate/Console/LICENSE.md 0000644 00000002063 15060132305 0014435 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. framework/src/Illuminate/Console/OutputStyle.php 0000644 00000010707 15060132305 0016067 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} */ #[\Override] public function askQuestion(Question $question): mixed { try { return parent::askQuestion($question); } finally { $this->newLinesWritten++; } } /** * {@inheritdoc} */ #[\Override] public function write(string|iterable $messages, bool $newline = false, int $options = 0): void { $this->newLinesWritten = $this->trailingNewLineCount($messages) + (int) $newline; $this->newLineWritten = $this->newLinesWritten > 0; parent::write($messages, $newline, $options); } /** * {@inheritdoc} */ #[\Override] public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL): void { $this->newLinesWritten = $this->trailingNewLineCount($messages) + 1; $this->newLineWritten = true; parent::writeln($messages, $type); } /** * {@inheritdoc} */ #[\Override] public function newLine(int $count = 1): void { $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; } } framework/src/Illuminate/Console/QuestionHelper.php 0000644 00000005036 15060132305 0016514 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 */ #[\Override] protected function writePrompt(OutputInterface $output, Question $question): void { $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; } } framework/src/Illuminate/Console/ContainerCommandLoader.php 0000644 00000003440 15060132305 0020112 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); } } framework/src/Illuminate/Console/View/Components/Error.php 0000644 00000000735 15060132305 0017676 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); } } framework/src/Illuminate/Console/View/Components/Alert.php 0000644 00000001306 15060132305 0017647 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); } } framework/src/Illuminate/Console/View/Components/Mutators/EnsureNoPunctuation.php 0000644 00000000655 15060132305 0024414 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; } } framework/src/Illuminate/Console/View/Components/Mutators/EnsureRelativePaths.php 0000644 00000000676 15060132305 0024364 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; } } framework/src/Illuminate/Console/View/Components/Mutators/EnsureDynamicContentIsHighlighted.php 0000644 00000000575 15060132305 0027163 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); } } framework/src/Illuminate/Console/View/Components/Mutators/EnsurePunctuation.php 0000644 00000000620 15060132305 0024107 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; } } framework/src/Illuminate/Console/View/Components/Component.php 0000644 00000005643 15060132305 0020552 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); } } } framework/src/Illuminate/Console/View/Components/Confirm.php 0000644 00000000670 15060132305 0020200 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), ); } } framework/src/Illuminate/Console/View/Components/Secret.php 0000644 00000001077 15060132305 0020032 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)); } } framework/src/Illuminate/Console/View/Components/Warn.php 0000644 00000000750 15060132305 0017511 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); } } framework/src/Illuminate/Console/View/Components/AskWithCompletion.php 0000644 00000001357 15060132305 0022212 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) ); } } framework/src/Illuminate/Console/View/Components/Task.php 0000644 00000003501 15060132305 0017501 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Illuminate\Support\InteractsWithTime; use Symfony\Component\Console\Output\OutputInterface; use Throwable; use function Termwind\terminal; class Task extends Component { use InteractsWithTime; /** * 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 ? (' '.$this->runTimeForHumans($startTime)) : ''; $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, ); } } } framework/src/Illuminate/Console/View/Components/Info.php 0000644 00000000733 15060132305 0017476 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); } } framework/src/Illuminate/Console/View/Components/Choice.php 0000644 00000002567 15060132305 0020004 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( $this->getChoiceQuestion($question, $choices, $default) ->setMaxAttempts($attempts) ->setMultiselect($multiple) ), ); } /** * Get a ChoiceQuestion instance that handles array keys like Prompts. * * @param string $question * @param array $choices * @param mixed $default * @return \Symfony\Component\Console\Question\ChoiceQuestion */ protected function getChoiceQuestion($question, $choices, $default) { return new class($question, $choices, $default) extends ChoiceQuestion { protected function isAssoc(array $array): bool { return ! array_is_list($array); } }; } } framework/src/Illuminate/Console/View/Components/TwoColumnDetail.php 0000644 00000002005 15060132305 0021647 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); } } framework/src/Illuminate/Console/View/Components/Factory.php 0000644 00000004664 15060132305 0020221 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); } } framework/src/Illuminate/Console/View/Components/Line.php 0000644 00000002714 15060132305 0017473 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); } } framework/src/Illuminate/Console/View/Components/Ask.php 0000644 00000001134 15060132305 0017315 0 ustar 00 <?php namespace Illuminate\Console\View\Components; use Symfony\Component\Console\Question\Question; 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, $multiline = false) { return $this->usingQuestionHelper( fn () => $this->output->askQuestion( (new Question($question, $default)) ->setMultiline($multiline) ) ); } } framework/src/Illuminate/Console/View/Components/BulletList.php 0000644 00000001352 15060132305 0020664 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); } } framework/src/Illuminate/Console/MigrationGeneratorCommand.php 0000644 00000004714 15060132305 0020646 0 ustar 00 <?php namespace Illuminate\Console; use Illuminate\Filesystem\Filesystem; use function Illuminate\Filesystem\join_paths; abstract class MigrationGeneratorCommand extends Command { /** * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** * Create a new migration generator command instance. * * @param \Illuminate\Filesystem\Filesystem $files * @return void */ public function __construct(Filesystem $files) { parent::__construct(); $this->files = $files; } /** * 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( join_paths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php') )) !== 0; } } framework/src/Illuminate/Console/CacheCommandMutex.php 0000644 00000007130 15060132305 0017067 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; } } framework/src/Illuminate/Console/BufferedConsoleOutput.php 0000644 00000001372 15060132305 0020032 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} */ #[\Override] protected function doWrite(string $message, bool $newline): void { $this->buffer .= $message; if ($newline) { $this->buffer .= \PHP_EOL; } parent::doWrite($message, $newline); } } framework/src/Illuminate/Console/Command.php 0000755 00000020774 15060132305 0015134 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; use Throwable; 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 */ #[\Override] 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 */ #[\Override] protected function execute(InputInterface $input, OutputInterface $output): int { 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]); } catch (ManuallyFailedException $e) { $this->components->error($e->getMessage()); return static::FAILURE; } 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; } /** * Fail the command manually. * * @param \Throwable|string|null $exception * @return void */ public function fail(Throwable|string|null $exception = null) { if (is_null($exception)) { $exception = 'Command failed manually.'; } if (is_string($exception)) { $exception = new ManuallyFailedException($exception); } throw $exception; } /** * {@inheritdoc} * * @return bool */ #[\Override] public function isHidden(): bool { return $this->hidden; } /** * {@inheritdoc} */ #[\Override] 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; } } framework/src/Illuminate/Console/ManuallyFailedException.php 0000644 00000000177 15060132305 0020314 0 ustar 00 <?php namespace Illuminate\Console; use RuntimeException; class ManuallyFailedException extends RuntimeException { // } framework/src/Illuminate/Console/composer.json 0000755 00000003302 15060132305 0015553 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.2", "ext-mbstring": "*", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0", "illuminate/view": "^11.0", "laravel/prompts": "^0.1.12", "nunomaduro/termwind": "^2.0", "symfony/console": "^7.0", "symfony/polyfill-php83": "^1.28", "symfony/process": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Console\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.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.8).", "illuminate/bus": "Required to use the scheduled job dispatcher (^11.0).", "illuminate/container": "Required to use the scheduler (^11.0).", "illuminate/filesystem": "Required to use the generator command (^11.0).", "illuminate/queue": "Required to use closures for scheduled jobs (^11.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Console/Parser.php 0000644 00000011157 15060132305 0015002 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, '']; } } framework/src/Illuminate/Console/resources/views/components/two-column-detail.php 0000644 00000000504 15060132305 0024440 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> framework/src/Illuminate/Console/resources/views/components/alert.php 0000644 00000000203 15060132305 0022177 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> framework/src/Illuminate/Console/resources/views/components/bullet-list.php 0000644 00000000270 15060132305 0023334 0 ustar 00 <div> <?php foreach ($elements as $element) { ?> <div class="text-gray mx-2"> ⇂ <?php echo htmlspecialchars($element) ?> </div> <?php } ?> </div> framework/src/Illuminate/Console/resources/views/components/line.php 0000644 00000000460 15060132305 0022024 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> framework/src/Illuminate/Console/Prohibitable.php 0000644 00000001674 15060132305 0016155 0 ustar 00 <?php namespace Illuminate\Console; trait Prohibitable { /** * Indicates if the command should be prohibited from running. * * @var bool */ protected static $prohibitedFromRunning = false; /** * Indicate whether the command should be prohibited from running. * * @param bool $prohibit * @return bool */ public static function prohibit($prohibit = true) { static::$prohibitedFromRunning = $prohibit; } /** * Determine if the command is prohibited from running and display a warning if so. * * @param bool $quiet * @return bool */ protected function isProhibited(bool $quiet = false) { if (! static::$prohibitedFromRunning) { return false; } if (! $quiet) { $this->components->warn('This command is prohibited from running in this environment.'); } return true; } } framework/src/Illuminate/Console/Application.php 0000755 00000020605 15060132305 0016012 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|null */ #[\Override] public function add(SymfonyCommand $command): ?SymfonyCommand { 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())) { foreach (explode('|', $commandName) as $name) { $this->commandMap[$name] = $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 */ #[\Override] 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; } } framework/src/Illuminate/Console/ConfirmableTrait.php 0000644 00000002443 15060132305 0016771 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'; }; } } framework/src/Illuminate/Console/Contracts/NewLineAware.php 0000644 00000000577 15060132305 0020033 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(); } framework/src/Illuminate/Console/PromptValidationException.php 0000644 00000000172 15060132305 0020714 0 ustar 00 <?php namespace Illuminate\Console; use RuntimeException; class PromptValidationException extends RuntimeException { } framework/src/Illuminate/Console/GeneratorCommand.php 0000644 00000032713 15060132305 0016774 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', 'parent', '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))) { $this->handleTestCreation($path); } if (windows_os()) { $path = str_replace('/', '\\', $path); } $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(Finder::create()->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(Finder::create()->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 => '', }, ], ]; } } framework/src/Illuminate/Foundation/FileBasedMaintenanceMode.php 0000644 00000002616 15060132305 0021040 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'); } } framework/src/Illuminate/Foundation/ComposerScripts.php 0000644 00000003155 15060132305 0017410 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); } } } framework/src/Illuminate/Foundation/EnvironmentDetector.php 0000644 00000003621 15060132305 0020245 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)); } } } } framework/src/Illuminate/Foundation/Configuration/Middleware.php 0000644 00000047517 15060132305 0021147 0 ustar 00 <?php namespace Illuminate\Foundation\Configuration; use Closure; use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\Middleware\Authenticate; use Illuminate\Auth\Middleware\RedirectIfAuthenticated; use Illuminate\Cookie\Middleware\EncryptCookies; use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull; use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance; use Illuminate\Foundation\Http\Middleware\TrimStrings; use Illuminate\Foundation\Http\Middleware\ValidateCsrfToken; use Illuminate\Http\Middleware\TrustHosts; use Illuminate\Http\Middleware\TrustProxies; use Illuminate\Routing\Middleware\ValidateSignature; use Illuminate\Session\Middleware\AuthenticateSession; use Illuminate\Support\Arr; class Middleware { /** * The user defined global middleware stack. * * @var array */ protected $global = []; /** * The middleware that should be prepended to the global middleware stack. * * @var array */ protected $prepends = []; /** * The middleware that should be appended to the global middleware stack. * * @var array */ protected $appends = []; /** * The middleware that should be removed from the global middleware stack. * * @var array */ protected $removals = []; /** * The middleware that should be replaced in the global middleware stack. * * @var array */ protected $replacements = []; /** * The user defined middleware groups. * * @var array */ protected $groups = []; /** * The middleware that should be prepended to the specified groups. * * @var array */ protected $groupPrepends = []; /** * The middleware that should be appended to the specified groups. * * @var array */ protected $groupAppends = []; /** * The middleware that should be removed from the specified groups. * * @var array */ protected $groupRemovals = []; /** * The middleware that should be replaced in the specified groups. * * @var array */ protected $groupReplacements = []; /** * The Folio / page middleware for the application. * * @var array */ protected $pageMiddleware = []; /** * Indicates if the "trust hosts" middleware is enabled. * * @var bool */ protected $trustHosts = false; /** * Indicates if Sanctum's frontend state middleware is enabled. * * @var bool */ protected $statefulApi = false; /** * Indicates the API middleware group's rate limiter. * * @var string */ protected $apiLimiter; /** * Indicates if Redis throttling should be applied. * * @var bool */ protected $throttleWithRedis = false; /** * Indicates if sessions should be authenticated for the "web" middleware group. * * @var bool */ protected $authenticatedSessions = false; /** * The custom middleware aliases. * * @var array */ protected $customAliases = []; /** * The custom middleware priority definition. * * @var array */ protected $priority = []; /** * Prepend middleware to the application's global middleware stack. * * @param array|string $middleware * @return $this */ public function prepend(array|string $middleware) { $this->prepends = array_merge( Arr::wrap($middleware), $this->prepends ); return $this; } /** * Append middleware to the application's global middleware stack. * * @param array|string $middleware * @return $this */ public function append(array|string $middleware) { $this->appends = array_merge( $this->appends, Arr::wrap($middleware) ); return $this; } /** * Remove middleware from the application's global middleware stack. * * @param array|string $middleware * @return $this */ public function remove(array|string $middleware) { $this->removals = array_merge( $this->removals, Arr::wrap($middleware) ); return $this; } /** * Specify a middleware that should be replaced with another middleware. * * @param string $search * @param string $replace * @return $this */ public function replace(string $search, string $replace) { $this->replacements[$search] = $replace; return $this; } /** * Define the global middleware for the application. * * @param array $middleware * @return $this */ public function use(array $middleware) { $this->global = $middleware; return $this; } /** * Define a middleware group. * * @param string $group * @param array $middleware * @return $this */ public function group(string $group, array $middleware) { $this->groups[$group] = $middleware; return $this; } /** * Prepend the given middleware to the specified group. * * @param string $group * @param array|string $middleware * @return $this */ public function prependToGroup(string $group, array|string $middleware) { $this->groupPrepends[$group] = array_merge( Arr::wrap($middleware), $this->groupPrepends[$group] ?? [] ); return $this; } /** * Append the given middleware to the specified group. * * @param string $group * @param array|string $middleware * @return $this */ public function appendToGroup(string $group, array|string $middleware) { $this->groupAppends[$group] = array_merge( $this->groupAppends[$group] ?? [], Arr::wrap($middleware) ); return $this; } /** * Remove the given middleware from the specified group. * * @param string $group * @param array|string $middleware * @return $this */ public function removeFromGroup(string $group, array|string $middleware) { $this->groupRemovals[$group] = array_merge( Arr::wrap($middleware), $this->groupRemovals[$group] ?? [] ); return $this; } /** * Replace the given middleware in the specified group with another middleware. * * @param string $group * @param string $search * @param string $replace * @return $this */ public function replaceInGroup(string $group, string $search, string $replace) { $this->groupReplacements[$group][$search] = $replace; return $this; } /** * Modify the middleware in the "web" group. * * @param array|string $append * @param array|string $prepend * @param array|string $remove * @param array $replace * @return $this */ public function web(array|string $append = [], array|string $prepend = [], array|string $remove = [], array $replace = []) { return $this->modifyGroup('web', $append, $prepend, $remove, $replace); } /** * Modify the middleware in the "api" group. * * @param array|string $append * @param array|string $prepend * @param array|string $remove * @param array $replace * @return $this */ public function api(array|string $append = [], array|string $prepend = [], array|string $remove = [], array $replace = []) { return $this->modifyGroup('api', $append, $prepend, $remove, $replace); } /** * Modify the middleware in the given group. * * @param string $group * @param array|string $append * @param array|string $prepend * @param array|string $remove * @param array $replace * @return $this */ protected function modifyGroup(string $group, array|string $append, array|string $prepend, array|string $remove, array $replace) { if (! empty($append)) { $this->appendToGroup($group, $append); } if (! empty($prepend)) { $this->prependToGroup($group, $prepend); } if (! empty($remove)) { $this->removeFromGroup($group, $remove); } if (! empty($replace)) { foreach ($replace as $search => $replace) { $this->replaceInGroup($group, $search, $replace); } } return $this; } /** * Register the Folio / page middleware for the application. * * @param array $middleware * @return $this */ public function pages(array $middleware) { $this->pageMiddleware = $middleware; return $this; } /** * Register additional middleware aliases. * * @param array $aliases * @return $this */ public function alias(array $aliases) { $this->customAliases = $aliases; return $this; } /** * Define the middleware priority for the application. * * @param array $priority * @return $this */ public function priority(array $priority) { $this->priority = $priority; return $this; } /** * Get the global middleware. * * @return array */ public function getGlobalMiddleware() { $middleware = $this->global ?: array_values(array_filter([ $this->trustHosts ? \Illuminate\Http\Middleware\TrustHosts::class : null, \Illuminate\Http\Middleware\TrustProxies::class, \Illuminate\Http\Middleware\HandleCors::class, \Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance::class, \Illuminate\Http\Middleware\ValidatePostSize::class, \Illuminate\Foundation\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, ])); $middleware = array_map(function ($middleware) { return $this->replacements[$middleware] ?? $middleware; }, $middleware); return array_values(array_filter( array_diff( array_unique(array_merge($this->prepends, $middleware, $this->appends)), $this->removals ) )); } /** * Get the middleware groups. * * @return array */ public function getMiddlewareGroups() { $middleware = [ 'web' => array_values(array_filter([ \Illuminate\Cookie\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, $this->authenticatedSessions ? 'auth.session' : null, ])), 'api' => array_values(array_filter([ $this->statefulApi ? \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class : null, $this->apiLimiter ? 'throttle:'.$this->apiLimiter : null, \Illuminate\Routing\Middleware\SubstituteBindings::class, ])), ]; $middleware = array_merge($middleware, $this->groups); foreach ($middleware as $group => $groupedMiddleware) { foreach ($groupedMiddleware as $index => $groupMiddleware) { if (isset($this->groupReplacements[$group][$groupMiddleware])) { $middleware[$group][$index] = $this->groupReplacements[$group][$groupMiddleware]; } } } foreach ($this->groupRemovals as $group => $removals) { $middleware[$group] = array_values(array_filter( array_diff($middleware[$group] ?? [], $removals) )); } foreach ($this->groupPrepends as $group => $prepends) { $middleware[$group] = array_values(array_filter( array_unique(array_merge($prepends, $middleware[$group] ?? [])) )); } foreach ($this->groupAppends as $group => $appends) { $middleware[$group] = array_values(array_filter( array_unique(array_merge($middleware[$group] ?? [], $appends)) )); } return $middleware; } /** * Configure where guests are redirected by the "auth" middleware. * * @param callable|string $redirect * @return $this */ public function redirectGuestsTo(callable|string $redirect) { return $this->redirectTo(guests: $redirect); } /** * Configure where users are redirected by the "guest" middleware. * * @param callable|string $redirect * @return $this */ public function redirectUsersTo(callable|string $redirect) { return $this->redirectTo(users: $redirect); } /** * Configure where users are redirected by the authentication and guest middleware. * * @param callable|string $guests * @param callable|string $users * @return $this */ public function redirectTo(callable|string|null $guests = null, callable|string|null $users = null) { $guests = is_string($guests) ? fn () => $guests : $guests; $users = is_string($users) ? fn () => $users : $users; if ($guests) { Authenticate::redirectUsing($guests); AuthenticateSession::redirectUsing($guests); AuthenticationException::redirectUsing($guests); } if ($users) { RedirectIfAuthenticated::redirectUsing($users); } return $this; } /** * Configure the cookie encryption middleware. * * @param array<int, string> $except * @return $this */ public function encryptCookies(array $except = []) { EncryptCookies::except($except); return $this; } /** * Configure the CSRF token validation middleware. * * @param array $except * @return $this */ public function validateCsrfTokens(array $except = []) { ValidateCsrfToken::except($except); return $this; } /** * Configure the URL signature validation middleware. * * @param array $except * @return $this */ public function validateSignatures(array $except = []) { ValidateSignature::except($except); return $this; } /** * Configure the empty string conversion middleware. * * @param array<int, (\Closure(\Illuminate\Http\Request): bool)> $except * @return $this */ public function convertEmptyStringsToNull(array $except = []) { collect($except)->each(fn (Closure $callback) => ConvertEmptyStringsToNull::skipWhen($callback)); return $this; } /** * Configure the string trimming middleware. * * @param array<int, (\Closure(\Illuminate\Http\Request): bool)|string> $except * @return $this */ public function trimStrings(array $except = []) { [$skipWhen, $except] = collect($except)->partition(fn ($value) => $value instanceof Closure); $skipWhen->each(fn (Closure $callback) => TrimStrings::skipWhen($callback)); TrimStrings::except($except->all()); return $this; } /** * Indicate that the trusted host middleware should be enabled. * * @param array<int, string>|(callable(): array<int, string>)|null $at * @param bool $subdomains * @return $this */ public function trustHosts(array|callable|null $at = null, bool $subdomains = true) { $this->trustHosts = true; if (! is_null($at)) { TrustHosts::at($at, $subdomains); } return $this; } /** * Configure the trusted proxies for the application. * * @param array<int, string>|string|null $at * @param int|null $headers * @return $this */ public function trustProxies(array|string|null $at = null, ?int $headers = null) { if (! is_null($at)) { TrustProxies::at($at); } if (! is_null($headers)) { TrustProxies::withHeaders($headers); } return $this; } /** * Configure the middleware that prevents requests during maintenance mode. * * @param array<int, string> $except * @return $this */ public function preventRequestsDuringMaintenance(array $except = []) { PreventRequestsDuringMaintenance::except($except); return $this; } /** * Indicate that Sanctum's frontend state middleware should be enabled. * * @return $this */ public function statefulApi() { $this->statefulApi = true; return $this; } /** * Indicate that the API middleware group's throttling middleware should be enabled. * * @param string $limiter * @param bool $redis * @return $this */ public function throttleApi($limiter = 'api', $redis = false) { $this->apiLimiter = $limiter; if ($redis) { $this->throttleWithRedis(); } return $this; } /** * Indicate that Laravel's throttling middleware should use Redis. * * @return $this */ public function throttleWithRedis() { $this->throttleWithRedis = true; return $this; } /** * Indicate that sessions should be authenticated for the "web" middleware group. * * @return $this */ public function authenticateSessions() { $this->authenticatedSessions = true; return $this; } /** * Get the Folio / page middleware for the application. * * @return array */ public function getPageMiddleware() { return $this->pageMiddleware; } /** * Get the middleware aliases. * * @return array */ public function getMiddlewareAliases() { return array_merge($this->defaultAliases(), $this->customAliases); } /** * Get the default middleware aliases. * * @return array */ protected function defaultAliases() { $aliases = [ 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \Illuminate\Auth\Middleware\RedirectIfAuthenticated::class, 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, 'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => $this->throttleWithRedis ? \Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class : \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, ]; if (class_exists(\Spark\Http\Middleware\VerifyBillableIsSubscribed::class)) { $aliases['subscribed'] = \Spark\Http\Middleware\VerifyBillableIsSubscribed::class; } return $aliases; } /** * Get the middleware priority for the application. * * @return array */ public function getMiddlewarePriority() { return $this->priority; } } framework/src/Illuminate/Foundation/Configuration/Exceptions.php 0000644 00000011056 15060132305 0021200 0 ustar 00 <?php namespace Illuminate\Foundation\Configuration; use Closure; use Illuminate\Foundation\Exceptions\Handler; use Illuminate\Support\Arr; class Exceptions { /** * Create a new exception handling configuration instance. * * @param \Illuminate\Foundation\Exceptions\Handler $handler * @return void */ public function __construct(public Handler $handler) { } /** * Register a reportable callback. * * @param callable $using * @return \Illuminate\Foundation\Exceptions\ReportableHandler */ public function report(callable $using) { return $this->handler->reportable($using); } /** * Register a reportable callback. * * @param callable $reportUsing * @return \Illuminate\Foundation\Exceptions\ReportableHandler */ public function reportable(callable $reportUsing) { return $this->handler->reportable($reportUsing); } /** * Register a renderable callback. * * @param callable $using * @return $this */ public function render(callable $using) { $this->handler->renderable($using); return $this; } /** * Register a renderable callback. * * @param callable $renderUsing * @return $this */ public function renderable(callable $renderUsing) { $this->handler->renderable($renderUsing); return $this; } /** * Register a callback to prepare the final, rendered exception response. * * @param callable $using * @return $this */ public function respond(callable $using) { $this->handler->respondUsing($using); return $this; } /** * Specify the callback that should be used to throttle reportable exceptions. * * @param callable $throttleUsing * @return $this */ public function throttle(callable $throttleUsing) { $this->handler->throttleUsing($throttleUsing); 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) { $this->handler->map($from, $to); 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(string $type, string $level) { $this->handler->level($type, $level); return $this; } /** * Register a closure that should be used to build exception context data. * * @param \Closure $contextCallback * @return $this */ public function context(Closure $contextCallback) { $this->handler->buildContextUsing($contextCallback); return $this; } /** * Indicate that the given exception type should not be reported. * * @param array|string $class * @return $this */ public function dontReport(array|string $class) { foreach (Arr::wrap($class) as $exceptionClass) { $this->handler->dontReport($exceptionClass); } return $this; } /** * Do not report duplicate exceptions. * * @return $this */ public function dontReportDuplicates() { $this->handler->dontReportDuplicates(); return $this; } /** * Indicate that the given attributes should never be flashed to the session on validation errors. * * @param array|string $attributes * @return $this */ public function dontFlash(array|string $attributes) { $this->handler->dontFlash($attributes); return $this; } /** * Register the callable that determines if the exception handler response should be JSON. * * @param callable(\Illuminate\Http\Request $request, \Throwable): bool $callback * @return $this */ public function shouldRenderJsonWhen(callable $callback) { $this->handler->shouldRenderJsonWhen($callback); return $this; } /** * Indicate that the given exception class should not be ignored. * * @param array<int, class-string<\Throwable>>|class-string<\Throwable> $class * @return $this */ public function stopIgnoring(array|string $class) { $this->handler->stopIgnoring($class); return $this; } } framework/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php 0000644 00000030431 15060132305 0022627 0 ustar 00 <?php namespace Illuminate\Foundation\Configuration; use Closure; use Illuminate\Console\Application as Artisan; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Contracts\Console\Kernel as ConsoleKernel; use Illuminate\Contracts\Http\Kernel as HttpKernel; use Illuminate\Foundation\Application; use Illuminate\Foundation\Bootstrap\RegisterProviders; use Illuminate\Foundation\Events\DiagnosingHealth; use Illuminate\Foundation\Support\Providers\EventServiceProvider as AppEventServiceProvider; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as AppRouteServiceProvider; use Illuminate\Support\Facades\Broadcast; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\View; use Laravel\Folio\Folio; class ApplicationBuilder { /** * The service provider that are marked for registration. * * @var array */ protected array $pendingProviders = []; /** * The Folio / page middleware that have been defined by the user. * * @var array */ protected array $pageMiddleware = []; /** * Create a new application builder instance. */ public function __construct(protected Application $app) { } /** * Register the standard kernel classes for the application. * * @return $this */ public function withKernels() { $this->app->singleton( \Illuminate\Contracts\Http\Kernel::class, \Illuminate\Foundation\Http\Kernel::class, ); $this->app->singleton( \Illuminate\Contracts\Console\Kernel::class, \Illuminate\Foundation\Console\Kernel::class, ); return $this; } /** * Register additional service providers. * * @param array $providers * @param bool $withBootstrapProviders * @return $this */ public function withProviders(array $providers = [], bool $withBootstrapProviders = true) { RegisterProviders::merge( $providers, $withBootstrapProviders ? $this->app->getBootstrapProvidersPath() : null ); return $this; } /** * Register the core event service provider for the application. * * @param array|bool $discover * @return $this */ public function withEvents(array|bool $discover = []) { if (is_array($discover) && count($discover) > 0) { AppEventServiceProvider::setEventDiscoveryPaths($discover); } if ($discover === false) { AppEventServiceProvider::disableEventDiscovery(); } if (! isset($this->pendingProviders[AppEventServiceProvider::class])) { $this->app->booting(function () { $this->app->register(AppEventServiceProvider::class); }); } $this->pendingProviders[AppEventServiceProvider::class] = true; return $this; } /** * Register the broadcasting services for the application. * * @param string $channels * @param array $attributes * @return $this */ public function withBroadcasting(string $channels, array $attributes = []) { $this->app->booted(function () use ($channels, $attributes) { Broadcast::routes(! empty($attributes) ? $attributes : null); if (file_exists($channels)) { require $channels; } }); return $this; } /** * Register the routing services for the application. * * @param \Closure|null $using * @param array|string|null $web * @param array|string|null $api * @param string|null $commands * @param string|null $channels * @param string|null $pages * @param string $apiPrefix * @param callable|null $then * @return $this */ public function withRouting(?Closure $using = null, array|string|null $web = null, array|string|null $api = null, ?string $commands = null, ?string $channels = null, ?string $pages = null, ?string $health = null, string $apiPrefix = 'api', ?callable $then = null) { if (is_null($using) && (is_string($web) || is_array($web) || is_string($api) || is_array($api) || is_string($pages) || is_string($health)) || is_callable($then)) { $using = $this->buildRoutingCallback($web, $api, $pages, $health, $apiPrefix, $then); } AppRouteServiceProvider::loadRoutesUsing($using); $this->app->booting(function () { $this->app->register(AppRouteServiceProvider::class, force: true); }); if (is_string($commands) && realpath($commands) !== false) { $this->withCommands([$commands]); } if (is_string($channels) && realpath($channels) !== false) { $this->withBroadcasting($channels); } return $this; } /** * Create the routing callback for the application. * * @param array|string|null $web * @param array|string|null $api * @param string|null $pages * @param string|null $health * @param string $apiPrefix * @param callable|null $then * @return \Closure */ protected function buildRoutingCallback(array|string|null $web, array|string|null $api, ?string $pages, ?string $health, string $apiPrefix, ?callable $then) { return function () use ($web, $api, $pages, $health, $apiPrefix, $then) { if (is_string($api) || is_array($api)) { if (is_array($api)) { foreach ($api as $apiRoute) { if (realpath($apiRoute) !== false) { Route::middleware('api')->prefix($apiPrefix)->group($apiRoute); } } } else { Route::middleware('api')->prefix($apiPrefix)->group($api); } } if (is_string($health)) { Route::middleware('web')->get($health, function () { Event::dispatch(new DiagnosingHealth); return View::file(__DIR__.'/../resources/health-up.blade.php'); }); } if (is_string($web) || is_array($web)) { if (is_array($web)) { foreach ($web as $webRoute) { if (realpath($webRoute) !== false) { Route::middleware('web')->group($webRoute); } } } else { Route::middleware('web')->group($web); } } if (is_string($pages) && realpath($pages) !== false && class_exists(Folio::class)) { Folio::route($pages, middleware: $this->pageMiddleware); } if (is_callable($then)) { $then($this->app); } }; } /** * Register the global middleware, middleware groups, and middleware aliases for the application. * * @param callable|null $callback * @return $this */ public function withMiddleware(?callable $callback = null) { $this->app->afterResolving(HttpKernel::class, function ($kernel) use ($callback) { $middleware = (new Middleware) ->redirectGuestsTo(fn () => route('login')); if (! is_null($callback)) { $callback($middleware); } $this->pageMiddleware = $middleware->getPageMiddleware(); $kernel->setGlobalMiddleware($middleware->getGlobalMiddleware()); $kernel->setMiddlewareGroups($middleware->getMiddlewareGroups()); $kernel->setMiddlewareAliases($middleware->getMiddlewareAliases()); if ($priorities = $middleware->getMiddlewarePriority()) { $kernel->setMiddlewarePriority($priorities); } }); return $this; } /** * Register additional Artisan commands with the application. * * @param array $commands * @return $this */ public function withCommands(array $commands = []) { if (empty($commands)) { $commands = [$this->app->path('Console/Commands')]; } $this->app->afterResolving(ConsoleKernel::class, function ($kernel) use ($commands) { [$commands, $paths] = collect($commands)->partition(fn ($command) => class_exists($command)); [$routes, $paths] = $paths->partition(fn ($path) => is_file($path)); $this->app->booted(static function () use ($kernel, $commands, $paths, $routes) { $kernel->addCommands($commands->all()); $kernel->addCommandPaths($paths->all()); $kernel->addCommandRoutePaths($routes->all()); }); }); return $this; } /** * Register additional Artisan route paths. * * @param array $paths * @return $this */ protected function withCommandRouting(array $paths) { $this->app->afterResolving(ConsoleKernel::class, function ($kernel) use ($paths) { $this->app->booted(fn () => $kernel->addCommandRoutePaths($paths)); }); } /** * Register the scheduled tasks for the application. * * @param callable(\Illuminate\Console\Scheduling\Schedule $schedule): void $callback * @return $this */ public function withSchedule(callable $callback) { Artisan::starting(fn () => $callback($this->app->make(Schedule::class))); return $this; } /** * Register and configure the application's exception handler. * * @param callable|null $using * @return $this */ public function withExceptions(?callable $using = null) { $this->app->singleton( \Illuminate\Contracts\Debug\ExceptionHandler::class, \Illuminate\Foundation\Exceptions\Handler::class ); $using ??= fn () => true; $this->app->afterResolving( \Illuminate\Foundation\Exceptions\Handler::class, fn ($handler) => $using(new Exceptions($handler)), ); return $this; } /** * Register an array of container bindings to be bound when the application is booting. * * @param array $bindings * @return $this */ public function withBindings(array $bindings) { return $this->registered(function ($app) use ($bindings) { foreach ($bindings as $abstract => $concrete) { $app->bind($abstract, $concrete); } }); } /** * Register an array of singleton container bindings to be bound when the application is booting. * * @param array $singletons * @return $this */ public function withSingletons(array $singletons) { return $this->registered(function ($app) use ($singletons) { foreach ($singletons as $abstract => $concrete) { if (is_string($abstract)) { $app->singleton($abstract, $concrete); } else { $app->singleton($concrete); } } }); } /** * Register a callback to be invoked when the application's service providers are registered. * * @param callable $callback * @return $this */ public function registered(callable $callback) { $this->app->registered($callback); return $this; } /** * Register a callback to be invoked when the application is "booting". * * @param callable $callback * @return $this */ public function booting(callable $callback) { $this->app->booting($callback); return $this; } /** * Register a callback to be invoked when the application is "booted". * * @param callable $callback * @return $this */ public function booted(callable $callback) { $this->app->booted($callback); return $this; } /** * Get the application instance. * * @return \Illuminate\Foundation\Application */ public function create() { return $this->app; } } framework/src/Illuminate/Foundation/stubs/facade.stub 0000644 00000000460 15060132305 0016776 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'; } } framework/src/Illuminate/Foundation/Support/Providers/AuthServiceProvider.php 0000644 00000001755 15060132305 0023643 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; } } framework/src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php 0000644 00000012113 15060132305 0024011 0 ustar 00 <?php namespace Illuminate\Foundation\Support\Providers; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Events\DiscoverEvents; use Illuminate\Support\Arr; 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 = []; /** * Indicates if events should be discovered. * * @var bool */ protected static $shouldDiscoverEvents = true; /** * The configured event discovery paths. * * @var array|null */ protected static $eventDiscoveryPaths; /** * 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); } }); $this->booted(function () { $this->configureEmailVerification(); }); } /** * 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 get_class($this) === __CLASS__ && static::$shouldDiscoverEvents === true; } /** * 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 static::$eventDiscoveryPaths ?: [ $this->app->path('Listeners'), ]; } /** * Set the globally configured event discovery paths. * * @param array $paths * @return void */ public static function setEventDiscoveryPaths(array $paths) { static::$eventDiscoveryPaths = $paths; } /** * Get the base path to be used during event discovery. * * @return string */ protected function eventDiscoveryBasePath() { return base_path(); } /** * Disable event discovery for the application. * * @return void */ public static function disableEventDiscovery() { static::$shouldDiscoverEvents = false; } /** * Configure the proper event listeners for email verification. * * @return void */ protected function configureEmailVerification() { if (! isset($this->listen[Registered::class]) || ! in_array(SendEmailVerificationNotification::class, Arr::wrap($this->listen[Registered::class]))) { Event::listen(Registered::class, SendEmailVerificationNotification::class); } } } framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php 0000644 00000010504 15060132305 0024030 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; /** * The global callback that should be used to load the application's routes. * * @var \Closure|null */ protected static $alwaysLoadRoutesUsing; /** * The callback that should be used to load the application's cached routes. * * @var \Closure|null */ protected static $alwaysLoadCachedRoutesUsing; /** * 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; } /** * Register the callback that will be used to load the application's routes. * * @param \Closure|null $routesCallback * @return void */ public static function loadRoutesUsing(?Closure $routesCallback) { self::$alwaysLoadRoutesUsing = $routesCallback; } /** * Register the callback that will be used to load the application's cached routes. * * @param \Closure|null $routesCallback * @return void */ public static function loadCachedRoutesUsing(?Closure $routesCallback) { self::$alwaysLoadCachedRoutesUsing = $routesCallback; } /** * 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() { if (! is_null(self::$alwaysLoadCachedRoutesUsing)) { $this->app->call(self::$alwaysLoadCachedRoutesUsing); return; } $this->app->booted(function () { require $this->app->getCachedRoutesPath(); }); } /** * Load the application routes. * * @return void */ protected function loadRoutes() { if (! is_null(self::$alwaysLoadRoutesUsing)) { $this->app->call(self::$alwaysLoadRoutesUsing); } 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 ); } } framework/src/Illuminate/Foundation/Concerns/ResolvesDumpSource.php 0000644 00000012613 15060132305 0021633 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; } } framework/src/Illuminate/Foundation/Bus/Dispatchable.php 0000644 00000005403 15060132305 0017363 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); } } framework/src/Illuminate/Foundation/Bus/PendingChain.php 0000644 00000010273 15060132305 0017330 0 ustar 00 <?php namespace Illuminate\Foundation\Bus; use Closure; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Support\Traits\Conditionable; use Laravel\SerializableClosure\SerializableClosure; class PendingChain { use Conditionable; /** * 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 chain. * * @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); } /** * Dispatch the job chain if the given truth test passes. * * @param bool|\Closure $boolean * @return \Illuminate\Foundation\Bus\PendingDispatch|null */ public function dispatchIf($boolean) { return value($boolean) ? $this->dispatch() : null; } /** * Dispatch the job chain unless the given truth test passes. * * @param bool|\Closure $boolean * @return \Illuminate\Foundation\Bus\PendingDispatch|null */ public function dispatchUnless($boolean) { return ! value($boolean) ? $this->dispatch() : null; } } framework/src/Illuminate/Foundation/Bus/PendingDispatch.php 0000644 00000007661 15060132305 0020054 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); } } } framework/src/Illuminate/Foundation/Bus/PendingClosureDispatch.php 0000644 00000000564 15060132305 0021404 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; } } framework/src/Illuminate/Foundation/Bus/DispatchesJobs.php 0000644 00000001075 15060132305 0017706 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); } } framework/src/Illuminate/Foundation/MaintenanceModeManager.php 0000644 00000002215 15060132305 0020567 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'); } } framework/src/Illuminate/Foundation/Precognition.php 0000644 00000001076 15060132305 0016711 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']); } }; } } framework/src/Illuminate/Foundation/Events/DiscoverEvents.php 0000644 00000006571 15060132305 0020465 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( Finder::create()->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; } } framework/src/Illuminate/Foundation/Events/Dispatchable.php 0000644 00000002324 15060132305 0020075 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())); } } framework/src/Illuminate/Foundation/Events/PublishingStubs.php 0000644 00000001224 15060132305 0020635 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; } } framework/src/Illuminate/Foundation/Events/MaintenanceModeDisabled.php 0000644 00000000131 15060132305 0022163 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class MaintenanceModeDisabled { // } framework/src/Illuminate/Foundation/Events/DiagnosingHealth.php 0000644 00000000122 15060132305 0020714 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class DiagnosingHealth { // } framework/src/Illuminate/Foundation/Events/MaintenanceModeEnabled.php 0000644 00000000130 15060132305 0022005 0 ustar 00 <?php namespace Illuminate\Foundation\Events; class MaintenanceModeEnabled { // } framework/src/Illuminate/Foundation/Events/LocaleUpdated.php 0000644 00000000542 15060132305 0020220 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; } } framework/src/Illuminate/Foundation/Events/VendorTagPublished.php 0000644 00000001040 15060132305 0021235 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; } } framework/src/Illuminate/Foundation/CacheBasedMaintenanceMode.php 0000644 00000004101 15060132305 0021153 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); } } framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php 0000644 00000014554 15060132305 0022335 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\Foundation\Http\Middleware\Concerns\ExcludesPaths; use Illuminate\Session\TokenMismatchException; use Illuminate\Support\Arr; use Illuminate\Support\InteractsWithTime; use Symfony\Component\HttpFoundation\Cookie; class VerifyCsrfToken { use InteractsWithTime, ExcludesPaths; /** * 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. * * @var array<int, string> */ protected $except = []; /** * The globally ignored URIs that should be excluded from CSRF verification. * * @var array */ protected static $neverVerify = []; /** * 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(); } /** * Get the URIs that should be excluded. * * @return array */ public function getExcludedPaths() { return array_merge($this->except, static::$neverVerify); } /** * 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, $config['partitioned'] ?? false ); } /** * Indicate that the given URIs should be excluded from CSRF verification. * * @param array|string $uris * @return void */ public static function except($uris) { static::$neverVerify = array_values(array_unique( array_merge(static::$neverVerify, Arr::wrap($uris)) )); } /** * Determine if the cookie contents should be serialized. * * @return bool */ public static function serialized() { return EncryptCookies::serialized('XSRF-TOKEN'); } /** * Flush the state of the middleware. * * @return void */ public static function flushState() { static::$neverVerify = []; } } framework/src/Illuminate/Foundation/Http/Middleware/Concerns/ExcludesPaths.php 0000644 00000001424 15060132305 0023570 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware\Concerns; trait ExcludesPaths { /** * Determine if the request has a URI that should be excluded. * * @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; } /** * Get the URIs that should be excluded. * * @return array */ public function getExcludedPaths() { return $this->except ?? []; } } framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php 0000644 00000004060 15060132305 0022750 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; } } framework/src/Illuminate/Foundation/Http/Middleware/ValidateCsrfToken.php 0000644 00000000251 15060132305 0022607 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; /** * Alias of VerifyCsrfToken for consistency. */ class ValidateCsrfToken extends VerifyCsrfToken { // } framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php 0000644 00000005037 15060132305 0021533 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Str; class TrimStrings extends TransformsRequest { /** * The attributes that should not be trimmed. * * @var array<int, string> */ protected $except = [ 'current_password', 'password', 'password_confirmation', ]; /** * The globally ignored attributes that should not be trimmed. * * @var array */ protected static $neverTrim = []; /** * 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) { $except = array_merge($this->except, static::$neverTrim); if ($this->shouldSkip($key, $except) || ! is_string($value)) { return $value; } return Str::trim($value); } /** * Determine if the given key should be skipped. * * @param string $key * @param array $except * @return bool */ protected function shouldSkip($key, $except) { return in_array($key, $except, true); } /** * Indicate that the given attributes should never be trimmed. * * @param array|string $attributes * @return void */ public static function except($attributes) { static::$neverTrim = array_values(array_unique( array_merge(static::$neverTrim, Arr::wrap($attributes)) )); } /** * Register a callback that instructs the middleware to be skipped. * * @param \Closure $callback * @return void */ public static function skipWhen(Closure $callback) { static::$skipCallbacks[] = $callback; } /** * Flush the middleware's global state. * * @return void */ public static function flushState() { static::$neverTrim = []; static::$skipCallbacks = []; } } framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php 0000644 00000002540 15060132305 0024411 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; } /** * Flush the middleware's global state. * * @return void */ public static function flushState() { static::$skipCallbacks = []; } } framework/src/Illuminate/Foundation/Http/Middleware/HandlePrecognitiveRequests.php 0000644 00000004621 15060132305 0024552 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', ])))); } } framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php 0000644 00000011567 15060132305 0025746 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Closure; use ErrorException; use Illuminate\Contracts\Foundation\Application; use Illuminate\Foundation\Http\MaintenanceModeBypassCookie; use Illuminate\Foundation\Http\Middleware\Concerns\ExcludesPaths; use Illuminate\Support\Arr; use Symfony\Component\HttpKernel\Exception\HttpException; class PreventRequestsDuringMaintenance { use ExcludesPaths; /** * The application implementation. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * The URIs that should be excluded. * * @var array<int, string> */ protected $except = []; /** * The URIs that should be accessible during maintenance. * * @var array */ protected static $neverPrevent = []; /** * 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'] ); } /** * 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 excluded. * * @return array */ public function getExcludedPaths() { return array_merge($this->except, static::$neverPrevent); } /** * Indicate that the given URIs should always be accessible. * * @param array|string $uris * @return void */ public static function except($uris) { static::$neverPrevent = array_values(array_unique( array_merge(static::$neverPrevent, Arr::wrap($uris)) )); } /** * Flush the state of the middleware. * * @return void */ public static function flushState() { static::$neverPrevent = []; } } framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php 0000644 00000000256 15060132305 0022476 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; use Illuminate\Http\Middleware\ValidatePostSize as Middleware; class ValidatePostSize extends Middleware { // } framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php 0000644 00000000213 15060132305 0023671 0 ustar 00 <?php namespace Illuminate\Foundation\Http\Middleware; class CheckForMaintenanceMode extends PreventRequestsDuringMaintenance { // } framework/src/Illuminate/Foundation/Http/HtmlDumper.php 0000644 00000007117 15060132305 0017253 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); } } framework/src/Illuminate/Foundation/Http/Events/RequestHandled.php 0000644 00000001155 15060132305 0021342 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; } } framework/src/Illuminate/Foundation/Http/FormRequest.php 0000644 00000016317 15060132305 0017450 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 = $this->validationRules(); $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(); } /** * Get the validation rules for this form request. * * @return array */ protected function validationRules() { return method_exists($this, 'rules') ? $this->container->call([$this, 'rules']) : []; } /** * 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; } } framework/src/Illuminate/Foundation/Http/Kernel.php 0000644 00000037775 15060132305 0016427 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 global middleware. * * @return array */ public function getGlobalMiddleware() { return $this->middleware; } /** * Set the application's global middleware. * * @param array $middleware * @return $this */ public function setGlobalMiddleware(array $middleware) { $this->middleware = $middleware; $this->syncMiddlewareToRouter(); return $this; } /** * Get the application's route middleware groups. * * @return array */ public function getMiddlewareGroups() { return $this->middlewareGroups; } /** * Set the application's middleware groups. * * @param array $groups * @return $this */ public function setMiddlewareGroups(array $groups) { $this->middlewareGroups = $groups; $this->syncMiddlewareToRouter(); return $this; } /** * 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); } /** * Set the application's route middleware aliases. * * @param array $aliases * @return $this */ public function setMiddlewareAliases(array $aliases) { $this->middlewareAliases = $aliases; $this->syncMiddlewareToRouter(); return $this; } /** * Set the application's middleware priority. * * @param array $priority * @return $this */ public function setMiddlewarePriority(array $priority) { $this->middlewarePriority = $priority; $this->syncMiddlewareToRouter(); return $this; } /** * 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; } } framework/src/Illuminate/Foundation/Http/MaintenanceModeBypassCookie.php 0000644 00000002463 15060132305 0022534 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(); } } framework/src/Illuminate/Foundation/Auth/Access/AuthorizesRequests.php 0000644 00000007267 15060132305 0022254 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']; } } framework/src/Illuminate/Foundation/Auth/Access/Authorizable.php 0000644 00000002574 15060132305 0021010 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); } } framework/src/Illuminate/Foundation/Auth/User.php 0000644 00000001254 15060132305 0016066 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; } framework/src/Illuminate/Foundation/Auth/EmailVerificationRequest.php 0000644 00000002607 15060132305 0022116 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; } } framework/src/Illuminate/Foundation/PackageManifest.php 0000644 00000010524 15060132305 0017271 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).';' ); } } framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php 0000644 00000005617 15060132305 0022020 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); } } framework/src/Illuminate/Foundation/Routing/PrecognitionCallableDispatcher.php 0000644 00000001047 15060132305 0023765 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']); } } framework/src/Illuminate/Foundation/Routing/PrecognitionControllerDispatcher.php 0000644 00000002302 15060132305 0024404 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."); } } framework/src/Illuminate/Foundation/Vite.php 0000644 00000051412 15060132305 0015157 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 custom asset path resolver. * * @var callable|null */ protected $assetPathResolver = null; /** * 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; } /** * Resolve asset paths using the provided resolver. * * @param callable|null $resolver * @return $this */ public function createAssetPathsUsing($resolver) { $this->assetPathResolver = $resolver; 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 ($this->assetPathResolver ?? asset(...))($path, $secure); } /** * Get 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(); } } framework/src/Illuminate/Foundation/Inspiring.php 0000644 00000015062 15060132305 0016213 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), ); } } framework/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php 0000755 00000065322 15060132305 0022672 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\ApiInstallCommand; use Illuminate\Foundation\Console\BroadcastingInstallCommand; use Illuminate\Foundation\Console\CastMakeCommand; use Illuminate\Foundation\Console\ChannelListCommand; use Illuminate\Foundation\Console\ChannelMakeCommand; use Illuminate\Foundation\Console\ClassMakeCommand; use Illuminate\Foundation\Console\ClearCompiledCommand; use Illuminate\Foundation\Console\ComponentMakeCommand; use Illuminate\Foundation\Console\ConfigCacheCommand; use Illuminate\Foundation\Console\ConfigClearCommand; use Illuminate\Foundation\Console\ConfigPublishCommand; use Illuminate\Foundation\Console\ConfigShowCommand; use Illuminate\Foundation\Console\ConsoleMakeCommand; use Illuminate\Foundation\Console\DocsCommand; use Illuminate\Foundation\Console\DownCommand; use Illuminate\Foundation\Console\EnumMakeCommand; 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\InterfaceMakeCommand; 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\StorageUnlinkCommand; use Illuminate\Foundation\Console\StubPublishCommand; use Illuminate\Foundation\Console\TestMakeCommand; use Illuminate\Foundation\Console\TraitMakeCommand; 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, 'StorageUnlink' => StorageUnlinkCommand::class, 'Up' => UpCommand::class, 'ViewCache' => ViewCacheCommand::class, 'ViewClear' => ViewClearCommand::class, ]; /** * The commands to be registered. * * @var array */ protected $devCommands = [ 'ApiInstall' => ApiInstallCommand::class, 'BroadcastingInstall' => BroadcastingInstallCommand::class, 'CacheTable' => CacheTableCommand::class, 'CastMake' => CastMakeCommand::class, 'ChannelList' => ChannelListCommand::class, 'ChannelMake' => ChannelMakeCommand::class, 'ClassMake' => ClassMakeCommand::class, 'ComponentMake' => ComponentMakeCommand::class, 'ConfigPublish' => ConfigPublishCommand::class, 'ConsoleMake' => ConsoleMakeCommand::class, 'ControllerMake' => ControllerMakeCommand::class, 'Docs' => DocsCommand::class, 'EnumMake' => EnumMakeCommand::class, 'EventGenerate' => EventGenerateCommand::class, 'EventMake' => EventMakeCommand::class, 'ExceptionMake' => ExceptionMakeCommand::class, 'FactoryMake' => FactoryMakeCommand::class, 'InterfaceMake' => InterfaceMakeCommand::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, 'TraitMake' => TraitMakeCommand::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']); }); } /** * 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 registerClassMakeCommand() { $this->app->singleton(ClassMakeCommand::class, function ($app) { return new ClassMakeCommand($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 registerConfigPublishCommand() { $this->app->singleton(ConfigPublishCommand::class, function ($app) { return new ConfigPublishCommand; }); } /** * 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 registerEnumMakeCommand() { $this->app->singleton(EnumMakeCommand::class, function ($app) { return new EnumMakeCommand($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 registerInterfaceMakeCommand() { $this->app->singleton(InterfaceMakeCommand::class, function ($app) { return new InterfaceMakeCommand($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']); }); } /** * 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']); }); } /** * Register the command. * * @return void */ protected function registerQueueTableCommand() { $this->app->singleton(TableCommand::class, function ($app) { return new TableCommand($app['files']); }); } /** * Register the command. * * @return void */ protected function registerQueueBatchesTableCommand() { $this->app->singleton(BatchesTableCommand::class, function ($app) { return new BatchesTableCommand($app['files']); }); } /** * 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']); }); } /** * Register the command. * * @return void */ protected function registerSessionTableCommand() { $this->app->singleton(SessionTableCommand::class, function ($app) { return new SessionTableCommand($app['files']); }); } /** * 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 registerTraitMakeCommand() { $this->app->singleton(TraitMakeCommand::class, function ($app) { return new TraitMakeCommand($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)); } } framework/src/Illuminate/Foundation/Providers/ConsoleSupportServiceProvider.php 0000644 00000001034 15060132305 0024253 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, ]; } framework/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php 0000644 00000020215 15060132305 0023364 0 ustar 00 <?php namespace Illuminate\Foundation\Providers; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Contracts\Console\Kernel as ConsoleKernel; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\Application; 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\Exceptions\Renderer\Listener; use Illuminate\Foundation\Exceptions\Renderer\Mappers\BladeMapper; use Illuminate\Foundation\Exceptions\Renderer\Renderer; 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\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; 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'); } if ($this->app->hasDebugModeEnabled()) { $this->app->make(Listener::class)->registerListeners( $this->app->make(Dispatcher::class) ); } } /** * Register the service provider. * * @return void */ public function register() { parent::register(); $this->registerConsoleSchedule(); $this->registerDumper(); $this->registerRequestValidation(); $this->registerRequestSignatureValidation(); $this->registerExceptionTracking(); $this->registerExceptionRenderer(); $this->registerMaintenanceModeManager(); } /** * Register the console schedule implementation. * * @return void */ public function registerConsoleSchedule() { $this->app->singleton(Schedule::class, function ($app) { return $app->make(ConsoleKernel::class)->resolveConsoleSchedule(); }); } /** * 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); }); Request::macro('hasValidRelativeSignatureWhileIgnoring', function ($ignoreQuery = []) { return URL::hasValidSignature($this, $absolute = false, $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 exceptions renderer. * * @return void */ protected function registerExceptionRenderer() { if (! $this->app->hasDebugModeEnabled()) { return; } $this->loadViewsFrom(__DIR__.'/../resources/exceptions/renderer', 'laravel-exceptions-renderer'); $this->app->singleton(Renderer::class, function (Application $app) { $errorRenderer = new HtmlErrorRenderer( $app['config']->get('app.debug'), ); return new Renderer( $app->make(Factory::class), $app->make(Listener::class), $errorRenderer, $app->make(BladeMapper::class), $app->basePath(), ); }); $this->app->singleton(Listener::class); } /** * 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() ); } } framework/src/Illuminate/Foundation/Providers/ComposerServiceProvider.php 0000755 00000001267 15060132305 0023056 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']; } } framework/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php 0000644 00000001666 15060132305 0023543 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)); }); } } framework/src/Illuminate/Foundation/helpers.php 0000644 00000063527 15060132305 0015724 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\Log\Context\Repository as ContextRepository; 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 * @throws \Illuminate\Http\Exceptions\HttpResponseException */ 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('context')) { /** * Get / set the specified context value. * * @param array|string|null $key * @param mixed $default * @return mixed|\Illuminate\Log\Context\Repository */ function context($key = null, $default = null) { return match (true) { is_null($key) => app(ContextRepository::class), is_array($key) => app(ContextRepository::class)->add($key), default => app(ContextRepository::class)->get($key, $default), }; } } 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|float|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); } } framework/src/Illuminate/Foundation/resources/health-up.blade.php 0000644 00000004303 15060132305 0021214 0 ustar 00 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ config('app.name', 'Laravel') }}</title> <!-- Fonts --> <link rel="preconnect" href="https://fonts.bunny.net"> <link href="https://fonts.bunny.net/css?family=figtree:400,600&display=swap" rel="stylesheet" /> <!-- Styles --> <script src="https://cdn.tailwindcss.com"></script> <script> tailwind.config = { theme: { extend: { fontFamily: { sans: ['Figtree', 'ui-sans-serif', 'system-ui', 'sans-serif', "Apple Color Emoji", "Segoe UI Emoji"], } } } } </script> </head> <body class="antialiased"> <div class="relative sm:flex sm:justify-center sm:items-center min-h-screen bg-gray-100 selection:bg-red-500 selection:text-white"> <div class="w-full sm:w-3/4 xl:w-1/2 mx-auto p-6"> <div class="px-6 py-4 bg-white from-gray-700/50 via-transparent rounded-lg shadow-2xl shadow-gray-500/20 flex items-center focus:outline focus:outline-2 focus:outline-red-500"> <div class="relative flex h-3 w-3"> <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span> <span class="relative inline-flex rounded-full h-3 w-3 bg-green-400"></span> </div> <div class="ml-6"> <h2 class="text-xl font-semibold text-gray-900">Application up</h2> <p class="mt-2 text-gray-500 dark:text-gray-400 text-sm leading-relaxed"> HTTP request received. @if (defined('LARAVEL_START')) Response successfully rendered in {{ round((microtime(true) - LARAVEL_START) * 1000) }}ms. @endif </p> </div> </div> </div> </div> </body> </html> framework/src/Illuminate/Foundation/resources/exceptions/renderer/light-mode.css 0000644 00000000056 15060132305 0024301 0 ustar 00 @import 'highlight.js/styles/github.min.css'; framework/src/Illuminate/Foundation/resources/exceptions/renderer/postcss.config.js 0000644 00000000137 15060132305 0025036 0 ustar 00 module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, }; framework/src/Illuminate/Foundation/resources/exceptions/renderer/dark-mode.css 0000644 00000000065 15060132305 0024113 0 ustar 00 @import 'highlight.js/styles/atom-one-dark.min.css'; framework/src/Illuminate/Foundation/resources/exceptions/renderer/vite.config.js 0000644 00000000566 15060132305 0024315 0 ustar 00 /** @type {import('vite').UserConfig} */ export default { plugins: [], build: { assetsDir: '', rollupOptions: { input: ['scripts.js', 'styles.css', 'dark-mode.css', 'light-mode.css'], output: { assetFileNames: '[name][extname]', entryFileNames: '[name].js', }, }, }, }; framework/src/Illuminate/Foundation/resources/exceptions/renderer/scripts.js 0000644 00000001123 15060132305 0023557 0 ustar 00 import tippy from 'tippy.js'; import alpine from 'alpinejs'; import hljs from 'highlight.js/lib/core'; import php from 'highlight.js/lib/languages/php'; alpine.start(); hljs.registerLanguage('php', php); window.hljs = hljs; hljs.highlightElement(document.querySelector('.default-highlightable-code')); document.querySelectorAll('.highlightable-code').forEach((block) => { if (block.dataset.highlighted !== 'yes') { hljs.highlightElement(block); } }); tippy('[data-tippy-content]', { trigger: 'click', arrow: true, theme: 'material', animation: 'scale', }); framework/src/Illuminate/Foundation/resources/exceptions/renderer/package-lock.json 0000644 00000270342 15060132305 0024761 0 ustar 00 { "name": "renderer", "lockfileVersion": 3, "requires": true, "packages": { "": { "dependencies": { "alpinejs": "^3.13.10", "autoprefixer": "^10.4.19", "highlight.js": "^11.9.0", "postcss": "^8.4.38", "tailwindcss": "^3.4.3", "tippy.js": "^6.3.7", "vite": "^5.2.10", "vite-require": "^0.2.3" } }, "node_modules/@alloc/quick-lru": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/@alloc/quick-lru/-/quick-lru-5.2.0.tgz", "integrity": "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==", "engines": { "node": ">=10" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/@esbuild/aix-ppc64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.20.2.tgz", "integrity": "sha512-D+EBOJHXdNZcLJRBkhENNG8Wji2kgc9AZ9KiPr1JuZjsNtyHzrsfLRrY0tk2H2aoFu6RANO1y1iPPUCDYWkb5g==", "cpu": [ "ppc64" ], "optional": true, "os": [ "aix" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/android-arm": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.20.2.tgz", "integrity": "sha512-t98Ra6pw2VaDhqNWO2Oph2LXbz/EJcnLmKLGBJwEwXX/JAN83Fym1rU8l0JUWK6HkIbWONCSSatf4sf2NBRx/w==", "cpu": [ "arm" ], "optional": true, "os": [ "android" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/android-arm64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.20.2.tgz", "integrity": "sha512-mRzjLacRtl/tWU0SvD8lUEwb61yP9cqQo6noDZP/O8VkwafSYwZ4yWy24kan8jE/IMERpYncRt2dw438LP3Xmg==", "cpu": [ "arm64" ], "optional": true, "os": [ "android" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/android-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.20.2.tgz", "integrity": "sha512-btzExgV+/lMGDDa194CcUQm53ncxzeBrWJcncOBxuC6ndBkKxnHdFJn86mCIgTELsooUmwUm9FkhSp5HYu00Rg==", "cpu": [ "x64" ], "optional": true, "os": [ "android" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/darwin-arm64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.20.2.tgz", "integrity": "sha512-4J6IRT+10J3aJH3l1yzEg9y3wkTDgDk7TSDFX+wKFiWjqWp/iCfLIYzGyasx9l0SAFPT1HwSCR+0w/h1ES/MjA==", "cpu": [ "arm64" ], "optional": true, "os": [ "darwin" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/darwin-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.20.2.tgz", "integrity": "sha512-tBcXp9KNphnNH0dfhv8KYkZhjc+H3XBkF5DKtswJblV7KlT9EI2+jeA8DgBjp908WEuYll6pF+UStUCfEpdysA==", "cpu": [ "x64" ], "optional": true, "os": [ "darwin" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/freebsd-arm64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.20.2.tgz", "integrity": "sha512-d3qI41G4SuLiCGCFGUrKsSeTXyWG6yem1KcGZVS+3FYlYhtNoNgYrWcvkOoaqMhwXSMrZRl69ArHsGJ9mYdbbw==", "cpu": [ "arm64" ], "optional": true, "os": [ "freebsd" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/freebsd-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.20.2.tgz", "integrity": "sha512-d+DipyvHRuqEeM5zDivKV1KuXn9WeRX6vqSqIDgwIfPQtwMP4jaDsQsDncjTDDsExT4lR/91OLjRo8bmC1e+Cw==", "cpu": [ "x64" ], "optional": true, "os": [ "freebsd" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-arm": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.20.2.tgz", "integrity": "sha512-VhLPeR8HTMPccbuWWcEUD1Az68TqaTYyj6nfE4QByZIQEQVWBB8vup8PpR7y1QHL3CpcF6xd5WVBU/+SBEvGTg==", "cpu": [ "arm" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-arm64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.20.2.tgz", "integrity": "sha512-9pb6rBjGvTFNira2FLIWqDk/uaf42sSyLE8j1rnUpuzsODBq7FvpwHYZxQ/It/8b+QOS1RYfqgGFNLRI+qlq2A==", "cpu": [ "arm64" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-ia32": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.20.2.tgz", "integrity": "sha512-o10utieEkNPFDZFQm9CoP7Tvb33UutoJqg3qKf1PWVeeJhJw0Q347PxMvBgVVFgouYLGIhFYG0UGdBumROyiig==", "cpu": [ "ia32" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-loong64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.20.2.tgz", "integrity": "sha512-PR7sp6R/UC4CFVomVINKJ80pMFlfDfMQMYynX7t1tNTeivQ6XdX5r2XovMmha/VjR1YN/HgHWsVcTRIMkymrgQ==", "cpu": [ "loong64" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-mips64el": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.20.2.tgz", "integrity": "sha512-4BlTqeutE/KnOiTG5Y6Sb/Hw6hsBOZapOVF6njAESHInhlQAghVVZL1ZpIctBOoTFbQyGW+LsVYZ8lSSB3wkjA==", "cpu": [ "mips64el" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-ppc64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.20.2.tgz", "integrity": "sha512-rD3KsaDprDcfajSKdn25ooz5J5/fWBylaaXkuotBDGnMnDP1Uv5DLAN/45qfnf3JDYyJv/ytGHQaziHUdyzaAg==", "cpu": [ "ppc64" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-riscv64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.20.2.tgz", "integrity": "sha512-snwmBKacKmwTMmhLlz/3aH1Q9T8v45bKYGE3j26TsaOVtjIag4wLfWSiZykXzXuE1kbCE+zJRmwp+ZbIHinnVg==", "cpu": [ "riscv64" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-s390x": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.20.2.tgz", "integrity": "sha512-wcWISOobRWNm3cezm5HOZcYz1sKoHLd8VL1dl309DiixxVFoFe/o8HnwuIwn6sXre88Nwj+VwZUvJf4AFxkyrQ==", "cpu": [ "s390x" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/linux-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.20.2.tgz", "integrity": "sha512-1MdwI6OOTsfQfek8sLwgyjOXAu+wKhLEoaOLTjbijk6E2WONYpH9ZU2mNtR+lZ2B4uwr+usqGuVfFT9tMtGvGw==", "cpu": [ "x64" ], "optional": true, "os": [ "linux" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/netbsd-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.20.2.tgz", "integrity": "sha512-K8/DhBxcVQkzYc43yJXDSyjlFeHQJBiowJ0uVL6Tor3jGQfSGHNNJcWxNbOI8v5k82prYqzPuwkzHt3J1T1iZQ==", "cpu": [ "x64" ], "optional": true, "os": [ "netbsd" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/openbsd-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.20.2.tgz", "integrity": "sha512-eMpKlV0SThJmmJgiVyN9jTPJ2VBPquf6Kt/nAoo6DgHAoN57K15ZghiHaMvqjCye/uU4X5u3YSMgVBI1h3vKrQ==", "cpu": [ "x64" ], "optional": true, "os": [ "openbsd" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/sunos-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.20.2.tgz", "integrity": "sha512-2UyFtRC6cXLyejf/YEld4Hajo7UHILetzE1vsRcGL3earZEW77JxrFjH4Ez2qaTiEfMgAXxfAZCm1fvM/G/o8w==", "cpu": [ "x64" ], "optional": true, "os": [ "sunos" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/win32-arm64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.20.2.tgz", "integrity": "sha512-GRibxoawM9ZCnDxnP3usoUDO9vUkpAxIIZ6GQI+IlVmr5kP3zUq+l17xELTHMWTWzjxa2guPNyrpq1GWmPvcGQ==", "cpu": [ "arm64" ], "optional": true, "os": [ "win32" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/win32-ia32": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.20.2.tgz", "integrity": "sha512-HfLOfn9YWmkSKRQqovpnITazdtquEW8/SoHW7pWpuEeguaZI4QnCRW6b+oZTztdBnZOS2hqJ6im/D5cPzBTTlQ==", "cpu": [ "ia32" ], "optional": true, "os": [ "win32" ], "engines": { "node": ">=12" } }, "node_modules/@esbuild/win32-x64": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.20.2.tgz", "integrity": "sha512-N49X4lJX27+l9jbLKSqZ6bKNjzQvHaT8IIFUy+YIqmXQdjYCToGWwOItDrfby14c78aDd5NHQl29xingXfCdLQ==", "cpu": [ "x64" ], "optional": true, "os": [ "win32" ], "engines": { "node": ">=12" } }, "node_modules/@isaacs/cliui": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz", "integrity": "sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==", "dependencies": { "string-width": "^5.1.2", "string-width-cjs": "npm:string-width@^4.2.0", "strip-ansi": "^7.0.1", "strip-ansi-cjs": "npm:strip-ansi@^6.0.1", "wrap-ansi": "^8.1.0", "wrap-ansi-cjs": "npm:wrap-ansi@^7.0.0" }, "engines": { "node": ">=12" } }, "node_modules/@jridgewell/gen-mapping": { "version": "0.3.5", "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.5.tgz", "integrity": "sha512-IzL8ZoEDIBRWEzlCcRhOaCupYyN5gdIK+Q6fbFdPDg6HqX6jpkItn7DFIpW9LQzXG6Df9sA7+OKnq0qlz/GaQg==", "dependencies": { "@jridgewell/set-array": "^1.2.1", "@jridgewell/sourcemap-codec": "^1.4.10", "@jridgewell/trace-mapping": "^0.3.24" }, "engines": { "node": ">=6.0.0" } }, "node_modules/@jridgewell/resolve-uri": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", "engines": { "node": ">=6.0.0" } }, "node_modules/@jridgewell/set-array": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.2.1.tgz", "integrity": "sha512-R8gLRTZeyp03ymzP/6Lil/28tGeGEzhx1q2k703KGWRAI1VdvPIXdG70VJc2pAMw3NA6JKL5hhFu1sJX0Mnn/A==", "engines": { "node": ">=6.0.0" } }, "node_modules/@jridgewell/source-map": { "version": "0.3.6", "resolved": "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.6.tgz", "integrity": "sha512-1ZJTZebgqllO79ue2bm3rIGud/bOe0pP5BjSRCRxxYkEZS8STV7zN84UBbiYu7jy+eCKSnVIUgoWWE/tt+shMQ==", "optional": true, "peer": true, "dependencies": { "@jridgewell/gen-mapping": "^0.3.5", "@jridgewell/trace-mapping": "^0.3.25" } }, "node_modules/@jridgewell/sourcemap-codec": { "version": "1.4.15", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==" }, "node_modules/@jridgewell/trace-mapping": { "version": "0.3.25", "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.25.tgz", "integrity": "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==", "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", "dependencies": { "@nodelib/fs.stat": "2.0.5", "run-parallel": "^1.1.9" }, "engines": { "node": ">= 8" } }, "node_modules/@nodelib/fs.stat": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz", "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==", "engines": { "node": ">= 8" } }, "node_modules/@nodelib/fs.walk": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz", "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==", "dependencies": { "@nodelib/fs.scandir": "2.1.5", "fastq": "^1.6.0" }, "engines": { "node": ">= 8" } }, "node_modules/@pkgjs/parseargs": { "version": "0.11.0", "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz", "integrity": "sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==", "optional": true, "engines": { "node": ">=14" } }, "node_modules/@popperjs/core": { "version": "2.11.8", "resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.8.tgz", "integrity": "sha512-P1st0aksCrn9sGZhp8GMYwBnQsbvAWsZAX44oXNNvLHGqAOcoVxmjZiohstwQ7SqKnbR47akdNi+uleWD8+g6A==", "funding": { "type": "opencollective", "url": "https://opencollective.com/popperjs" } }, "node_modules/@rollup/rollup-android-arm-eabi": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.17.1.tgz", "integrity": "sha512-P6Wg856Ou/DLpR+O0ZLneNmrv7QpqBg+hK4wE05ijbC/t349BRfMfx+UFj5Ha3fCFopIa6iSZlpdaB4agkWp2Q==", "cpu": [ "arm" ], "optional": true, "os": [ "android" ] }, "node_modules/@rollup/rollup-android-arm64": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.17.1.tgz", "integrity": "sha512-piwZDjuW2WiHr05djVdUkrG5JbjnGbtx8BXQchYCMfib/nhjzWoiScelZ+s5IJI7lecrwSxHCzW026MWBL+oJQ==", "cpu": [ "arm64" ], "optional": true, "os": [ "android" ] }, "node_modules/@rollup/rollup-darwin-arm64": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.17.1.tgz", "integrity": "sha512-LsZXXIsN5Q460cKDT4Y+bzoPDhBmO5DTr7wP80d+2EnYlxSgkwdPfE3hbE+Fk8dtya+8092N9srjBTJ0di8RIA==", "cpu": [ "arm64" ], "optional": true, "os": [ "darwin" ] }, "node_modules/@rollup/rollup-darwin-x64": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.17.1.tgz", "integrity": "sha512-S7TYNQpWXB9APkxu/SLmYHezWwCoZRA9QLgrDeml+SR2A1LLPD2DBUdUlvmCF7FUpRMKvbeeWky+iizQj65Etw==", "cpu": [ "x64" ], "optional": true, "os": [ "darwin" ] }, "node_modules/@rollup/rollup-linux-arm-gnueabihf": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.17.1.tgz", "integrity": "sha512-Lq2JR5a5jsA5um2ZoLiXXEaOagnVyCpCW7xvlcqHC7y46tLwTEgUSTM3a2TfmmTMmdqv+jknUioWXlmxYxE9Yw==", "cpu": [ "arm" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-arm-musleabihf": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.17.1.tgz", "integrity": "sha512-9BfzwyPNV0IizQoR+5HTNBGkh1KXE8BqU0DBkqMngmyFW7BfuIZyMjQ0s6igJEiPSBvT3ZcnIFohZ19OqjhDPg==", "cpu": [ "arm" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-arm64-gnu": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.17.1.tgz", "integrity": "sha512-e2uWaoxo/rtzA52OifrTSXTvJhAXb0XeRkz4CdHBK2KtxrFmuU/uNd544Ogkpu938BzEfvmWs8NZ8Axhw33FDw==", "cpu": [ "arm64" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-arm64-musl": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.17.1.tgz", "integrity": "sha512-ekggix/Bc/d/60H1Mi4YeYb/7dbal1kEDZ6sIFVAE8pUSx7PiWeEh+NWbL7bGu0X68BBIkgF3ibRJe1oFTksQQ==", "cpu": [ "arm64" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-powerpc64le-gnu": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-powerpc64le-gnu/-/rollup-linux-powerpc64le-gnu-4.17.1.tgz", "integrity": "sha512-UGV0dUo/xCv4pkr/C8KY7XLFwBNnvladt8q+VmdKrw/3RUd3rD0TptwjisvE2TTnnlENtuY4/PZuoOYRiGp8Gw==", "cpu": [ "ppc64" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-riscv64-gnu": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.17.1.tgz", "integrity": "sha512-gEYmYYHaehdvX46mwXrU49vD6Euf1Bxhq9pPb82cbUU9UT2NV+RSckQ5tKWOnNXZixKsy8/cPGtiUWqzPuAcXQ==", "cpu": [ "riscv64" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-s390x-gnu": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.17.1.tgz", "integrity": "sha512-xeae5pMAxHFp6yX5vajInG2toST5lsCTrckSRUFwNgzYqnUjNBcQyqk1bXUxX5yhjWFl2Mnz3F8vQjl+2FRIcw==", "cpu": [ "s390x" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-x64-gnu": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.17.1.tgz", "integrity": "sha512-AsdnINQoDWfKpBzCPqQWxSPdAWzSgnYbrJYtn6W0H2E9It5bZss99PiLA8CgmDRfvKygt20UpZ3xkhFlIfX9zQ==", "cpu": [ "x64" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-linux-x64-musl": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.17.1.tgz", "integrity": "sha512-KoB4fyKXTR+wYENkIG3fFF+5G6N4GFvzYx8Jax8BR4vmddtuqSb5oQmYu2Uu067vT/Fod7gxeQYKupm8gAcMSQ==", "cpu": [ "x64" ], "optional": true, "os": [ "linux" ] }, "node_modules/@rollup/rollup-win32-arm64-msvc": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.17.1.tgz", "integrity": "sha512-J0d3NVNf7wBL9t4blCNat+d0PYqAx8wOoY+/9Q5cujnafbX7BmtYk3XvzkqLmFECaWvXGLuHmKj/wrILUinmQg==", "cpu": [ "arm64" ], "optional": true, "os": [ "win32" ] }, "node_modules/@rollup/rollup-win32-ia32-msvc": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.17.1.tgz", "integrity": "sha512-xjgkWUwlq7IbgJSIxvl516FJ2iuC/7ttjsAxSPpC9kkI5iQQFHKyEN5BjbhvJ/IXIZ3yIBcW5QDlWAyrA+TFag==", "cpu": [ "ia32" ], "optional": true, "os": [ "win32" ] }, "node_modules/@rollup/rollup-win32-x64-msvc": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.17.1.tgz", "integrity": "sha512-0QbCkfk6cnnVKWqqlC0cUrrUMDMfu5ffvYMTUHf+qMN2uAb3MKP31LPcwiMXBNsvoFGs/kYdFOsuLmvppCopXA==", "cpu": [ "x64" ], "optional": true, "os": [ "win32" ] }, "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==" }, "node_modules/@types/node": { "version": "20.12.7", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", "integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", "optional": true, "peer": true, "dependencies": { "undici-types": "~5.26.4" } }, "node_modules/@vue/reactivity": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/@vue/reactivity/-/reactivity-3.1.5.tgz", "integrity": "sha512-1tdfLmNjWG6t/CsPldh+foumYFo3cpyCHgBYQ34ylaMsJ+SNHQ1kApMIa8jN+i593zQuaw3AdWH0nJTARzCFhg==", "dependencies": { "@vue/shared": "3.1.5" } }, "node_modules/@vue/shared": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/@vue/shared/-/shared-3.1.5.tgz", "integrity": "sha512-oJ4F3TnvpXaQwZJNF3ZK+kLPHKarDmJjJ6jyzVNDKH9md1dptjC7lWR//jrGuLdek/U6iltWxqAnYOu8gCiOvA==" }, "node_modules/acorn": { "version": "8.11.3", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", "bin": { "acorn": "bin/acorn" }, "engines": { "node": ">=0.4.0" } }, "node_modules/alpinejs": { "version": "3.13.10", "resolved": "https://registry.npmjs.org/alpinejs/-/alpinejs-3.13.10.tgz", "integrity": "sha512-86RB307VWICex0vG15Eq0x058cNNsvS57ohrjN6n/TJAVSFV+zXOK/E34nNHDHc6Poq+yTNCLqEzPqEkRBTMRQ==", "dependencies": { "@vue/reactivity": "~3.1.1" } }, "node_modules/ansi-regex": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.0.1.tgz", "integrity": "sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==", "engines": { "node": ">=12" }, "funding": { "url": "https://github.com/chalk/ansi-regex?sponsor=1" } }, "node_modules/ansi-styles": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.1.tgz", "integrity": "sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==", "engines": { "node": ">=12" }, "funding": { "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, "node_modules/any-promise": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "integrity": "sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==" }, "node_modules/anymatch": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", "dependencies": { "normalize-path": "^3.0.0", "picomatch": "^2.0.4" }, "engines": { "node": ">= 8" } }, "node_modules/arg": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.2.tgz", "integrity": "sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==" }, "node_modules/autoprefixer": { "version": "10.4.19", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.19.tgz", "integrity": "sha512-BaENR2+zBZ8xXhM4pUaKUxlVdxZ0EZhjvbopwnXmxRUfqDmwSpC2lAi/QXvx7NRdPCo1WKEcEF6mV64si1z4Ew==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/postcss/" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/autoprefixer" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "dependencies": { "browserslist": "^4.23.0", "caniuse-lite": "^1.0.30001599", "fraction.js": "^4.3.7", "normalize-range": "^0.1.2", "picocolors": "^1.0.0", "postcss-value-parser": "^4.2.0" }, "bin": { "autoprefixer": "bin/autoprefixer" }, "engines": { "node": "^10 || ^12 || >=14" }, "peerDependencies": { "postcss": "^8.1.0" } }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, "node_modules/binary-extensions": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", "engines": { "node": ">=8" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/brace-expansion": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", "dependencies": { "balanced-match": "^1.0.0" } }, "node_modules/braces": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", "dependencies": { "fill-range": "^7.0.1" }, "engines": { "node": ">=8" } }, "node_modules/browserslist": { "version": "4.23.0", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.23.0.tgz", "integrity": "sha512-QW8HiM1shhT2GuzkvklfjcKDiWFXHOeFCIA/huJPwHsslwcydgk7X+z2zXpEijP98UCY7HbubZt5J2Zgvf0CaQ==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/browserslist" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/browserslist" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "dependencies": { "caniuse-lite": "^1.0.30001587", "electron-to-chromium": "^1.4.668", "node-releases": "^2.0.14", "update-browserslist-db": "^1.0.13" }, "bin": { "browserslist": "cli.js" }, "engines": { "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "optional": true, "peer": true }, "node_modules/camelcase-css": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/camelcase-css/-/camelcase-css-2.0.1.tgz", "integrity": "sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA==", "engines": { "node": ">= 6" } }, "node_modules/caniuse-lite": { "version": "1.0.30001614", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001614.tgz", "integrity": "sha512-jmZQ1VpmlRwHgdP1/uiKzgiAuGOfLEJsYFP4+GBou/QQ4U6IOJCB4NP1c+1p9RGLpwObcT94jA5/uO+F1vBbog==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/browserslist" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/caniuse-lite" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ] }, "node_modules/chokidar": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", "dependencies": { "anymatch": "~3.1.2", "braces": "~3.0.2", "glob-parent": "~5.1.2", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", "normalize-path": "~3.0.0", "readdirp": "~3.6.0" }, "engines": { "node": ">= 8.10.0" }, "funding": { "url": "https://paulmillr.com/funding/" }, "optionalDependencies": { "fsevents": "~2.3.2" } }, "node_modules/chokidar/node_modules/glob-parent": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", "dependencies": { "is-glob": "^4.0.1" }, "engines": { "node": ">= 6" } }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", "dependencies": { "color-name": "~1.1.4" }, "engines": { "node": ">=7.0.0" } }, "node_modules/color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, "node_modules/commander": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", "engines": { "node": ">= 6" } }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" }, "engines": { "node": ">= 8" } }, "node_modules/cssesc": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/cssesc/-/cssesc-3.0.0.tgz", "integrity": "sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg==", "bin": { "cssesc": "bin/cssesc" }, "engines": { "node": ">=4" } }, "node_modules/didyoumean": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/didyoumean/-/didyoumean-1.2.2.tgz", "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==" }, "node_modules/dlv": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/dlv/-/dlv-1.1.3.tgz", "integrity": "sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==" }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==" }, "node_modules/electron-to-chromium": { "version": "1.4.750", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.750.tgz", "integrity": "sha512-9ItEpeu15hW5m8jKdriL+BQrgwDTXEL9pn4SkillWFu73ZNNNQ2BKKLS+ZHv2vC9UkNhosAeyfxOf/5OSeTCPA==" }, "node_modules/emoji-regex": { "version": "9.2.2", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-9.2.2.tgz", "integrity": "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==" }, "node_modules/es-module-lexer": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.5.2.tgz", "integrity": "sha512-l60ETUTmLqbVbVHv1J4/qj+M8nq7AwMzEcg3kmJDt9dCNrTk+yHcYFf/Kw75pMDwd9mPcIGCG5LcS20SxYRzFA==" }, "node_modules/esbuild": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.20.2.tgz", "integrity": "sha512-WdOOppmUNU+IbZ0PaDiTst80zjnrOkyJNHoKupIcVyU8Lvla3Ugx94VzkQ32Ijqd7UhHJy75gNWDMUekcrSJ6g==", "hasInstallScript": true, "bin": { "esbuild": "bin/esbuild" }, "engines": { "node": ">=12" }, "optionalDependencies": { "@esbuild/aix-ppc64": "0.20.2", "@esbuild/android-arm": "0.20.2", "@esbuild/android-arm64": "0.20.2", "@esbuild/android-x64": "0.20.2", "@esbuild/darwin-arm64": "0.20.2", "@esbuild/darwin-x64": "0.20.2", "@esbuild/freebsd-arm64": "0.20.2", "@esbuild/freebsd-x64": "0.20.2", "@esbuild/linux-arm": "0.20.2", "@esbuild/linux-arm64": "0.20.2", "@esbuild/linux-ia32": "0.20.2", "@esbuild/linux-loong64": "0.20.2", "@esbuild/linux-mips64el": "0.20.2", "@esbuild/linux-ppc64": "0.20.2", "@esbuild/linux-riscv64": "0.20.2", "@esbuild/linux-s390x": "0.20.2", "@esbuild/linux-x64": "0.20.2", "@esbuild/netbsd-x64": "0.20.2", "@esbuild/openbsd-x64": "0.20.2", "@esbuild/sunos-x64": "0.20.2", "@esbuild/win32-arm64": "0.20.2", "@esbuild/win32-ia32": "0.20.2", "@esbuild/win32-x64": "0.20.2" } }, "node_modules/escalade": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.2.tgz", "integrity": "sha512-ErCHMCae19vR8vQGe50xIsVomy19rg6gFu3+r3jkEO46suLMWBksvVyoGgQV+jOfl84ZSOSlmv6Gxa89PmTGmA==", "engines": { "node": ">=6" } }, "node_modules/fast-glob": { "version": "3.3.2", "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.2.tgz", "integrity": "sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==", "dependencies": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", "glob-parent": "^5.1.2", "merge2": "^1.3.0", "micromatch": "^4.0.4" }, "engines": { "node": ">=8.6.0" } }, "node_modules/fast-glob/node_modules/glob-parent": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", "dependencies": { "is-glob": "^4.0.1" }, "engines": { "node": ">= 6" } }, "node_modules/fastq": { "version": "1.17.1", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.1.tgz", "integrity": "sha512-sRVD3lWVIXWg6By68ZN7vho9a1pQcN/WBFaAAsDDFzlJjvoGx0P8z7V1t72grFJfJhu3YPZBuu25f7Kaw2jN1w==", "dependencies": { "reusify": "^1.0.4" } }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", "dependencies": { "to-regex-range": "^5.0.1" }, "engines": { "node": ">=8" } }, "node_modules/foreground-child": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.1.1.tgz", "integrity": "sha512-TMKDUnIte6bfb5nWv7V/caI169OHgvwjb7V4WkeUvbQQdjr5rWKqHFiKWb/fcOwB+CzBT+qbWjvj+DVwRskpIg==", "dependencies": { "cross-spawn": "^7.0.0", "signal-exit": "^4.0.1" }, "engines": { "node": ">=14" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, "node_modules/fraction.js": { "version": "4.3.7", "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.3.7.tgz", "integrity": "sha512-ZsDfxO51wGAXREY55a7la9LScWpwv9RxIrYABrlvOFBlH/ShPnrtsXeuUIfXKKOVicNxQ+o8JTbJvjS4M89yew==", "engines": { "node": "*" }, "funding": { "type": "patreon", "url": "https://github.com/sponsors/rawify" } }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", "hasInstallScript": true, "optional": true, "os": [ "darwin" ], "engines": { "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", "funding": { "url": "https://github.com/sponsors/ljharb" } }, "node_modules/glob": { "version": "10.3.12", "resolved": "https://registry.npmjs.org/glob/-/glob-10.3.12.tgz", "integrity": "sha512-TCNv8vJ+xz4QiqTpfOJA7HvYv+tNIRHKfUWw/q+v2jdgN4ebz+KY9tGx5J4rHP0o84mNP+ApH66HRX8us3Khqg==", "dependencies": { "foreground-child": "^3.1.0", "jackspeak": "^2.3.6", "minimatch": "^9.0.1", "minipass": "^7.0.4", "path-scurry": "^1.10.2" }, "bin": { "glob": "dist/esm/bin.mjs" }, "engines": { "node": ">=16 || 14 >=14.17" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, "node_modules/glob-parent": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", "integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==", "dependencies": { "is-glob": "^4.0.3" }, "engines": { "node": ">=10.13.0" } }, "node_modules/hasown": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", "dependencies": { "function-bind": "^1.1.2" }, "engines": { "node": ">= 0.4" } }, "node_modules/highlight.js": { "version": "11.9.0", "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-11.9.0.tgz", "integrity": "sha512-fJ7cW7fQGCYAkgv4CPfwFHrfd/cLS4Hau96JuJ+ZTOWhjnhoeN1ub1tFmALm/+lW5z4WCAuAV9bm05AP0mS6Gw==", "engines": { "node": ">=12.0.0" } }, "node_modules/is-binary-path": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", "dependencies": { "binary-extensions": "^2.0.0" }, "engines": { "node": ">=8" } }, "node_modules/is-core-module": { "version": "2.13.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz", "integrity": "sha512-hHrIjvZsftOsvKSn2TRYl63zvxsgE0K+0mYMoH6gD4omR5IWB2KynivBQczo3+wF1cCkjzvptnI9Q0sPU66ilw==", "dependencies": { "hasown": "^2.0.0" }, "funding": { "url": "https://github.com/sponsors/ljharb" } }, "node_modules/is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", "engines": { "node": ">=0.10.0" } }, "node_modules/is-fullwidth-code-point": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", "engines": { "node": ">=8" } }, "node_modules/is-glob": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", "dependencies": { "is-extglob": "^2.1.1" }, "engines": { "node": ">=0.10.0" } }, "node_modules/is-number": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "engines": { "node": ">=0.12.0" } }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==" }, "node_modules/jackspeak": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/jackspeak/-/jackspeak-2.3.6.tgz", "integrity": "sha512-N3yCS/NegsOBokc8GAdM8UcmfsKiSS8cipheD/nivzr700H+nsMOxJjQnvwOcRYVuFkdH0wGUvW2WbXGmrZGbQ==", "dependencies": { "@isaacs/cliui": "^8.0.2" }, "engines": { "node": ">=14" }, "funding": { "url": "https://github.com/sponsors/isaacs" }, "optionalDependencies": { "@pkgjs/parseargs": "^0.11.0" } }, "node_modules/jiti": { "version": "1.21.0", "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.21.0.tgz", "integrity": "sha512-gFqAIbuKyyso/3G2qhiO2OM6shY6EPP/R0+mkDbyspxKazh8BXDC5FiFsUjlczgdNz/vfra0da2y+aHrusLG/Q==", "bin": { "jiti": "bin/jiti.js" } }, "node_modules/lilconfig": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.1.0.tgz", "integrity": "sha512-utWOt/GHzuUxnLKxB6dk81RoOeoNeHgbrXiuGk4yyF5qlRz+iIVWu56E2fqGHFrXz0QNUhLB/8nKqvRH66JKGQ==", "engines": { "node": ">=10" } }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", "integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==" }, "node_modules/lru-cache": { "version": "10.2.2", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.2.tgz", "integrity": "sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==", "engines": { "node": "14 || >=16.14" } }, "node_modules/magic-string": { "version": "0.30.10", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.10.tgz", "integrity": "sha512-iIRwTIf0QKV3UAnYK4PU8uiEc4SRh5jX0mwpIwETPpHdhVM4f53RSwS/vXvN1JhGX+Cs7B8qIq3d6AH49O5fAQ==", "dependencies": { "@jridgewell/sourcemap-codec": "^1.4.15" } }, "node_modules/merge2": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", "engines": { "node": ">= 8" } }, "node_modules/micromatch": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", "dependencies": { "braces": "^3.0.2", "picomatch": "^2.3.1" }, "engines": { "node": ">=8.6" } }, "node_modules/minimatch": { "version": "9.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.4.tgz", "integrity": "sha512-KqWh+VchfxcMNRAJjj2tnsSJdNbHsVgnkBhTNrW7AjVo6OvLtxw8zfT9oLw1JSohlFzJ8jCoTgaoXvJ+kHt6fw==", "dependencies": { "brace-expansion": "^2.0.1" }, "engines": { "node": ">=16 || 14 >=14.17" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, "node_modules/minipass": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.0.4.tgz", "integrity": "sha512-jYofLM5Dam9279rdkWzqHozUo4ybjdZmCsDHePy5V/PbBcVMiSZR97gmAy45aqi8CK1lG2ECd356FU86avfwUQ==", "engines": { "node": ">=16 || 14 >=14.17" } }, "node_modules/mz": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz", "integrity": "sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==", "dependencies": { "any-promise": "^1.0.0", "object-assign": "^4.0.1", "thenify-all": "^1.0.0" } }, "node_modules/nanoid": { "version": "3.3.7", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz", "integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==", "funding": [ { "type": "github", "url": "https://github.com/sponsors/ai" } ], "bin": { "nanoid": "bin/nanoid.cjs" }, "engines": { "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, "node_modules/node-releases": { "version": "2.0.14", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.14.tgz", "integrity": "sha512-y10wOWt8yZpqXmOgRo77WaHEmhYQYGNA6y421PKsKYWEK8aW+cqAphborZDhqfyKrbZEN92CN1X2KbafY2s7Yw==" }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", "engines": { "node": ">=0.10.0" } }, "node_modules/normalize-range": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/normalize-range/-/normalize-range-0.1.2.tgz", "integrity": "sha512-bdok/XvKII3nUpklnV6P2hxtMNrCboOjAcyBuQnWEhO665FwrSNRxU+AqpsyvO6LgGYPspN+lu5CLtw4jPRKNA==", "engines": { "node": ">=0.10.0" } }, "node_modules/object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", "engines": { "node": ">=0.10.0" } }, "node_modules/object-hash": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", "engines": { "node": ">= 6" } }, "node_modules/path-key": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", "engines": { "node": ">=8" } }, "node_modules/path-parse": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" }, "node_modules/path-scurry": { "version": "1.10.2", "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-1.10.2.tgz", "integrity": "sha512-7xTavNy5RQXnsjANvVvMkEjvloOinkAjv/Z6Ildz9v2RinZ4SBKTWFOVRbaF8p0vpHnyjV/UwNDdKuUv6M5qcA==", "dependencies": { "lru-cache": "^10.2.0", "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0" }, "engines": { "node": ">=16 || 14 >=14.17" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, "node_modules/picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==" }, "node_modules/picomatch": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", "engines": { "node": ">=8.6" }, "funding": { "url": "https://github.com/sponsors/jonschlinkert" } }, "node_modules/pify": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha512-udgsAY+fTnvv7kI7aaxbqwWNb0AHiB0qBO89PZKPkoTmGOgdbrHDKD+0B2X4uTfJ/FT1R09r9gTsjUjNJotuog==", "engines": { "node": ">=0.10.0" } }, "node_modules/pirates": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.6.tgz", "integrity": "sha512-saLsH7WeYYPiD25LDuLRRY/i+6HaPYr6G1OUlN39otzkSTxKnubR9RTxS3/Kk50s1g2JTgFwWQDQyplC5/SHZg==", "engines": { "node": ">= 6" } }, "node_modules/postcss": { "version": "8.4.38", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.38.tgz", "integrity": "sha512-Wglpdk03BSfXkHoQa3b/oulrotAkwrlLDRSOb9D0bN86FdRyE9lppSp33aHNPgBa0JKCoB+drFLZkQoRRYae5A==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/postcss/" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/postcss" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "dependencies": { "nanoid": "^3.3.7", "picocolors": "^1.0.0", "source-map-js": "^1.2.0" }, "engines": { "node": "^10 || ^12 || >=14" } }, "node_modules/postcss-import": { "version": "15.1.0", "resolved": "https://registry.npmjs.org/postcss-import/-/postcss-import-15.1.0.tgz", "integrity": "sha512-hpr+J05B2FVYUAXHeK1YyI267J/dDDhMU6B6civm8hSY1jYJnBXxzKDKDswzJmtLHryrjhnDjqqp/49t8FALew==", "dependencies": { "postcss-value-parser": "^4.0.0", "read-cache": "^1.0.0", "resolve": "^1.1.7" }, "engines": { "node": ">=14.0.0" }, "peerDependencies": { "postcss": "^8.0.0" } }, "node_modules/postcss-js": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/postcss-js/-/postcss-js-4.0.1.tgz", "integrity": "sha512-dDLF8pEO191hJMtlHFPRa8xsizHaM82MLfNkUHdUtVEV3tgTp5oj+8qbEqYM57SLfc74KSbw//4SeJma2LRVIw==", "dependencies": { "camelcase-css": "^2.0.1" }, "engines": { "node": "^12 || ^14 || >= 16" }, "funding": { "type": "opencollective", "url": "https://opencollective.com/postcss/" }, "peerDependencies": { "postcss": "^8.4.21" } }, "node_modules/postcss-load-config": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/postcss-load-config/-/postcss-load-config-4.0.2.tgz", "integrity": "sha512-bSVhyJGL00wMVoPUzAVAnbEoWyqRxkjv64tUl427SKnPrENtq6hJwUojroMz2VB+Q1edmi4IfrAPpami5VVgMQ==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/postcss/" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "dependencies": { "lilconfig": "^3.0.0", "yaml": "^2.3.4" }, "engines": { "node": ">= 14" }, "peerDependencies": { "postcss": ">=8.0.9", "ts-node": ">=9.0.0" }, "peerDependenciesMeta": { "postcss": { "optional": true }, "ts-node": { "optional": true } } }, "node_modules/postcss-load-config/node_modules/lilconfig": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-3.1.1.tgz", "integrity": "sha512-O18pf7nyvHTckunPWCV1XUNXU1piu01y2b7ATJ0ppkUkk8ocqVWBrYjJBCwHDjD/ZWcfyrA0P4gKhzWGi5EINQ==", "engines": { "node": ">=14" }, "funding": { "url": "https://github.com/sponsors/antonk52" } }, "node_modules/postcss-nested": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/postcss-nested/-/postcss-nested-6.0.1.tgz", "integrity": "sha512-mEp4xPMi5bSWiMbsgoPfcP74lsWLHkQbZc3sY+jWYd65CUwXrUaTp0fmNpa01ZcETKlIgUdFN/MpS2xZtqL9dQ==", "dependencies": { "postcss-selector-parser": "^6.0.11" }, "engines": { "node": ">=12.0" }, "funding": { "type": "opencollective", "url": "https://opencollective.com/postcss/" }, "peerDependencies": { "postcss": "^8.2.14" } }, "node_modules/postcss-selector-parser": { "version": "6.0.16", "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-6.0.16.tgz", "integrity": "sha512-A0RVJrX+IUkVZbW3ClroRWurercFhieevHB38sr2+l9eUClMqome3LmEmnhlNy+5Mr2EYN6B2Kaw9wYdd+VHiw==", "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" }, "engines": { "node": ">=4" } }, "node_modules/postcss-value-parser": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz", "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==" }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", "funding": [ { "type": "github", "url": "https://github.com/sponsors/feross" }, { "type": "patreon", "url": "https://www.patreon.com/feross" }, { "type": "consulting", "url": "https://feross.org/support" } ] }, "node_modules/read-cache": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz", "integrity": "sha512-Owdv/Ft7IjOgm/i0xvNDZ1LrRANRfew4b2prF3OWMQLxLfu3bS8FVhCsrSCMK4lR56Y9ya+AThoTpDCTxCmpRA==", "dependencies": { "pify": "^2.3.0" } }, "node_modules/readdirp": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", "dependencies": { "picomatch": "^2.2.1" }, "engines": { "node": ">=8.10.0" } }, "node_modules/resolve": { "version": "1.22.8", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.8.tgz", "integrity": "sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==", "dependencies": { "is-core-module": "^2.13.0", "path-parse": "^1.0.7", "supports-preserve-symlinks-flag": "^1.0.0" }, "bin": { "resolve": "bin/resolve" }, "funding": { "url": "https://github.com/sponsors/ljharb" } }, "node_modules/reusify": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==", "engines": { "iojs": ">=1.0.0", "node": ">=0.10.0" } }, "node_modules/rollup": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.17.1.tgz", "integrity": "sha512-0gG94inrUtg25sB2V/pApwiv1lUb0bQ25FPNuzO89Baa+B+c0ccaaBKM5zkZV/12pUUdH+lWCSm9wmHqyocuVQ==", "dependencies": { "@types/estree": "1.0.5" }, "bin": { "rollup": "dist/bin/rollup" }, "engines": { "node": ">=18.0.0", "npm": ">=8.0.0" }, "optionalDependencies": { "@rollup/rollup-android-arm-eabi": "4.17.1", "@rollup/rollup-android-arm64": "4.17.1", "@rollup/rollup-darwin-arm64": "4.17.1", "@rollup/rollup-darwin-x64": "4.17.1", "@rollup/rollup-linux-arm-gnueabihf": "4.17.1", "@rollup/rollup-linux-arm-musleabihf": "4.17.1", "@rollup/rollup-linux-arm64-gnu": "4.17.1", "@rollup/rollup-linux-arm64-musl": "4.17.1", "@rollup/rollup-linux-powerpc64le-gnu": "4.17.1", "@rollup/rollup-linux-riscv64-gnu": "4.17.1", "@rollup/rollup-linux-s390x-gnu": "4.17.1", "@rollup/rollup-linux-x64-gnu": "4.17.1", "@rollup/rollup-linux-x64-musl": "4.17.1", "@rollup/rollup-win32-arm64-msvc": "4.17.1", "@rollup/rollup-win32-ia32-msvc": "4.17.1", "@rollup/rollup-win32-x64-msvc": "4.17.1", "fsevents": "~2.3.2" } }, "node_modules/run-parallel": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==", "funding": [ { "type": "github", "url": "https://github.com/sponsors/feross" }, { "type": "patreon", "url": "https://www.patreon.com/feross" }, { "type": "consulting", "url": "https://feross.org/support" } ], "dependencies": { "queue-microtask": "^1.2.2" } }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", "dependencies": { "shebang-regex": "^3.0.0" }, "engines": { "node": ">=8" } }, "node_modules/shebang-regex": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", "engines": { "node": ">=8" } }, "node_modules/signal-exit": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", "engines": { "node": ">=14" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", "optional": true, "peer": true, "engines": { "node": ">=0.10.0" } }, "node_modules/source-map-js": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.0.tgz", "integrity": "sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==", "engines": { "node": ">=0.10.0" } }, "node_modules/source-map-support": { "version": "0.5.21", "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", "optional": true, "peer": true, "dependencies": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" } }, "node_modules/string-width": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-5.1.2.tgz", "integrity": "sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==", "dependencies": { "eastasianwidth": "^0.2.0", "emoji-regex": "^9.2.2", "strip-ansi": "^7.0.1" }, "engines": { "node": ">=12" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/string-width-cjs": { "name": "string-width", "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" }, "engines": { "node": ">=8" } }, "node_modules/string-width-cjs/node_modules/ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", "engines": { "node": ">=8" } }, "node_modules/string-width-cjs/node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" }, "node_modules/string-width-cjs/node_modules/strip-ansi": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", "dependencies": { "ansi-regex": "^5.0.1" }, "engines": { "node": ">=8" } }, "node_modules/strip-ansi": { "version": "7.1.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz", "integrity": "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==", "dependencies": { "ansi-regex": "^6.0.1" }, "engines": { "node": ">=12" }, "funding": { "url": "https://github.com/chalk/strip-ansi?sponsor=1" } }, "node_modules/strip-ansi-cjs": { "name": "strip-ansi", "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", "dependencies": { "ansi-regex": "^5.0.1" }, "engines": { "node": ">=8" } }, "node_modules/strip-ansi-cjs/node_modules/ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", "engines": { "node": ">=8" } }, "node_modules/sucrase": { "version": "3.35.0", "resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.35.0.tgz", "integrity": "sha512-8EbVDiu9iN/nESwxeSxDKe0dunta1GOlHufmSSXxMD2z2/tMZpDMpvXQGsc+ajGo8y2uYUmixaSRUc/QPoQ0GA==", "dependencies": { "@jridgewell/gen-mapping": "^0.3.2", "commander": "^4.0.0", "glob": "^10.3.10", "lines-and-columns": "^1.1.6", "mz": "^2.7.0", "pirates": "^4.0.1", "ts-interface-checker": "^0.1.9" }, "bin": { "sucrase": "bin/sucrase", "sucrase-node": "bin/sucrase-node" }, "engines": { "node": ">=16 || 14 >=14.17" } }, "node_modules/supports-preserve-symlinks-flag": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", "engines": { "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" } }, "node_modules/tailwindcss": { "version": "3.4.3", "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.3.tgz", "integrity": "sha512-U7sxQk/n397Bmx4JHbJx/iSOOv5G+II3f1kpLpY2QeUv5DcPdcTsYLlusZfq1NthHS1c1cZoyFmmkex1rzke0A==", "dependencies": { "@alloc/quick-lru": "^5.2.0", "arg": "^5.0.2", "chokidar": "^3.5.3", "didyoumean": "^1.2.2", "dlv": "^1.1.3", "fast-glob": "^3.3.0", "glob-parent": "^6.0.2", "is-glob": "^4.0.3", "jiti": "^1.21.0", "lilconfig": "^2.1.0", "micromatch": "^4.0.5", "normalize-path": "^3.0.0", "object-hash": "^3.0.0", "picocolors": "^1.0.0", "postcss": "^8.4.23", "postcss-import": "^15.1.0", "postcss-js": "^4.0.1", "postcss-load-config": "^4.0.1", "postcss-nested": "^6.0.1", "postcss-selector-parser": "^6.0.11", "resolve": "^1.22.2", "sucrase": "^3.32.0" }, "bin": { "tailwind": "lib/cli.js", "tailwindcss": "lib/cli.js" }, "engines": { "node": ">=14.0.0" } }, "node_modules/terser": { "version": "5.31.0", "resolved": "https://registry.npmjs.org/terser/-/terser-5.31.0.tgz", "integrity": "sha512-Q1JFAoUKE5IMfI4Z/lkE/E6+SwgzO+x4tq4v1AyBLRj8VSYvRO6A/rQrPg1yud4g0En9EKI1TvFRF2tQFcoUkg==", "optional": true, "peer": true, "dependencies": { "@jridgewell/source-map": "^0.3.3", "acorn": "^8.8.2", "commander": "^2.20.0", "source-map-support": "~0.5.20" }, "bin": { "terser": "bin/terser" }, "engines": { "node": ">=10" } }, "node_modules/terser/node_modules/commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", "optional": true, "peer": true }, "node_modules/thenify": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/thenify/-/thenify-3.3.1.tgz", "integrity": "sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==", "dependencies": { "any-promise": "^1.0.0" } }, "node_modules/thenify-all": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/thenify-all/-/thenify-all-1.6.0.tgz", "integrity": "sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA==", "dependencies": { "thenify": ">= 3.1.0 < 4" }, "engines": { "node": ">=0.8" } }, "node_modules/tippy.js": { "version": "6.3.7", "resolved": "https://registry.npmjs.org/tippy.js/-/tippy.js-6.3.7.tgz", "integrity": "sha512-E1d3oP2emgJ9dRQZdf3Kkn0qJgI6ZLpyS5z6ZkY1DF3kaQaBsGZsndEpHwx+eC+tYM41HaSNvNtLx8tU57FzTQ==", "dependencies": { "@popperjs/core": "^2.9.0" } }, "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dependencies": { "is-number": "^7.0.0" }, "engines": { "node": ">=8.0" } }, "node_modules/ts-interface-checker": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz", "integrity": "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==" }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", "optional": true, "peer": true }, "node_modules/update-browserslist-db": { "version": "1.0.13", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz", "integrity": "sha512-xebP81SNcPuNpPP3uzeW1NYXxI3rxyJzF3pD6sH4jE7o/IX+WtSpwnVU+qIsDPyk0d3hmFQ7mjqc6AtV604hbg==", "funding": [ { "type": "opencollective", "url": "https://opencollective.com/browserslist" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/browserslist" }, { "type": "github", "url": "https://github.com/sponsors/ai" } ], "dependencies": { "escalade": "^3.1.1", "picocolors": "^1.0.0" }, "bin": { "update-browserslist-db": "cli.js" }, "peerDependencies": { "browserslist": ">= 4.21.0" } }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, "node_modules/vite": { "version": "5.2.10", "resolved": "https://registry.npmjs.org/vite/-/vite-5.2.10.tgz", "integrity": "sha512-PAzgUZbP7msvQvqdSD+ErD5qGnSFiGOoWmV5yAKUEI0kdhjbH6nMWVyZQC/hSc4aXwc0oJ9aEdIiF9Oje0JFCw==", "dependencies": { "esbuild": "^0.20.1", "postcss": "^8.4.38", "rollup": "^4.13.0" }, "bin": { "vite": "bin/vite.js" }, "engines": { "node": "^18.0.0 || >=20.0.0" }, "funding": { "url": "https://github.com/vitejs/vite?sponsor=1" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^18.0.0 || >=20.0.0", "less": "*", "lightningcss": "^1.21.0", "sass": "*", "stylus": "*", "sugarss": "*", "terser": "^5.4.0" }, "peerDependenciesMeta": { "@types/node": { "optional": true }, "less": { "optional": true }, "lightningcss": { "optional": true }, "sass": { "optional": true }, "stylus": { "optional": true }, "sugarss": { "optional": true }, "terser": { "optional": true } } }, "node_modules/vite-plugin-dynamic-import": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/vite-plugin-dynamic-import/-/vite-plugin-dynamic-import-1.5.0.tgz", "integrity": "sha512-Qp85c+AVJmLa8MLni74U4BDiWpUeFNx7NJqbGZyR2XJOU7mgW0cb7nwlAMucFyM4arEd92Nfxp4j44xPi6Fu7g==", "dependencies": { "acorn": "^8.8.2", "es-module-lexer": "^1.2.1", "fast-glob": "^3.2.12", "magic-string": "^0.30.1" } }, "node_modules/vite-require": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/vite-require/-/vite-require-0.2.3.tgz", "integrity": "sha512-Lpeg5mxXiLAVrylKK9FMF8d6yxLBm6YtHXmtWpJSDSWBdl12tBsMGHOBC/fsLzATj+Zp9FHjqwzE1JayOPi9dQ==", "dependencies": { "fast-glob": "^3.2.11", "vite-plugin-dynamic-import": "^1.1.1" } }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", "dependencies": { "isexe": "^2.0.0" }, "bin": { "node-which": "bin/node-which" }, "engines": { "node": ">= 8" } }, "node_modules/wrap-ansi": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz", "integrity": "sha512-si7QWI6zUMq56bESFvagtmzMdGOtoxfR+Sez11Mobfc7tm+VkUckk9bW2UeffTGVUbOksxmSw0AA2gs8g71NCQ==", "dependencies": { "ansi-styles": "^6.1.0", "string-width": "^5.0.1", "strip-ansi": "^7.0.1" }, "engines": { "node": ">=12" }, "funding": { "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, "node_modules/wrap-ansi-cjs": { "name": "wrap-ansi", "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", "strip-ansi": "^6.0.0" }, "engines": { "node": ">=10" }, "funding": { "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, "node_modules/wrap-ansi-cjs/node_modules/ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", "engines": { "node": ">=8" } }, "node_modules/wrap-ansi-cjs/node_modules/ansi-styles": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", "dependencies": { "color-convert": "^2.0.1" }, "engines": { "node": ">=8" }, "funding": { "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, "node_modules/wrap-ansi-cjs/node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" }, "node_modules/wrap-ansi-cjs/node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" }, "engines": { "node": ">=8" } }, "node_modules/wrap-ansi-cjs/node_modules/strip-ansi": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", "dependencies": { "ansi-regex": "^5.0.1" }, "engines": { "node": ">=8" } }, "node_modules/yaml": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.2.tgz", "integrity": "sha512-B3VqDZ+JAg1nZpaEmWtTXUlBneoGx6CPM9b0TENK6aoSu5t73dItudwdgmi6tHlIZZId4dZ9skcAQ2UbcyAeVA==", "bin": { "yaml": "bin.mjs" }, "engines": { "node": ">= 14" } } } } framework/src/Illuminate/Foundation/resources/exceptions/renderer/dist/light-mode.css 0000644 00000002436 15060132305 0025250 0 ustar 00 pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}/*! Theme: GitHub Description: Light theme as seen on github.com Author: github.com Maintainer: @Hirse Updated: 2021-05-15 Outdated base version: https://github.com/primer/github-syntax-light Current colors taken from GitHub's CSS */.hljs{color:#24292e;background:#fff}.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-template-tag,.hljs-template-variable,.hljs-type,.hljs-variable.language_{color:#d73a49}.hljs-title,.hljs-title.class_,.hljs-title.class_.inherited__,.hljs-title.function_{color:#6f42c1}.hljs-attr,.hljs-attribute,.hljs-literal,.hljs-meta,.hljs-number,.hljs-operator,.hljs-selector-attr,.hljs-selector-class,.hljs-selector-id,.hljs-variable{color:#005cc5}.hljs-meta .hljs-string,.hljs-regexp,.hljs-string{color:#032f62}.hljs-built_in,.hljs-symbol{color:#e36209}.hljs-code,.hljs-comment,.hljs-formula{color:#6a737d}.hljs-name,.hljs-quote,.hljs-selector-pseudo,.hljs-selector-tag{color:#22863a}.hljs-subst{color:#24292e}.hljs-section{color:#005cc5;font-weight:700}.hljs-bullet{color:#735c0f}.hljs-emphasis{color:#24292e;font-style:italic}.hljs-strong{color:#24292e;font-weight:700}.hljs-addition{color:#22863a;background-color:#f0fff4}.hljs-deletion{color:#b31d28;background-color:#ffeef0} framework/src/Illuminate/Foundation/resources/exceptions/renderer/dist/dark-mode.css 0000644 00000001531 15060132305 0025055 0 ustar 00 pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}.hljs{color:#abb2bf;background:#282c34}.hljs-comment,.hljs-quote{color:#5c6370;font-style:italic}.hljs-doctag,.hljs-formula,.hljs-keyword{color:#c678dd}.hljs-deletion,.hljs-name,.hljs-section,.hljs-selector-tag,.hljs-subst{color:#e06c75}.hljs-literal{color:#56b6c2}.hljs-addition,.hljs-attribute,.hljs-meta .hljs-string,.hljs-regexp,.hljs-string{color:#98c379}.hljs-attr,.hljs-number,.hljs-selector-attr,.hljs-selector-class,.hljs-selector-pseudo,.hljs-template-variable,.hljs-type,.hljs-variable{color:#d19a66}.hljs-bullet,.hljs-link,.hljs-meta,.hljs-selector-id,.hljs-symbol,.hljs-title{color:#61aeee}.hljs-built_in,.hljs-class .hljs-title,.hljs-title.class_{color:#e6c07b}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}.hljs-link{text-decoration:underline} framework/src/Illuminate/Foundation/resources/exceptions/renderer/dist/scripts.js 0000644 00000322406 15060132305 0024534 0 ustar 00 var ce="top",be="bottom",ye="right",ue="left",nr="auto",Bt=[ce,be,ye,ue],dt="start",Dt="end",ua="clippingParents",mi="viewport",Et="popper",la="reference",Pr=Bt.reduce(function(e,t){return e.concat([t+"-"+dt,t+"-"+Dt])},[]),xi=[].concat(Bt,[nr]).reduce(function(e,t){return e.concat([t,t+"-"+dt,t+"-"+Dt])},[]),fa="beforeRead",da="read",pa="afterRead",ha="beforeMain",ga="main",va="afterMain",_a="beforeWrite",ba="write",ya="afterWrite",ma=[fa,da,pa,ha,ga,va,_a,ba,ya];function Me(e){return e?(e.nodeName||"").toLowerCase():null}function fe(e){if(e==null)return window;if(e.toString()!=="[object Window]"){var t=e.ownerDocument;return t&&t.defaultView||window}return e}function it(e){var t=fe(e).Element;return e instanceof t||e instanceof Element}function _e(e){var t=fe(e).HTMLElement;return e instanceof t||e instanceof HTMLElement}function rr(e){if(typeof ShadowRoot>"u")return!1;var t=fe(e).ShadowRoot;return e instanceof t||e instanceof ShadowRoot}function xa(e){var t=e.state;Object.keys(t.elements).forEach(function(n){var r=t.styles[n]||{},i=t.attributes[n]||{},o=t.elements[n];!_e(o)||!Me(o)||(Object.assign(o.style,r),Object.keys(i).forEach(function(a){var s=i[a];s===!1?o.removeAttribute(a):o.setAttribute(a,s===!0?"":s)}))})}function Ea(e){var t=e.state,n={popper:{position:t.options.strategy,left:"0",top:"0",margin:"0"},arrow:{position:"absolute"},reference:{}};return Object.assign(t.elements.popper.style,n.popper),t.styles=n,t.elements.arrow&&Object.assign(t.elements.arrow.style,n.arrow),function(){Object.keys(t.elements).forEach(function(r){var i=t.elements[r],o=t.attributes[r]||{},a=Object.keys(t.styles.hasOwnProperty(r)?t.styles[r]:n[r]),s=a.reduce(function(c,f){return c[f]="",c},{});!_e(i)||!Me(i)||(Object.assign(i.style,s),Object.keys(o).forEach(function(c){i.removeAttribute(c)}))})}}const Ei={name:"applyStyles",enabled:!0,phase:"write",fn:xa,effect:Ea,requires:["computeStyles"]};function Te(e){return e.split("-")[0]}var Ze=Math.max,rn=Math.min,pt=Math.round;function Pn(){var e=navigator.userAgentData;return e!=null&&e.brands&&Array.isArray(e.brands)?e.brands.map(function(t){return t.brand+"/"+t.version}).join(" "):navigator.userAgent}function wi(){return!/^((?!chrome|android).)*safari/i.test(Pn())}function ht(e,t,n){t===void 0&&(t=!1),n===void 0&&(n=!1);var r=e.getBoundingClientRect(),i=1,o=1;t&&_e(e)&&(i=e.offsetWidth>0&&pt(r.width)/e.offsetWidth||1,o=e.offsetHeight>0&&pt(r.height)/e.offsetHeight||1);var a=it(e)?fe(e):window,s=a.visualViewport,c=!wi()&&n,f=(r.left+(c&&s?s.offsetLeft:0))/i,l=(r.top+(c&&s?s.offsetTop:0))/o,v=r.width/i,b=r.height/o;return{width:v,height:b,top:l,right:f+v,bottom:l+b,left:f,x:f,y:l}}function ir(e){var t=ht(e),n=e.offsetWidth,r=e.offsetHeight;return Math.abs(t.width-n)<=1&&(n=t.width),Math.abs(t.height-r)<=1&&(r=t.height),{x:e.offsetLeft,y:e.offsetTop,width:n,height:r}}function Oi(e,t){var n=t.getRootNode&&t.getRootNode();if(e.contains(t))return!0;if(n&&rr(n)){var r=t;do{if(r&&e.isSameNode(r))return!0;r=r.parentNode||r.host}while(r)}return!1}function De(e){return fe(e).getComputedStyle(e)}function wa(e){return["table","td","th"].indexOf(Me(e))>=0}function We(e){return((it(e)?e.ownerDocument:e.document)||window.document).documentElement}function fn(e){return Me(e)==="html"?e:e.assignedSlot||e.parentNode||(rr(e)?e.host:null)||We(e)}function kr(e){return!_e(e)||De(e).position==="fixed"?null:e.offsetParent}function Oa(e){var t=/firefox/i.test(Pn()),n=/Trident/i.test(Pn());if(n&&_e(e)){var r=De(e);if(r.position==="fixed")return null}var i=fn(e);for(rr(i)&&(i=i.host);_e(i)&&["html","body"].indexOf(Me(i))<0;){var o=De(i);if(o.transform!=="none"||o.perspective!=="none"||o.contain==="paint"||["transform","perspective"].indexOf(o.willChange)!==-1||t&&o.willChange==="filter"||t&&o.filter&&o.filter!=="none")return i;i=i.parentNode}return null}function $t(e){for(var t=fe(e),n=kr(e);n&&wa(n)&&De(n).position==="static";)n=kr(n);return n&&(Me(n)==="html"||Me(n)==="body"&&De(n).position==="static")?t:n||Oa(e)||t}function or(e){return["top","bottom"].indexOf(e)>=0?"x":"y"}function Tt(e,t,n){return Ze(e,rn(t,n))}function Aa(e,t,n){var r=Tt(e,t,n);return r>n?n:r}function Ai(){return{top:0,right:0,bottom:0,left:0}}function Si(e){return Object.assign({},Ai(),e)}function Ti(e,t){return t.reduce(function(n,r){return n[r]=e,n},{})}var Sa=function(t,n){return t=typeof t=="function"?t(Object.assign({},n.rects,{placement:n.placement})):t,Si(typeof t!="number"?t:Ti(t,Bt))};function Ta(e){var t,n=e.state,r=e.name,i=e.options,o=n.elements.arrow,a=n.modifiersData.popperOffsets,s=Te(n.placement),c=or(s),f=[ue,ye].indexOf(s)>=0,l=f?"height":"width";if(!(!o||!a)){var v=Sa(i.padding,n),b=ir(o),_=c==="y"?ce:ue,A=c==="y"?be:ye,S=n.rects.reference[l]+n.rects.reference[c]-a[c]-n.rects.popper[l],h=a[c]-n.rects.reference[c],E=$t(o),O=E?c==="y"?E.clientHeight||0:E.clientWidth||0:0,M=S/2-h/2,u=v[_],I=O-b[l]-v[A],m=O/2-b[l]/2+M,L=Tt(u,m,I),K=c;n.modifiersData[r]=(t={},t[K]=L,t.centerOffset=L-m,t)}}function Ma(e){var t=e.state,n=e.options,r=n.element,i=r===void 0?"[data-popper-arrow]":r;i!=null&&(typeof i=="string"&&(i=t.elements.popper.querySelector(i),!i)||Oi(t.elements.popper,i)&&(t.elements.arrow=i))}const Ca={name:"arrow",enabled:!0,phase:"main",fn:Ta,effect:Ma,requires:["popperOffsets"],requiresIfExists:["preventOverflow"]};function gt(e){return e.split("-")[1]}var Ra={top:"auto",right:"auto",bottom:"auto",left:"auto"};function Ia(e,t){var n=e.x,r=e.y,i=t.devicePixelRatio||1;return{x:pt(n*i)/i||0,y:pt(r*i)/i||0}}function Br(e){var t,n=e.popper,r=e.popperRect,i=e.placement,o=e.variation,a=e.offsets,s=e.position,c=e.gpuAcceleration,f=e.adaptive,l=e.roundOffsets,v=e.isFixed,b=a.x,_=b===void 0?0:b,A=a.y,S=A===void 0?0:A,h=typeof l=="function"?l({x:_,y:S}):{x:_,y:S};_=h.x,S=h.y;var E=a.hasOwnProperty("x"),O=a.hasOwnProperty("y"),M=ue,u=ce,I=window;if(f){var m=$t(n),L="clientHeight",K="clientWidth";if(m===fe(n)&&(m=We(n),De(m).position!=="static"&&s==="absolute"&&(L="scrollHeight",K="scrollWidth")),m=m,i===ce||(i===ue||i===ye)&&o===Dt){u=be;var P=v&&m===I&&I.visualViewport?I.visualViewport.height:m[L];S-=P-r.height,S*=c?1:-1}if(i===ue||(i===ce||i===be)&&o===Dt){M=ye;var H=v&&m===I&&I.visualViewport?I.visualViewport.width:m[K];_-=H-r.width,_*=c?1:-1}}var q=Object.assign({position:s},f&&Ra),z=l===!0?Ia({x:_,y:S},fe(n)):{x:_,y:S};if(_=z.x,S=z.y,c){var k;return Object.assign({},q,(k={},k[u]=O?"0":"",k[M]=E?"0":"",k.transform=(I.devicePixelRatio||1)<=1?"translate("+_+"px, "+S+"px)":"translate3d("+_+"px, "+S+"px, 0)",k))}return Object.assign({},q,(t={},t[u]=O?S+"px":"",t[M]=E?_+"px":"",t.transform="",t))}function Da(e){var t=e.state,n=e.options,r=n.gpuAcceleration,i=r===void 0?!0:r,o=n.adaptive,a=o===void 0?!0:o,s=n.roundOffsets,c=s===void 0?!0:s,f={placement:Te(t.placement),variation:gt(t.placement),popper:t.elements.popper,popperRect:t.rects.popper,gpuAcceleration:i,isFixed:t.options.strategy==="fixed"};t.modifiersData.popperOffsets!=null&&(t.styles.popper=Object.assign({},t.styles.popper,Br(Object.assign({},f,{offsets:t.modifiersData.popperOffsets,position:t.options.strategy,adaptive:a,roundOffsets:c})))),t.modifiersData.arrow!=null&&(t.styles.arrow=Object.assign({},t.styles.arrow,Br(Object.assign({},f,{offsets:t.modifiersData.arrow,position:"absolute",adaptive:!1,roundOffsets:c})))),t.attributes.popper=Object.assign({},t.attributes.popper,{"data-popper-placement":t.placement})}const Na={name:"computeStyles",enabled:!0,phase:"beforeWrite",fn:Da,data:{}};var Vt={passive:!0};function La(e){var t=e.state,n=e.instance,r=e.options,i=r.scroll,o=i===void 0?!0:i,a=r.resize,s=a===void 0?!0:a,c=fe(t.elements.popper),f=[].concat(t.scrollParents.reference,t.scrollParents.popper);return o&&f.forEach(function(l){l.addEventListener("scroll",n.update,Vt)}),s&&c.addEventListener("resize",n.update,Vt),function(){o&&f.forEach(function(l){l.removeEventListener("scroll",n.update,Vt)}),s&&c.removeEventListener("resize",n.update,Vt)}}const Pa={name:"eventListeners",enabled:!0,phase:"write",fn:function(){},effect:La,data:{}};var ka={left:"right",right:"left",bottom:"top",top:"bottom"};function en(e){return e.replace(/left|right|bottom|top/g,function(t){return ka[t]})}var Ba={start:"end",end:"start"};function $r(e){return e.replace(/start|end/g,function(t){return Ba[t]})}function ar(e){var t=fe(e),n=t.pageXOffset,r=t.pageYOffset;return{scrollLeft:n,scrollTop:r}}function sr(e){return ht(We(e)).left+ar(e).scrollLeft}function $a(e,t){var n=fe(e),r=We(e),i=n.visualViewport,o=r.clientWidth,a=r.clientHeight,s=0,c=0;if(i){o=i.width,a=i.height;var f=wi();(f||!f&&t==="fixed")&&(s=i.offsetLeft,c=i.offsetTop)}return{width:o,height:a,x:s+sr(e),y:c}}function ja(e){var t,n=We(e),r=ar(e),i=(t=e.ownerDocument)==null?void 0:t.body,o=Ze(n.scrollWidth,n.clientWidth,i?i.scrollWidth:0,i?i.clientWidth:0),a=Ze(n.scrollHeight,n.clientHeight,i?i.scrollHeight:0,i?i.clientHeight:0),s=-r.scrollLeft+sr(e),c=-r.scrollTop;return De(i||n).direction==="rtl"&&(s+=Ze(n.clientWidth,i?i.clientWidth:0)-o),{width:o,height:a,x:s,y:c}}function cr(e){var t=De(e),n=t.overflow,r=t.overflowX,i=t.overflowY;return/auto|scroll|overlay|hidden/.test(n+i+r)}function Mi(e){return["html","body","#document"].indexOf(Me(e))>=0?e.ownerDocument.body:_e(e)&&cr(e)?e:Mi(fn(e))}function Mt(e,t){var n;t===void 0&&(t=[]);var r=Mi(e),i=r===((n=e.ownerDocument)==null?void 0:n.body),o=fe(r),a=i?[o].concat(o.visualViewport||[],cr(r)?r:[]):r,s=t.concat(a);return i?s:s.concat(Mt(fn(a)))}function kn(e){return Object.assign({},e,{left:e.x,top:e.y,right:e.x+e.width,bottom:e.y+e.height})}function Ha(e,t){var n=ht(e,!1,t==="fixed");return n.top=n.top+e.clientTop,n.left=n.left+e.clientLeft,n.bottom=n.top+e.clientHeight,n.right=n.left+e.clientWidth,n.width=e.clientWidth,n.height=e.clientHeight,n.x=n.left,n.y=n.top,n}function jr(e,t,n){return t===mi?kn($a(e,n)):it(t)?Ha(t,n):kn(ja(We(e)))}function Fa(e){var t=Mt(fn(e)),n=["absolute","fixed"].indexOf(De(e).position)>=0,r=n&&_e(e)?$t(e):e;return it(r)?t.filter(function(i){return it(i)&&Oi(i,r)&&Me(i)!=="body"}):[]}function Ua(e,t,n,r){var i=t==="clippingParents"?Fa(e):[].concat(t),o=[].concat(i,[n]),a=o[0],s=o.reduce(function(c,f){var l=jr(e,f,r);return c.top=Ze(l.top,c.top),c.right=rn(l.right,c.right),c.bottom=rn(l.bottom,c.bottom),c.left=Ze(l.left,c.left),c},jr(e,a,r));return s.width=s.right-s.left,s.height=s.bottom-s.top,s.x=s.left,s.y=s.top,s}function Ci(e){var t=e.reference,n=e.element,r=e.placement,i=r?Te(r):null,o=r?gt(r):null,a=t.x+t.width/2-n.width/2,s=t.y+t.height/2-n.height/2,c;switch(i){case ce:c={x:a,y:t.y-n.height};break;case be:c={x:a,y:t.y+t.height};break;case ye:c={x:t.x+t.width,y:s};break;case ue:c={x:t.x-n.width,y:s};break;default:c={x:t.x,y:t.y}}var f=i?or(i):null;if(f!=null){var l=f==="y"?"height":"width";switch(o){case dt:c[f]=c[f]-(t[l]/2-n[l]/2);break;case Dt:c[f]=c[f]+(t[l]/2-n[l]/2);break}}return c}function Nt(e,t){t===void 0&&(t={});var n=t,r=n.placement,i=r===void 0?e.placement:r,o=n.strategy,a=o===void 0?e.strategy:o,s=n.boundary,c=s===void 0?ua:s,f=n.rootBoundary,l=f===void 0?mi:f,v=n.elementContext,b=v===void 0?Et:v,_=n.altBoundary,A=_===void 0?!1:_,S=n.padding,h=S===void 0?0:S,E=Si(typeof h!="number"?h:Ti(h,Bt)),O=b===Et?la:Et,M=e.rects.popper,u=e.elements[A?O:b],I=Ua(it(u)?u:u.contextElement||We(e.elements.popper),c,l,a),m=ht(e.elements.reference),L=Ci({reference:m,element:M,strategy:"absolute",placement:i}),K=kn(Object.assign({},M,L)),P=b===Et?K:m,H={top:I.top-P.top+E.top,bottom:P.bottom-I.bottom+E.bottom,left:I.left-P.left+E.left,right:P.right-I.right+E.right},q=e.modifiersData.offset;if(b===Et&&q){var z=q[i];Object.keys(H).forEach(function(k){var U=[ye,be].indexOf(k)>=0?1:-1,Y=[ce,be].indexOf(k)>=0?"y":"x";H[k]+=z[Y]*U})}return H}function Wa(e,t){t===void 0&&(t={});var n=t,r=n.placement,i=n.boundary,o=n.rootBoundary,a=n.padding,s=n.flipVariations,c=n.allowedAutoPlacements,f=c===void 0?xi:c,l=gt(r),v=l?s?Pr:Pr.filter(function(A){return gt(A)===l}):Bt,b=v.filter(function(A){return f.indexOf(A)>=0});b.length===0&&(b=v);var _=b.reduce(function(A,S){return A[S]=Nt(e,{placement:S,boundary:i,rootBoundary:o,padding:a})[Te(S)],A},{});return Object.keys(_).sort(function(A,S){return _[A]-_[S]})}function Ka(e){if(Te(e)===nr)return[];var t=en(e);return[$r(e),t,$r(t)]}function za(e){var t=e.state,n=e.options,r=e.name;if(!t.modifiersData[r]._skip){for(var i=n.mainAxis,o=i===void 0?!0:i,a=n.altAxis,s=a===void 0?!0:a,c=n.fallbackPlacements,f=n.padding,l=n.boundary,v=n.rootBoundary,b=n.altBoundary,_=n.flipVariations,A=_===void 0?!0:_,S=n.allowedAutoPlacements,h=t.options.placement,E=Te(h),O=E===h,M=c||(O||!A?[en(h)]:Ka(h)),u=[h].concat(M).reduce(function(se,G){return se.concat(Te(G)===nr?Wa(t,{placement:G,boundary:l,rootBoundary:v,padding:f,flipVariations:A,allowedAutoPlacements:S}):G)},[]),I=t.rects.reference,m=t.rects.popper,L=new Map,K=!0,P=u[0],H=0;H<u.length;H++){var q=u[H],z=Te(q),k=gt(q)===dt,U=[ce,be].indexOf(z)>=0,Y=U?"width":"height",te=Nt(t,{placement:q,boundary:l,rootBoundary:v,altBoundary:b,padding:f}),p=U?k?ye:ue:k?be:ce;I[Y]>m[Y]&&(p=en(p));var y=en(p),C=[];if(o&&C.push(te[z]<=0),s&&C.push(te[p]<=0,te[y]<=0),C.every(function(se){return se})){P=q,K=!1;break}L.set(q,C)}if(K)for(var N=A?3:1,W=function(G){var Z=u.find(function(Ce){var de=L.get(Ce);if(de)return de.slice(0,G).every(function(Re){return Re})});if(Z)return P=Z,"break"},J=N;J>0;J--){var re=W(J);if(re==="break")break}t.placement!==P&&(t.modifiersData[r]._skip=!0,t.placement=P,t.reset=!0)}}const Va={name:"flip",enabled:!0,phase:"main",fn:za,requiresIfExists:["offset"],data:{_skip:!1}};function Hr(e,t,n){return n===void 0&&(n={x:0,y:0}),{top:e.top-t.height-n.y,right:e.right-t.width+n.x,bottom:e.bottom-t.height+n.y,left:e.left-t.width-n.x}}function Fr(e){return[ce,ye,be,ue].some(function(t){return e[t]>=0})}function qa(e){var t=e.state,n=e.name,r=t.rects.reference,i=t.rects.popper,o=t.modifiersData.preventOverflow,a=Nt(t,{elementContext:"reference"}),s=Nt(t,{altBoundary:!0}),c=Hr(a,r),f=Hr(s,i,o),l=Fr(c),v=Fr(f);t.modifiersData[n]={referenceClippingOffsets:c,popperEscapeOffsets:f,isReferenceHidden:l,hasPopperEscaped:v},t.attributes.popper=Object.assign({},t.attributes.popper,{"data-popper-reference-hidden":l,"data-popper-escaped":v})}const Ga={name:"hide",enabled:!0,phase:"main",requiresIfExists:["preventOverflow"],fn:qa};function Xa(e,t,n){var r=Te(e),i=[ue,ce].indexOf(r)>=0?-1:1,o=typeof n=="function"?n(Object.assign({},t,{placement:e})):n,a=o[0],s=o[1];return a=a||0,s=(s||0)*i,[ue,ye].indexOf(r)>=0?{x:s,y:a}:{x:a,y:s}}function Ya(e){var t=e.state,n=e.options,r=e.name,i=n.offset,o=i===void 0?[0,0]:i,a=xi.reduce(function(l,v){return l[v]=Xa(v,t.rects,o),l},{}),s=a[t.placement],c=s.x,f=s.y;t.modifiersData.popperOffsets!=null&&(t.modifiersData.popperOffsets.x+=c,t.modifiersData.popperOffsets.y+=f),t.modifiersData[r]=a}const Ja={name:"offset",enabled:!0,phase:"main",requires:["popperOffsets"],fn:Ya};function Za(e){var t=e.state,n=e.name;t.modifiersData[n]=Ci({reference:t.rects.reference,element:t.rects.popper,strategy:"absolute",placement:t.placement})}const Qa={name:"popperOffsets",enabled:!0,phase:"read",fn:Za,data:{}};function es(e){return e==="x"?"y":"x"}function ts(e){var t=e.state,n=e.options,r=e.name,i=n.mainAxis,o=i===void 0?!0:i,a=n.altAxis,s=a===void 0?!1:a,c=n.boundary,f=n.rootBoundary,l=n.altBoundary,v=n.padding,b=n.tether,_=b===void 0?!0:b,A=n.tetherOffset,S=A===void 0?0:A,h=Nt(t,{boundary:c,rootBoundary:f,padding:v,altBoundary:l}),E=Te(t.placement),O=gt(t.placement),M=!O,u=or(E),I=es(u),m=t.modifiersData.popperOffsets,L=t.rects.reference,K=t.rects.popper,P=typeof S=="function"?S(Object.assign({},t.rects,{placement:t.placement})):S,H=typeof P=="number"?{mainAxis:P,altAxis:P}:Object.assign({mainAxis:0,altAxis:0},P),q=t.modifiersData.offset?t.modifiersData.offset[t.placement]:null,z={x:0,y:0};if(m){if(o){var k,U=u==="y"?ce:ue,Y=u==="y"?be:ye,te=u==="y"?"height":"width",p=m[u],y=p+h[U],C=p-h[Y],N=_?-K[te]/2:0,W=O===dt?L[te]:K[te],J=O===dt?-K[te]:-L[te],re=t.elements.arrow,se=_&&re?ir(re):{width:0,height:0},G=t.modifiersData["arrow#persistent"]?t.modifiersData["arrow#persistent"].padding:Ai(),Z=G[U],Ce=G[Y],de=Tt(0,L[te],se[te]),Re=M?L[te]/2-N-de-Z-H.mainAxis:W-de-Z-H.mainAxis,Oe=M?-L[te]/2+N+de+Ce+H.mainAxis:J+de+Ce+H.mainAxis,Le=t.elements.arrow&&$t(t.elements.arrow),st=Le?u==="y"?Le.clientTop||0:Le.clientLeft||0:0,ze=(k=q==null?void 0:q[u])!=null?k:0,Ie=p+Re-ze-st,Ve=p+Oe-ze,ie=Tt(_?rn(y,Ie):y,p,_?Ze(C,Ve):C);m[u]=ie,z[u]=ie-p}if(s){var qe,Pe=u==="x"?ce:ue,T=u==="x"?be:ye,pe=m[I],V=I==="y"?"height":"width",$=pe+h[Pe],he=pe-h[T],le=[ce,ue].indexOf(E)!==-1,ke=(qe=q==null?void 0:q[I])!=null?qe:0,Be=le?$:pe-L[V]-K[V]-ke+H.altAxis,g=le?pe+L[V]+K[V]-ke-H.altAxis:he,x=_&&le?Aa(Be,pe,g):Tt(_?Be:$,pe,_?g:he);m[I]=x,z[I]=x-pe}t.modifiersData[r]=z}}const ns={name:"preventOverflow",enabled:!0,phase:"main",fn:ts,requiresIfExists:["offset"]};function rs(e){return{scrollLeft:e.scrollLeft,scrollTop:e.scrollTop}}function is(e){return e===fe(e)||!_e(e)?ar(e):rs(e)}function os(e){var t=e.getBoundingClientRect(),n=pt(t.width)/e.offsetWidth||1,r=pt(t.height)/e.offsetHeight||1;return n!==1||r!==1}function as(e,t,n){n===void 0&&(n=!1);var r=_e(t),i=_e(t)&&os(t),o=We(t),a=ht(e,i,n),s={scrollLeft:0,scrollTop:0},c={x:0,y:0};return(r||!r&&!n)&&((Me(t)!=="body"||cr(o))&&(s=is(t)),_e(t)?(c=ht(t,!0),c.x+=t.clientLeft,c.y+=t.clientTop):o&&(c.x=sr(o))),{x:a.left+s.scrollLeft-c.x,y:a.top+s.scrollTop-c.y,width:a.width,height:a.height}}function ss(e){var t=new Map,n=new Set,r=[];e.forEach(function(o){t.set(o.name,o)});function i(o){n.add(o.name);var a=[].concat(o.requires||[],o.requiresIfExists||[]);a.forEach(function(s){if(!n.has(s)){var c=t.get(s);c&&i(c)}}),r.push(o)}return e.forEach(function(o){n.has(o.name)||i(o)}),r}function cs(e){var t=ss(e);return ma.reduce(function(n,r){return n.concat(t.filter(function(i){return i.phase===r}))},[])}function us(e){var t;return function(){return t||(t=new Promise(function(n){Promise.resolve().then(function(){t=void 0,n(e())})})),t}}function ls(e){var t=e.reduce(function(n,r){var i=n[r.name];return n[r.name]=i?Object.assign({},i,r,{options:Object.assign({},i.options,r.options),data:Object.assign({},i.data,r.data)}):r,n},{});return Object.keys(t).map(function(n){return t[n]})}var Ur={placement:"bottom",modifiers:[],strategy:"absolute"};function Wr(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return!t.some(function(r){return!(r&&typeof r.getBoundingClientRect=="function")})}function fs(e){e===void 0&&(e={});var t=e,n=t.defaultModifiers,r=n===void 0?[]:n,i=t.defaultOptions,o=i===void 0?Ur:i;return function(s,c,f){f===void 0&&(f=o);var l={placement:"bottom",orderedModifiers:[],options:Object.assign({},Ur,o),modifiersData:{},elements:{reference:s,popper:c},attributes:{},styles:{}},v=[],b=!1,_={state:l,setOptions:function(E){var O=typeof E=="function"?E(l.options):E;S(),l.options=Object.assign({},o,l.options,O),l.scrollParents={reference:it(s)?Mt(s):s.contextElement?Mt(s.contextElement):[],popper:Mt(c)};var M=cs(ls([].concat(r,l.options.modifiers)));return l.orderedModifiers=M.filter(function(u){return u.enabled}),A(),_.update()},forceUpdate:function(){if(!b){var E=l.elements,O=E.reference,M=E.popper;if(Wr(O,M)){l.rects={reference:as(O,$t(M),l.options.strategy==="fixed"),popper:ir(M)},l.reset=!1,l.placement=l.options.placement,l.orderedModifiers.forEach(function(H){return l.modifiersData[H.name]=Object.assign({},H.data)});for(var u=0;u<l.orderedModifiers.length;u++){if(l.reset===!0){l.reset=!1,u=-1;continue}var I=l.orderedModifiers[u],m=I.fn,L=I.options,K=L===void 0?{}:L,P=I.name;typeof m=="function"&&(l=m({state:l,options:K,name:P,instance:_})||l)}}}},update:us(function(){return new Promise(function(h){_.forceUpdate(),h(l)})}),destroy:function(){S(),b=!0}};if(!Wr(s,c))return _;_.setOptions(f).then(function(h){!b&&f.onFirstUpdate&&f.onFirstUpdate(h)});function A(){l.orderedModifiers.forEach(function(h){var E=h.name,O=h.options,M=O===void 0?{}:O,u=h.effect;if(typeof u=="function"){var I=u({state:l,name:E,instance:_,options:M}),m=function(){};v.push(I||m)}})}function S(){v.forEach(function(h){return h()}),v=[]}return _}}var ds=[Pa,Qa,Na,Ei,Ja,Va,ns,Ca,Ga],ps=fs({defaultModifiers:ds}),hs="tippy-box",Ri="tippy-content",gs="tippy-backdrop",Ii="tippy-arrow",Di="tippy-svg-arrow",Xe={passive:!0,capture:!0},Ni=function(){return document.body};function An(e,t,n){if(Array.isArray(e)){var r=e[t];return r??(Array.isArray(n)?n[t]:n)}return e}function ur(e,t){var n={}.toString.call(e);return n.indexOf("[object")===0&&n.indexOf(t+"]")>-1}function Li(e,t){return typeof e=="function"?e.apply(void 0,t):e}function Kr(e,t){if(t===0)return e;var n;return function(r){clearTimeout(n),n=setTimeout(function(){e(r)},t)}}function vs(e){return e.split(/\s+/).filter(Boolean)}function ft(e){return[].concat(e)}function zr(e,t){e.indexOf(t)===-1&&e.push(t)}function _s(e){return e.filter(function(t,n){return e.indexOf(t)===n})}function bs(e){return e.split("-")[0]}function on(e){return[].slice.call(e)}function Vr(e){return Object.keys(e).reduce(function(t,n){return e[n]!==void 0&&(t[n]=e[n]),t},{})}function Ct(){return document.createElement("div")}function dn(e){return["Element","Fragment"].some(function(t){return ur(e,t)})}function ys(e){return ur(e,"NodeList")}function ms(e){return ur(e,"MouseEvent")}function xs(e){return!!(e&&e._tippy&&e._tippy.reference===e)}function Es(e){return dn(e)?[e]:ys(e)?on(e):Array.isArray(e)?e:on(document.querySelectorAll(e))}function Sn(e,t){e.forEach(function(n){n&&(n.style.transitionDuration=t+"ms")})}function qr(e,t){e.forEach(function(n){n&&n.setAttribute("data-state",t)})}function ws(e){var t,n=ft(e),r=n[0];return r!=null&&(t=r.ownerDocument)!=null&&t.body?r.ownerDocument:document}function Os(e,t){var n=t.clientX,r=t.clientY;return e.every(function(i){var o=i.popperRect,a=i.popperState,s=i.props,c=s.interactiveBorder,f=bs(a.placement),l=a.modifiersData.offset;if(!l)return!0;var v=f==="bottom"?l.top.y:0,b=f==="top"?l.bottom.y:0,_=f==="right"?l.left.x:0,A=f==="left"?l.right.x:0,S=o.top-r+v>c,h=r-o.bottom-b>c,E=o.left-n+_>c,O=n-o.right-A>c;return S||h||E||O})}function Tn(e,t,n){var r=t+"EventListener";["transitionend","webkitTransitionEnd"].forEach(function(i){e[r](i,n)})}function Gr(e,t){for(var n=t;n;){var r;if(e.contains(n))return!0;n=n.getRootNode==null||(r=n.getRootNode())==null?void 0:r.host}return!1}var Se={isTouch:!1},Xr=0;function As(){Se.isTouch||(Se.isTouch=!0,window.performance&&document.addEventListener("mousemove",Pi))}function Pi(){var e=performance.now();e-Xr<20&&(Se.isTouch=!1,document.removeEventListener("mousemove",Pi)),Xr=e}function Ss(){var e=document.activeElement;if(xs(e)){var t=e._tippy;e.blur&&!t.state.isVisible&&e.blur()}}function Ts(){document.addEventListener("touchstart",As,Xe),window.addEventListener("blur",Ss)}var Ms=typeof window<"u"&&typeof document<"u",Cs=Ms?!!window.msCrypto:!1,Rs={animateFill:!1,followCursor:!1,inlinePositioning:!1,sticky:!1},Is={allowHTML:!1,animation:"fade",arrow:!0,content:"",inertia:!1,maxWidth:350,role:"tooltip",theme:"",zIndex:9999},xe=Object.assign({appendTo:Ni,aria:{content:"auto",expanded:"auto"},delay:0,duration:[300,250],getReferenceClientRect:null,hideOnClick:!0,ignoreAttributes:!1,interactive:!1,interactiveBorder:2,interactiveDebounce:0,moveTransition:"",offset:[0,10],onAfterUpdate:function(){},onBeforeUpdate:function(){},onCreate:function(){},onDestroy:function(){},onHidden:function(){},onHide:function(){},onMount:function(){},onShow:function(){},onShown:function(){},onTrigger:function(){},onUntrigger:function(){},onClickOutside:function(){},placement:"top",plugins:[],popperOptions:{},render:null,showOnCreate:!1,touch:!0,trigger:"mouseenter focus",triggerTarget:null},Rs,Is),Ds=Object.keys(xe),Ns=function(t){var n=Object.keys(t);n.forEach(function(r){xe[r]=t[r]})};function ki(e){var t=e.plugins||[],n=t.reduce(function(r,i){var o=i.name,a=i.defaultValue;if(o){var s;r[o]=e[o]!==void 0?e[o]:(s=xe[o])!=null?s:a}return r},{});return Object.assign({},e,n)}function Ls(e,t){var n=t?Object.keys(ki(Object.assign({},xe,{plugins:t}))):Ds,r=n.reduce(function(i,o){var a=(e.getAttribute("data-tippy-"+o)||"").trim();if(!a)return i;if(o==="content")i[o]=a;else try{i[o]=JSON.parse(a)}catch{i[o]=a}return i},{});return r}function Yr(e,t){var n=Object.assign({},t,{content:Li(t.content,[e])},t.ignoreAttributes?{}:Ls(e,t.plugins));return n.aria=Object.assign({},xe.aria,n.aria),n.aria={expanded:n.aria.expanded==="auto"?t.interactive:n.aria.expanded,content:n.aria.content==="auto"?t.interactive?null:"describedby":n.aria.content},n}var Ps=function(){return"innerHTML"};function Bn(e,t){e[Ps()]=t}function Jr(e){var t=Ct();return e===!0?t.className=Ii:(t.className=Di,dn(e)?t.appendChild(e):Bn(t,e)),t}function Zr(e,t){dn(t.content)?(Bn(e,""),e.appendChild(t.content)):typeof t.content!="function"&&(t.allowHTML?Bn(e,t.content):e.textContent=t.content)}function $n(e){var t=e.firstElementChild,n=on(t.children);return{box:t,content:n.find(function(r){return r.classList.contains(Ri)}),arrow:n.find(function(r){return r.classList.contains(Ii)||r.classList.contains(Di)}),backdrop:n.find(function(r){return r.classList.contains(gs)})}}function Bi(e){var t=Ct(),n=Ct();n.className=hs,n.setAttribute("data-state","hidden"),n.setAttribute("tabindex","-1");var r=Ct();r.className=Ri,r.setAttribute("data-state","hidden"),Zr(r,e.props),t.appendChild(n),n.appendChild(r),i(e.props,e.props);function i(o,a){var s=$n(t),c=s.box,f=s.content,l=s.arrow;a.theme?c.setAttribute("data-theme",a.theme):c.removeAttribute("data-theme"),typeof a.animation=="string"?c.setAttribute("data-animation",a.animation):c.removeAttribute("data-animation"),a.inertia?c.setAttribute("data-inertia",""):c.removeAttribute("data-inertia"),c.style.maxWidth=typeof a.maxWidth=="number"?a.maxWidth+"px":a.maxWidth,a.role?c.setAttribute("role",a.role):c.removeAttribute("role"),(o.content!==a.content||o.allowHTML!==a.allowHTML)&&Zr(f,e.props),a.arrow?l?o.arrow!==a.arrow&&(c.removeChild(l),c.appendChild(Jr(a.arrow))):c.appendChild(Jr(a.arrow)):l&&c.removeChild(l)}return{popper:t,onUpdate:i}}Bi.$$tippy=!0;var ks=1,qt=[],Mn=[];function Bs(e,t){var n=Yr(e,Object.assign({},xe,ki(Vr(t)))),r,i,o,a=!1,s=!1,c=!1,f=!1,l,v,b,_=[],A=Kr(Ie,n.interactiveDebounce),S,h=ks++,E=null,O=_s(n.plugins),M={isEnabled:!0,isVisible:!1,isDestroyed:!1,isMounted:!1,isShown:!1},u={id:h,reference:e,popper:Ct(),popperInstance:E,props:n,state:M,plugins:O,clearDelayTimeouts:Be,setProps:g,setContent:x,show:D,hide:j,hideWithInteractivity:ne,enable:le,disable:ke,unmount:me,destroy:En};if(!n.render)return u;var I=n.render(u),m=I.popper,L=I.onUpdate;m.setAttribute("data-tippy-root",""),m.id="tippy-"+u.id,u.popper=m,e._tippy=u,m._tippy=u;var K=O.map(function(d){return d.fn(u)}),P=e.hasAttribute("aria-expanded");return Le(),N(),p(),y("onCreate",[u]),n.showOnCreate&&$(),m.addEventListener("mouseenter",function(){u.props.interactive&&u.state.isVisible&&u.clearDelayTimeouts()}),m.addEventListener("mouseleave",function(){u.props.interactive&&u.props.trigger.indexOf("mouseenter")>=0&&U().addEventListener("mousemove",A)}),u;function H(){var d=u.props.touch;return Array.isArray(d)?d:[d,0]}function q(){return H()[0]==="hold"}function z(){var d;return!!((d=u.props.render)!=null&&d.$$tippy)}function k(){return S||e}function U(){var d=k().parentNode;return d?ws(d):document}function Y(){return $n(m)}function te(d){return u.state.isMounted&&!u.state.isVisible||Se.isTouch||l&&l.type==="focus"?0:An(u.props.delay,d?0:1,xe.delay)}function p(d){d===void 0&&(d=!1),m.style.pointerEvents=u.props.interactive&&!d?"":"none",m.style.zIndex=""+u.props.zIndex}function y(d,w,R){if(R===void 0&&(R=!0),K.forEach(function(B){B[d]&&B[d].apply(B,w)}),R){var F;(F=u.props)[d].apply(F,w)}}function C(){var d=u.props.aria;if(d.content){var w="aria-"+d.content,R=m.id,F=ft(u.props.triggerTarget||e);F.forEach(function(B){var oe=B.getAttribute(w);if(u.state.isVisible)B.setAttribute(w,oe?oe+" "+R:R);else{var ge=oe&&oe.replace(R,"").trim();ge?B.setAttribute(w,ge):B.removeAttribute(w)}})}}function N(){if(!(P||!u.props.aria.expanded)){var d=ft(u.props.triggerTarget||e);d.forEach(function(w){u.props.interactive?w.setAttribute("aria-expanded",u.state.isVisible&&w===k()?"true":"false"):w.removeAttribute("aria-expanded")})}}function W(){U().removeEventListener("mousemove",A),qt=qt.filter(function(d){return d!==A})}function J(d){if(!(Se.isTouch&&(c||d.type==="mousedown"))){var w=d.composedPath&&d.composedPath()[0]||d.target;if(!(u.props.interactive&&Gr(m,w))){if(ft(u.props.triggerTarget||e).some(function(R){return Gr(R,w)})){if(Se.isTouch||u.state.isVisible&&u.props.trigger.indexOf("click")>=0)return}else y("onClickOutside",[u,d]);u.props.hideOnClick===!0&&(u.clearDelayTimeouts(),u.hide(),s=!0,setTimeout(function(){s=!1}),u.state.isMounted||Z())}}}function re(){c=!0}function se(){c=!1}function G(){var d=U();d.addEventListener("mousedown",J,!0),d.addEventListener("touchend",J,Xe),d.addEventListener("touchstart",se,Xe),d.addEventListener("touchmove",re,Xe)}function Z(){var d=U();d.removeEventListener("mousedown",J,!0),d.removeEventListener("touchend",J,Xe),d.removeEventListener("touchstart",se,Xe),d.removeEventListener("touchmove",re,Xe)}function Ce(d,w){Re(d,function(){!u.state.isVisible&&m.parentNode&&m.parentNode.contains(m)&&w()})}function de(d,w){Re(d,w)}function Re(d,w){var R=Y().box;function F(B){B.target===R&&(Tn(R,"remove",F),w())}if(d===0)return w();Tn(R,"remove",v),Tn(R,"add",F),v=F}function Oe(d,w,R){R===void 0&&(R=!1);var F=ft(u.props.triggerTarget||e);F.forEach(function(B){B.addEventListener(d,w,R),_.push({node:B,eventType:d,handler:w,options:R})})}function Le(){q()&&(Oe("touchstart",ze,{passive:!0}),Oe("touchend",Ve,{passive:!0})),vs(u.props.trigger).forEach(function(d){if(d!=="manual")switch(Oe(d,ze),d){case"mouseenter":Oe("mouseleave",Ve);break;case"focus":Oe(Cs?"focusout":"blur",ie);break;case"focusin":Oe("focusout",ie);break}})}function st(){_.forEach(function(d){var w=d.node,R=d.eventType,F=d.handler,B=d.options;w.removeEventListener(R,F,B)}),_=[]}function ze(d){var w,R=!1;if(!(!u.state.isEnabled||qe(d)||s)){var F=((w=l)==null?void 0:w.type)==="focus";l=d,S=d.currentTarget,N(),!u.state.isVisible&&ms(d)&&qt.forEach(function(B){return B(d)}),d.type==="click"&&(u.props.trigger.indexOf("mouseenter")<0||a)&&u.props.hideOnClick!==!1&&u.state.isVisible?R=!0:$(d),d.type==="click"&&(a=!R),R&&!F&&he(d)}}function Ie(d){var w=d.target,R=k().contains(w)||m.contains(w);if(!(d.type==="mousemove"&&R)){var F=V().concat(m).map(function(B){var oe,ge=B._tippy,ct=(oe=ge.popperInstance)==null?void 0:oe.state;return ct?{popperRect:B.getBoundingClientRect(),popperState:ct,props:n}:null}).filter(Boolean);Os(F,d)&&(W(),he(d))}}function Ve(d){var w=qe(d)||u.props.trigger.indexOf("click")>=0&&a;if(!w){if(u.props.interactive){u.hideWithInteractivity(d);return}he(d)}}function ie(d){u.props.trigger.indexOf("focusin")<0&&d.target!==k()||u.props.interactive&&d.relatedTarget&&m.contains(d.relatedTarget)||he(d)}function qe(d){return Se.isTouch?q()!==d.type.indexOf("touch")>=0:!1}function Pe(){T();var d=u.props,w=d.popperOptions,R=d.placement,F=d.offset,B=d.getReferenceClientRect,oe=d.moveTransition,ge=z()?$n(m).arrow:null,ct=B?{getBoundingClientRect:B,contextElement:B.contextElement||k()}:e,Lr={name:"$$tippy",enabled:!0,phase:"beforeWrite",requires:["computeStyles"],fn:function(Kt){var ut=Kt.state;if(z()){var ca=Y(),On=ca.box;["placement","reference-hidden","escaped"].forEach(function(zt){zt==="placement"?On.setAttribute("data-placement",ut.placement):ut.attributes.popper["data-popper-"+zt]?On.setAttribute("data-"+zt,""):On.removeAttribute("data-"+zt)}),ut.attributes.popper={}}}},Ge=[{name:"offset",options:{offset:F}},{name:"preventOverflow",options:{padding:{top:2,bottom:2,left:5,right:5}}},{name:"flip",options:{padding:5}},{name:"computeStyles",options:{adaptive:!oe}},Lr];z()&&ge&&Ge.push({name:"arrow",options:{element:ge,padding:3}}),Ge.push.apply(Ge,(w==null?void 0:w.modifiers)||[]),u.popperInstance=ps(ct,m,Object.assign({},w,{placement:R,onFirstUpdate:b,modifiers:Ge}))}function T(){u.popperInstance&&(u.popperInstance.destroy(),u.popperInstance=null)}function pe(){var d=u.props.appendTo,w,R=k();u.props.interactive&&d===Ni||d==="parent"?w=R.parentNode:w=Li(d,[R]),w.contains(m)||w.appendChild(m),u.state.isMounted=!0,Pe()}function V(){return on(m.querySelectorAll("[data-tippy-root]"))}function $(d){u.clearDelayTimeouts(),d&&y("onTrigger",[u,d]),G();var w=te(!0),R=H(),F=R[0],B=R[1];Se.isTouch&&F==="hold"&&B&&(w=B),w?r=setTimeout(function(){u.show()},w):u.show()}function he(d){if(u.clearDelayTimeouts(),y("onUntrigger",[u,d]),!u.state.isVisible){Z();return}if(!(u.props.trigger.indexOf("mouseenter")>=0&&u.props.trigger.indexOf("click")>=0&&["mouseleave","mousemove"].indexOf(d.type)>=0&&a)){var w=te(!1);w?i=setTimeout(function(){u.state.isVisible&&u.hide()},w):o=requestAnimationFrame(function(){u.hide()})}}function le(){u.state.isEnabled=!0}function ke(){u.hide(),u.state.isEnabled=!1}function Be(){clearTimeout(r),clearTimeout(i),cancelAnimationFrame(o)}function g(d){if(!u.state.isDestroyed){y("onBeforeUpdate",[u,d]),st();var w=u.props,R=Yr(e,Object.assign({},w,Vr(d),{ignoreAttributes:!0}));u.props=R,Le(),w.interactiveDebounce!==R.interactiveDebounce&&(W(),A=Kr(Ie,R.interactiveDebounce)),w.triggerTarget&&!R.triggerTarget?ft(w.triggerTarget).forEach(function(F){F.removeAttribute("aria-expanded")}):R.triggerTarget&&e.removeAttribute("aria-expanded"),N(),p(),L&&L(w,R),u.popperInstance&&(Pe(),V().forEach(function(F){requestAnimationFrame(F._tippy.popperInstance.forceUpdate)})),y("onAfterUpdate",[u,d])}}function x(d){u.setProps({content:d})}function D(){var d=u.state.isVisible,w=u.state.isDestroyed,R=!u.state.isEnabled,F=Se.isTouch&&!u.props.touch,B=An(u.props.duration,0,xe.duration);if(!(d||w||R||F)&&!k().hasAttribute("disabled")&&(y("onShow",[u],!1),u.props.onShow(u)!==!1)){if(u.state.isVisible=!0,z()&&(m.style.visibility="visible"),p(),G(),u.state.isMounted||(m.style.transition="none"),z()){var oe=Y(),ge=oe.box,ct=oe.content;Sn([ge,ct],0)}b=function(){var Ge;if(!(!u.state.isVisible||f)){if(f=!0,m.offsetHeight,m.style.transition=u.props.moveTransition,z()&&u.props.animation){var wn=Y(),Kt=wn.box,ut=wn.content;Sn([Kt,ut],B),qr([Kt,ut],"visible")}C(),N(),zr(Mn,u),(Ge=u.popperInstance)==null||Ge.forceUpdate(),y("onMount",[u]),u.props.animation&&z()&&de(B,function(){u.state.isShown=!0,y("onShown",[u])})}},pe()}}function j(){var d=!u.state.isVisible,w=u.state.isDestroyed,R=!u.state.isEnabled,F=An(u.props.duration,1,xe.duration);if(!(d||w||R)&&(y("onHide",[u],!1),u.props.onHide(u)!==!1)){if(u.state.isVisible=!1,u.state.isShown=!1,f=!1,a=!1,z()&&(m.style.visibility="hidden"),W(),Z(),p(!0),z()){var B=Y(),oe=B.box,ge=B.content;u.props.animation&&(Sn([oe,ge],F),qr([oe,ge],"hidden"))}C(),N(),u.props.animation?z()&&Ce(F,u.unmount):u.unmount()}}function ne(d){U().addEventListener("mousemove",A),zr(qt,A),A(d)}function me(){u.state.isVisible&&u.hide(),u.state.isMounted&&(T(),V().forEach(function(d){d._tippy.unmount()}),m.parentNode&&m.parentNode.removeChild(m),Mn=Mn.filter(function(d){return d!==u}),u.state.isMounted=!1,y("onHidden",[u]))}function En(){u.state.isDestroyed||(u.clearDelayTimeouts(),u.unmount(),st(),delete e._tippy,u.state.isDestroyed=!0,y("onDestroy",[u]))}}function jt(e,t){t===void 0&&(t={});var n=xe.plugins.concat(t.plugins||[]);Ts();var r=Object.assign({},t,{plugins:n}),i=Es(e),o=i.reduce(function(a,s){var c=s&&Bs(s,r);return c&&a.push(c),a},[]);return dn(e)?o[0]:o}jt.defaultProps=xe;jt.setDefaultProps=Ns;jt.currentInput=Se;Object.assign({},Ei,{effect:function(t){var n=t.state,r={popper:{position:n.options.strategy,left:"0",top:"0",margin:"0"},arrow:{position:"absolute"},reference:{}};Object.assign(n.elements.popper.style,r.popper),n.styles=r,n.elements.arrow&&Object.assign(n.elements.arrow.style,r.arrow)}});jt.setDefaultProps({render:Bi});var jn=!1,Hn=!1,Qe=[],Fn=-1;function $s(e){js(e)}function js(e){Qe.includes(e)||Qe.push(e),Hs()}function $i(e){let t=Qe.indexOf(e);t!==-1&&t>Fn&&Qe.splice(t,1)}function Hs(){!Hn&&!jn&&(jn=!0,queueMicrotask(Fs))}function Fs(){jn=!1,Hn=!0;for(let e=0;e<Qe.length;e++)Qe[e](),Fn=e;Qe.length=0,Fn=-1,Hn=!1}var yt,ot,mt,ji,Un=!0;function Us(e){Un=!1,e(),Un=!0}function Ws(e){yt=e.reactive,mt=e.release,ot=t=>e.effect(t,{scheduler:n=>{Un?$s(n):n()}}),ji=e.raw}function Qr(e){ot=e}function Ks(e){let t=()=>{};return[r=>{let i=ot(r);return e._x_effects||(e._x_effects=new Set,e._x_runEffects=()=>{e._x_effects.forEach(o=>o())}),e._x_effects.add(i),t=()=>{i!==void 0&&(e._x_effects.delete(i),mt(i))},i},()=>{t()}]}function Hi(e,t){let n=!0,r,i=ot(()=>{let o=e();JSON.stringify(o),n?r=o:queueMicrotask(()=>{t(o,r),r=o}),n=!1});return()=>mt(i)}var Fi=[],Ui=[],Wi=[];function zs(e){Wi.push(e)}function lr(e,t){typeof t=="function"?(e._x_cleanups||(e._x_cleanups=[]),e._x_cleanups.push(t)):(t=e,Ui.push(t))}function Ki(e){Fi.push(e)}function zi(e,t,n){e._x_attributeCleanups||(e._x_attributeCleanups={}),e._x_attributeCleanups[t]||(e._x_attributeCleanups[t]=[]),e._x_attributeCleanups[t].push(n)}function Vi(e,t){e._x_attributeCleanups&&Object.entries(e._x_attributeCleanups).forEach(([n,r])=>{(t===void 0||t.includes(n))&&(r.forEach(i=>i()),delete e._x_attributeCleanups[n])})}function Vs(e){if(e._x_cleanups)for(;e._x_cleanups.length;)e._x_cleanups.pop()()}var fr=new MutationObserver(gr),dr=!1;function pr(){fr.observe(document,{subtree:!0,childList:!0,attributes:!0,attributeOldValue:!0}),dr=!0}function qi(){qs(),fr.disconnect(),dr=!1}var wt=[];function qs(){let e=fr.takeRecords();wt.push(()=>e.length>0&&gr(e));let t=wt.length;queueMicrotask(()=>{if(wt.length===t)for(;wt.length>0;)wt.shift()()})}function ee(e){if(!dr)return e();qi();let t=e();return pr(),t}var hr=!1,an=[];function Gs(){hr=!0}function Xs(){hr=!1,gr(an),an=[]}function gr(e){if(hr){an=an.concat(e);return}let t=new Set,n=new Set,r=new Map,i=new Map;for(let o=0;o<e.length;o++)if(!e[o].target._x_ignoreMutationObserver&&(e[o].type==="childList"&&(e[o].addedNodes.forEach(a=>a.nodeType===1&&t.add(a)),e[o].removedNodes.forEach(a=>a.nodeType===1&&n.add(a))),e[o].type==="attributes")){let a=e[o].target,s=e[o].attributeName,c=e[o].oldValue,f=()=>{r.has(a)||r.set(a,[]),r.get(a).push({name:s,value:a.getAttribute(s)})},l=()=>{i.has(a)||i.set(a,[]),i.get(a).push(s)};a.hasAttribute(s)&&c===null?f():a.hasAttribute(s)?(l(),f()):l()}i.forEach((o,a)=>{Vi(a,o)}),r.forEach((o,a)=>{Fi.forEach(s=>s(a,o))});for(let o of n)t.has(o)||Ui.forEach(a=>a(o));t.forEach(o=>{o._x_ignoreSelf=!0,o._x_ignore=!0});for(let o of t)n.has(o)||o.isConnected&&(delete o._x_ignoreSelf,delete o._x_ignore,Wi.forEach(a=>a(o)),o._x_ignore=!0,o._x_ignoreSelf=!0);t.forEach(o=>{delete o._x_ignoreSelf,delete o._x_ignore}),t=null,n=null,r=null,i=null}function Gi(e){return Ft(vt(e))}function Ht(e,t,n){return e._x_dataStack=[t,...vt(n||e)],()=>{e._x_dataStack=e._x_dataStack.filter(r=>r!==t)}}function vt(e){return e._x_dataStack?e._x_dataStack:typeof ShadowRoot=="function"&&e instanceof ShadowRoot?vt(e.host):e.parentNode?vt(e.parentNode):[]}function Ft(e){return new Proxy({objects:e},Ys)}var Ys={ownKeys({objects:e}){return Array.from(new Set(e.flatMap(t=>Object.keys(t))))},has({objects:e},t){return t==Symbol.unscopables?!1:e.some(n=>Object.prototype.hasOwnProperty.call(n,t)||Reflect.has(n,t))},get({objects:e},t,n){return t=="toJSON"?Js:Reflect.get(e.find(r=>Reflect.has(r,t))||{},t,n)},set({objects:e},t,n,r){const i=e.find(a=>Object.prototype.hasOwnProperty.call(a,t))||e[e.length-1],o=Object.getOwnPropertyDescriptor(i,t);return o!=null&&o.set&&(o!=null&&o.get)?Reflect.set(i,t,n,r):Reflect.set(i,t,n)}};function Js(){return Reflect.ownKeys(this).reduce((t,n)=>(t[n]=Reflect.get(this,n),t),{})}function Xi(e){let t=r=>typeof r=="object"&&!Array.isArray(r)&&r!==null,n=(r,i="")=>{Object.entries(Object.getOwnPropertyDescriptors(r)).forEach(([o,{value:a,enumerable:s}])=>{if(s===!1||a===void 0||typeof a=="object"&&a!==null&&a.__v_skip)return;let c=i===""?o:`${i}.${o}`;typeof a=="object"&&a!==null&&a._x_interceptor?r[o]=a.initialize(e,c,o):t(a)&&a!==r&&!(a instanceof Element)&&n(a,c)})};return n(e)}function Yi(e,t=()=>{}){let n={initialValue:void 0,_x_interceptor:!0,initialize(r,i,o){return e(this.initialValue,()=>Zs(r,i),a=>Wn(r,i,a),i,o)}};return t(n),r=>{if(typeof r=="object"&&r!==null&&r._x_interceptor){let i=n.initialize.bind(n);n.initialize=(o,a,s)=>{let c=r.initialize(o,a,s);return n.initialValue=c,i(o,a,s)}}else n.initialValue=r;return n}}function Zs(e,t){return t.split(".").reduce((n,r)=>n[r],e)}function Wn(e,t,n){if(typeof t=="string"&&(t=t.split(".")),t.length===1)e[t[0]]=n;else{if(t.length===0)throw error;return e[t[0]]||(e[t[0]]={}),Wn(e[t[0]],t.slice(1),n)}}var Ji={};function we(e,t){Ji[e]=t}function Kn(e,t){return Object.entries(Ji).forEach(([n,r])=>{let i=null;function o(){if(i)return i;{let[a,s]=ro(t);return i={interceptor:Yi,...a},lr(t,s),i}}Object.defineProperty(e,`$${n}`,{get(){return r(t,o())},enumerable:!1})}),e}function Qs(e,t,n,...r){try{return n(...r)}catch(i){Lt(i,e,t)}}function Lt(e,t,n=void 0){e=Object.assign(e??{message:"No error message given."},{el:t,expression:n}),console.warn(`Alpine Expression Error: ${e.message} ${n?'Expression: "'+n+`" `:""}`,t),setTimeout(()=>{throw e},0)}var tn=!0;function Zi(e){let t=tn;tn=!1;let n=e();return tn=t,n}function et(e,t,n={}){let r;return ae(e,t)(i=>r=i,n),r}function ae(...e){return Qi(...e)}var Qi=eo;function ec(e){Qi=e}function eo(e,t){let n={};Kn(n,e);let r=[n,...vt(e)],i=typeof t=="function"?tc(r,t):rc(r,t,e);return Qs.bind(null,e,t,i)}function tc(e,t){return(n=()=>{},{scope:r={},params:i=[]}={})=>{let o=t.apply(Ft([r,...e]),i);sn(n,o)}}var Cn={};function nc(e,t){if(Cn[e])return Cn[e];let n=Object.getPrototypeOf(async function(){}).constructor,r=/^[\n\s]*if.*\(.*\)/.test(e.trim())||/^(let|const)\s/.test(e.trim())?`(async()=>{ ${e} })()`:e,o=(()=>{try{let a=new n(["__self","scope"],`with (scope) { __self.result = ${r} }; __self.finished = true; return __self.result;`);return Object.defineProperty(a,"name",{value:`[Alpine] ${e}`}),a}catch(a){return Lt(a,t,e),Promise.resolve()}})();return Cn[e]=o,o}function rc(e,t,n){let r=nc(t,n);return(i=()=>{},{scope:o={},params:a=[]}={})=>{r.result=void 0,r.finished=!1;let s=Ft([o,...e]);if(typeof r=="function"){let c=r(r,s).catch(f=>Lt(f,n,t));r.finished?(sn(i,r.result,s,a,n),r.result=void 0):c.then(f=>{sn(i,f,s,a,n)}).catch(f=>Lt(f,n,t)).finally(()=>r.result=void 0)}}}function sn(e,t,n,r,i){if(tn&&typeof t=="function"){let o=t.apply(n,r);o instanceof Promise?o.then(a=>sn(e,a,n,r)).catch(a=>Lt(a,i,t)):e(o)}else typeof t=="object"&&t instanceof Promise?t.then(o=>e(o)):e(t)}var vr="x-";function xt(e=""){return vr+e}function ic(e){vr=e}var cn={};function Q(e,t){return cn[e]=t,{before(n){if(!cn[n]){console.warn(String.raw`Cannot find directive \`${n}\`. \`${e}\` will use the default order of execution`);return}const r=Je.indexOf(n);Je.splice(r>=0?r:Je.indexOf("DEFAULT"),0,e)}}}function oc(e){return Object.keys(cn).includes(e)}function _r(e,t,n){if(t=Array.from(t),e._x_virtualDirectives){let o=Object.entries(e._x_virtualDirectives).map(([s,c])=>({name:s,value:c})),a=to(o);o=o.map(s=>a.find(c=>c.name===s.name)?{name:`x-bind:${s.name}`,value:`"${s.value}"`}:s),t=t.concat(o)}let r={};return t.map(ao((o,a)=>r[o]=a)).filter(co).map(cc(r,n)).sort(uc).map(o=>sc(e,o))}function to(e){return Array.from(e).map(ao()).filter(t=>!co(t))}var zn=!1,St=new Map,no=Symbol();function ac(e){zn=!0;let t=Symbol();no=t,St.set(t,[]);let n=()=>{for(;St.get(t).length;)St.get(t).shift()();St.delete(t)},r=()=>{zn=!1,n()};e(n),r()}function ro(e){let t=[],n=s=>t.push(s),[r,i]=Ks(e);return t.push(i),[{Alpine:Wt,effect:r,cleanup:n,evaluateLater:ae.bind(ae,e),evaluate:et.bind(et,e)},()=>t.forEach(s=>s())]}function sc(e,t){let n=()=>{},r=cn[t.type]||n,[i,o]=ro(e);zi(e,t.original,o);let a=()=>{e._x_ignore||e._x_ignoreSelf||(r.inline&&r.inline(e,t,i),r=r.bind(r,e,t,i),zn?St.get(no).push(r):r())};return a.runCleanups=o,a}var io=(e,t)=>({name:n,value:r})=>(n.startsWith(e)&&(n=n.replace(e,t)),{name:n,value:r}),oo=e=>e;function ao(e=()=>{}){return({name:t,value:n})=>{let{name:r,value:i}=so.reduce((o,a)=>a(o),{name:t,value:n});return r!==t&&e(r,t),{name:r,value:i}}}var so=[];function br(e){so.push(e)}function co({name:e}){return uo().test(e)}var uo=()=>new RegExp(`^${vr}([^:^.]+)\\b`);function cc(e,t){return({name:n,value:r})=>{let i=n.match(uo()),o=n.match(/:([a-zA-Z0-9\-_:]+)/),a=n.match(/\.[^.\]]+(?=[^\]]*$)/g)||[],s=t||e[n]||n;return{type:i?i[1]:null,value:o?o[1]:null,modifiers:a.map(c=>c.replace(".","")),expression:r,original:s}}}var Vn="DEFAULT",Je=["ignore","ref","data","id","anchor","bind","init","for","model","modelable","transition","show","if",Vn,"teleport"];function uc(e,t){let n=Je.indexOf(e.type)===-1?Vn:e.type,r=Je.indexOf(t.type)===-1?Vn:t.type;return Je.indexOf(n)-Je.indexOf(r)}function Rt(e,t,n={}){e.dispatchEvent(new CustomEvent(t,{detail:n,bubbles:!0,composed:!0,cancelable:!0}))}function He(e,t){if(typeof ShadowRoot=="function"&&e instanceof ShadowRoot){Array.from(e.children).forEach(i=>He(i,t));return}let n=!1;if(t(e,()=>n=!0),n)return;let r=e.firstElementChild;for(;r;)He(r,t),r=r.nextElementSibling}function ve(e,...t){console.warn(`Alpine Warning: ${e}`,...t)}var ei=!1;function lc(){ei&&ve("Alpine has already been initialized on this page. Calling Alpine.start() more than once can cause problems."),ei=!0,document.body||ve("Unable to initialize. Trying to load Alpine before `<body>` is available. Did you forget to add `defer` in Alpine's `<script>` tag?"),Rt(document,"alpine:init"),Rt(document,"alpine:initializing"),pr(),zs(t=>Ne(t,He)),lr(t=>_o(t)),Ki((t,n)=>{_r(t,n).forEach(r=>r())});let e=t=>!pn(t.parentElement,!0);Array.from(document.querySelectorAll(po().join(","))).filter(e).forEach(t=>{Ne(t)}),Rt(document,"alpine:initialized"),setTimeout(()=>{pc()})}var yr=[],lo=[];function fo(){return yr.map(e=>e())}function po(){return yr.concat(lo).map(e=>e())}function ho(e){yr.push(e)}function go(e){lo.push(e)}function pn(e,t=!1){return Ut(e,n=>{if((t?po():fo()).some(i=>n.matches(i)))return!0})}function Ut(e,t){if(e){if(t(e))return e;if(e._x_teleportBack&&(e=e._x_teleportBack),!!e.parentElement)return Ut(e.parentElement,t)}}function fc(e){return fo().some(t=>e.matches(t))}var vo=[];function dc(e){vo.push(e)}function Ne(e,t=He,n=()=>{}){ac(()=>{t(e,(r,i)=>{n(r,i),vo.forEach(o=>o(r,i)),_r(r,r.attributes).forEach(o=>o()),r._x_ignore&&i()})})}function _o(e,t=He){t(e,n=>{Vi(n),Vs(n)})}function pc(){[["ui","dialog",["[x-dialog], [x-popover]"]],["anchor","anchor",["[x-anchor]"]],["sort","sort",["[x-sort]"]]].forEach(([t,n,r])=>{oc(n)||r.some(i=>{if(document.querySelector(i))return ve(`found "${i}", but missing ${t} plugin`),!0})})}var qn=[],mr=!1;function xr(e=()=>{}){return queueMicrotask(()=>{mr||setTimeout(()=>{Gn()})}),new Promise(t=>{qn.push(()=>{e(),t()})})}function Gn(){for(mr=!1;qn.length;)qn.shift()()}function hc(){mr=!0}function Er(e,t){return Array.isArray(t)?ti(e,t.join(" ")):typeof t=="object"&&t!==null?gc(e,t):typeof t=="function"?Er(e,t()):ti(e,t)}function ti(e,t){let n=i=>i.split(" ").filter(o=>!e.classList.contains(o)).filter(Boolean),r=i=>(e.classList.add(...i),()=>{e.classList.remove(...i)});return t=t===!0?t="":t||"",r(n(t))}function gc(e,t){let n=s=>s.split(" ").filter(Boolean),r=Object.entries(t).flatMap(([s,c])=>c?n(s):!1).filter(Boolean),i=Object.entries(t).flatMap(([s,c])=>c?!1:n(s)).filter(Boolean),o=[],a=[];return i.forEach(s=>{e.classList.contains(s)&&(e.classList.remove(s),a.push(s))}),r.forEach(s=>{e.classList.contains(s)||(e.classList.add(s),o.push(s))}),()=>{a.forEach(s=>e.classList.add(s)),o.forEach(s=>e.classList.remove(s))}}function hn(e,t){return typeof t=="object"&&t!==null?vc(e,t):_c(e,t)}function vc(e,t){let n={};return Object.entries(t).forEach(([r,i])=>{n[r]=e.style[r],r.startsWith("--")||(r=bc(r)),e.style.setProperty(r,i)}),setTimeout(()=>{e.style.length===0&&e.removeAttribute("style")}),()=>{hn(e,n)}}function _c(e,t){let n=e.getAttribute("style",t);return e.setAttribute("style",t),()=>{e.setAttribute("style",n||"")}}function bc(e){return e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}function Xn(e,t=()=>{}){let n=!1;return function(){n?t.apply(this,arguments):(n=!0,e.apply(this,arguments))}}Q("transition",(e,{value:t,modifiers:n,expression:r},{evaluate:i})=>{typeof r=="function"&&(r=i(r)),r!==!1&&(!r||typeof r=="boolean"?mc(e,n,t):yc(e,r,t))});function yc(e,t,n){bo(e,Er,""),{enter:i=>{e._x_transition.enter.during=i},"enter-start":i=>{e._x_transition.enter.start=i},"enter-end":i=>{e._x_transition.enter.end=i},leave:i=>{e._x_transition.leave.during=i},"leave-start":i=>{e._x_transition.leave.start=i},"leave-end":i=>{e._x_transition.leave.end=i}}[n](t)}function mc(e,t,n){bo(e,hn);let r=!t.includes("in")&&!t.includes("out")&&!n,i=r||t.includes("in")||["enter"].includes(n),o=r||t.includes("out")||["leave"].includes(n);t.includes("in")&&!r&&(t=t.filter((E,O)=>O<t.indexOf("out"))),t.includes("out")&&!r&&(t=t.filter((E,O)=>O>t.indexOf("out")));let a=!t.includes("opacity")&&!t.includes("scale"),s=a||t.includes("opacity"),c=a||t.includes("scale"),f=s?0:1,l=c?Ot(t,"scale",95)/100:1,v=Ot(t,"delay",0)/1e3,b=Ot(t,"origin","center"),_="opacity, transform",A=Ot(t,"duration",150)/1e3,S=Ot(t,"duration",75)/1e3,h="cubic-bezier(0.4, 0.0, 0.2, 1)";i&&(e._x_transition.enter.during={transformOrigin:b,transitionDelay:`${v}s`,transitionProperty:_,transitionDuration:`${A}s`,transitionTimingFunction:h},e._x_transition.enter.start={opacity:f,transform:`scale(${l})`},e._x_transition.enter.end={opacity:1,transform:"scale(1)"}),o&&(e._x_transition.leave.during={transformOrigin:b,transitionDelay:`${v}s`,transitionProperty:_,transitionDuration:`${S}s`,transitionTimingFunction:h},e._x_transition.leave.start={opacity:1,transform:"scale(1)"},e._x_transition.leave.end={opacity:f,transform:`scale(${l})`})}function bo(e,t,n={}){e._x_transition||(e._x_transition={enter:{during:n,start:n,end:n},leave:{during:n,start:n,end:n},in(r=()=>{},i=()=>{}){Yn(e,t,{during:this.enter.during,start:this.enter.start,end:this.enter.end},r,i)},out(r=()=>{},i=()=>{}){Yn(e,t,{during:this.leave.during,start:this.leave.start,end:this.leave.end},r,i)}})}window.Element.prototype._x_toggleAndCascadeWithTransitions=function(e,t,n,r){const i=document.visibilityState==="visible"?requestAnimationFrame:setTimeout;let o=()=>i(n);if(t){e._x_transition&&(e._x_transition.enter||e._x_transition.leave)?e._x_transition.enter&&(Object.entries(e._x_transition.enter.during).length||Object.entries(e._x_transition.enter.start).length||Object.entries(e._x_transition.enter.end).length)?e._x_transition.in(n):o():e._x_transition?e._x_transition.in(n):o();return}e._x_hidePromise=e._x_transition?new Promise((a,s)=>{e._x_transition.out(()=>{},()=>a(r)),e._x_transitioning&&e._x_transitioning.beforeCancel(()=>s({isFromCancelledTransition:!0}))}):Promise.resolve(r),queueMicrotask(()=>{let a=yo(e);a?(a._x_hideChildren||(a._x_hideChildren=[]),a._x_hideChildren.push(e)):i(()=>{let s=c=>{let f=Promise.all([c._x_hidePromise,...(c._x_hideChildren||[]).map(s)]).then(([l])=>l());return delete c._x_hidePromise,delete c._x_hideChildren,f};s(e).catch(c=>{if(!c.isFromCancelledTransition)throw c})})})};function yo(e){let t=e.parentNode;if(t)return t._x_hidePromise?t:yo(t)}function Yn(e,t,{during:n,start:r,end:i}={},o=()=>{},a=()=>{}){if(e._x_transitioning&&e._x_transitioning.cancel(),Object.keys(n).length===0&&Object.keys(r).length===0&&Object.keys(i).length===0){o(),a();return}let s,c,f;xc(e,{start(){s=t(e,r)},during(){c=t(e,n)},before:o,end(){s(),f=t(e,i)},after:a,cleanup(){c(),f()}})}function xc(e,t){let n,r,i,o=Xn(()=>{ee(()=>{n=!0,r||t.before(),i||(t.end(),Gn()),t.after(),e.isConnected&&t.cleanup(),delete e._x_transitioning})});e._x_transitioning={beforeCancels:[],beforeCancel(a){this.beforeCancels.push(a)},cancel:Xn(function(){for(;this.beforeCancels.length;)this.beforeCancels.shift()();o()}),finish:o},ee(()=>{t.start(),t.during()}),hc(),requestAnimationFrame(()=>{if(n)return;let a=Number(getComputedStyle(e).transitionDuration.replace(/,.*/,"").replace("s",""))*1e3,s=Number(getComputedStyle(e).transitionDelay.replace(/,.*/,"").replace("s",""))*1e3;a===0&&(a=Number(getComputedStyle(e).animationDuration.replace("s",""))*1e3),ee(()=>{t.before()}),r=!0,requestAnimationFrame(()=>{n||(ee(()=>{t.end()}),Gn(),setTimeout(e._x_transitioning.finish,a+s),i=!0)})})}function Ot(e,t,n){if(e.indexOf(t)===-1)return n;const r=e[e.indexOf(t)+1];if(!r||t==="scale"&&isNaN(r))return n;if(t==="duration"||t==="delay"){let i=r.match(/([0-9]+)ms/);if(i)return i[1]}return t==="origin"&&["top","right","left","center","bottom"].includes(e[e.indexOf(t)+2])?[r,e[e.indexOf(t)+2]].join(" "):r}var Fe=!1;function Ke(e,t=()=>{}){return(...n)=>Fe?t(...n):e(...n)}function Ec(e){return(...t)=>Fe&&e(...t)}var mo=[];function gn(e){mo.push(e)}function wc(e,t){mo.forEach(n=>n(e,t)),Fe=!0,xo(()=>{Ne(t,(n,r)=>{r(n,()=>{})})}),Fe=!1}var Jn=!1;function Oc(e,t){t._x_dataStack||(t._x_dataStack=e._x_dataStack),Fe=!0,Jn=!0,xo(()=>{Ac(t)}),Fe=!1,Jn=!1}function Ac(e){let t=!1;Ne(e,(r,i)=>{He(r,(o,a)=>{if(t&&fc(o))return a();t=!0,i(o,a)})})}function xo(e){let t=ot;Qr((n,r)=>{let i=t(n);return mt(i),()=>{}}),e(),Qr(t)}function Eo(e,t,n,r=[]){switch(e._x_bindings||(e._x_bindings=yt({})),e._x_bindings[t]=n,t=r.includes("camel")?Nc(t):t,t){case"value":Sc(e,n);break;case"style":Mc(e,n);break;case"class":Tc(e,n);break;case"selected":case"checked":Cc(e,t,n);break;default:wo(e,t,n);break}}function Sc(e,t){if(e.type==="radio")e.attributes.value===void 0&&(e.value=t),window.fromModel&&(typeof t=="boolean"?e.checked=nn(e.value)===t:e.checked=ni(e.value,t));else if(e.type==="checkbox")Number.isInteger(t)?e.value=t:!Array.isArray(t)&&typeof t!="boolean"&&![null,void 0].includes(t)?e.value=String(t):Array.isArray(t)?e.checked=t.some(n=>ni(n,e.value)):e.checked=!!t;else if(e.tagName==="SELECT")Dc(e,t);else{if(e.value===t)return;e.value=t===void 0?"":t}}function Tc(e,t){e._x_undoAddedClasses&&e._x_undoAddedClasses(),e._x_undoAddedClasses=Er(e,t)}function Mc(e,t){e._x_undoAddedStyles&&e._x_undoAddedStyles(),e._x_undoAddedStyles=hn(e,t)}function Cc(e,t,n){wo(e,t,n),Ic(e,t,n)}function wo(e,t,n){[null,void 0,!1].includes(n)&&Lc(t)?e.removeAttribute(t):(Oo(t)&&(n=t),Rc(e,t,n))}function Rc(e,t,n){e.getAttribute(t)!=n&&e.setAttribute(t,n)}function Ic(e,t,n){e[t]!==n&&(e[t]=n)}function Dc(e,t){const n=[].concat(t).map(r=>r+"");Array.from(e.options).forEach(r=>{r.selected=n.includes(r.value)})}function Nc(e){return e.toLowerCase().replace(/-(\w)/g,(t,n)=>n.toUpperCase())}function ni(e,t){return e==t}function nn(e){return[1,"1","true","on","yes",!0].includes(e)?!0:[0,"0","false","off","no",!1].includes(e)?!1:e?!!e:null}function Oo(e){return["disabled","checked","required","readonly","open","selected","autofocus","itemscope","multiple","novalidate","allowfullscreen","allowpaymentrequest","formnovalidate","autoplay","controls","loop","muted","playsinline","default","ismap","reversed","async","defer","nomodule"].includes(e)}function Lc(e){return!["aria-pressed","aria-checked","aria-expanded","aria-selected"].includes(e)}function Pc(e,t,n){return e._x_bindings&&e._x_bindings[t]!==void 0?e._x_bindings[t]:Ao(e,t,n)}function kc(e,t,n,r=!0){if(e._x_bindings&&e._x_bindings[t]!==void 0)return e._x_bindings[t];if(e._x_inlineBindings&&e._x_inlineBindings[t]!==void 0){let i=e._x_inlineBindings[t];return i.extract=r,Zi(()=>et(e,i.expression))}return Ao(e,t,n)}function Ao(e,t,n){let r=e.getAttribute(t);return r===null?typeof n=="function"?n():n:r===""?!0:Oo(t)?!![t,"true"].includes(r):r}function So(e,t){var n;return function(){var r=this,i=arguments,o=function(){n=null,e.apply(r,i)};clearTimeout(n),n=setTimeout(o,t)}}function To(e,t){let n;return function(){let r=this,i=arguments;n||(e.apply(r,i),n=!0,setTimeout(()=>n=!1,t))}}function Mo({get:e,set:t},{get:n,set:r}){let i=!0,o,a=ot(()=>{let s=e(),c=n();if(i)r(Rn(s)),i=!1;else{let f=JSON.stringify(s),l=JSON.stringify(c);f!==o?r(Rn(s)):f!==l&&t(Rn(c))}o=JSON.stringify(e()),JSON.stringify(n())});return()=>{mt(a)}}function Rn(e){return typeof e=="object"?JSON.parse(JSON.stringify(e)):e}function Bc(e){(Array.isArray(e)?e:[e]).forEach(n=>n(Wt))}var Ye={},ri=!1;function $c(e,t){if(ri||(Ye=yt(Ye),ri=!0),t===void 0)return Ye[e];Ye[e]=t,typeof t=="object"&&t!==null&&t.hasOwnProperty("init")&&typeof t.init=="function"&&Ye[e].init(),Xi(Ye[e])}function jc(){return Ye}var Co={};function Hc(e,t){let n=typeof t!="function"?()=>t:t;return e instanceof Element?Ro(e,n()):(Co[e]=n,()=>{})}function Fc(e){return Object.entries(Co).forEach(([t,n])=>{Object.defineProperty(e,t,{get(){return(...r)=>n(...r)}})}),e}function Ro(e,t,n){let r=[];for(;r.length;)r.pop()();let i=Object.entries(t).map(([a,s])=>({name:a,value:s})),o=to(i);return i=i.map(a=>o.find(s=>s.name===a.name)?{name:`x-bind:${a.name}`,value:`"${a.value}"`}:a),_r(e,i,n).map(a=>{r.push(a.runCleanups),a()}),()=>{for(;r.length;)r.pop()()}}var Io={};function Uc(e,t){Io[e]=t}function Wc(e,t){return Object.entries(Io).forEach(([n,r])=>{Object.defineProperty(e,n,{get(){return(...i)=>r.bind(t)(...i)},enumerable:!1})}),e}var Kc={get reactive(){return yt},get release(){return mt},get effect(){return ot},get raw(){return ji},version:"3.13.10",flushAndStopDeferringMutations:Xs,dontAutoEvaluateFunctions:Zi,disableEffectScheduling:Us,startObservingMutations:pr,stopObservingMutations:qi,setReactivityEngine:Ws,onAttributeRemoved:zi,onAttributesAdded:Ki,closestDataStack:vt,skipDuringClone:Ke,onlyDuringClone:Ec,addRootSelector:ho,addInitSelector:go,interceptClone:gn,addScopeToNode:Ht,deferMutations:Gs,mapAttributes:br,evaluateLater:ae,interceptInit:dc,setEvaluator:ec,mergeProxies:Ft,extractProp:kc,findClosest:Ut,onElRemoved:lr,closestRoot:pn,destroyTree:_o,interceptor:Yi,transition:Yn,setStyles:hn,mutateDom:ee,directive:Q,entangle:Mo,throttle:To,debounce:So,evaluate:et,initTree:Ne,nextTick:xr,prefixed:xt,prefix:ic,plugin:Bc,magic:we,store:$c,start:lc,clone:Oc,cloneNode:wc,bound:Pc,$data:Gi,watch:Hi,walk:He,data:Uc,bind:Hc},Wt=Kc;function zc(e,t){const n=Object.create(null),r=e.split(",");for(let i=0;i<r.length;i++)n[r[i]]=!0;return i=>!!n[i]}var Vc=Object.freeze({}),qc=Object.prototype.hasOwnProperty,vn=(e,t)=>qc.call(e,t),tt=Array.isArray,It=e=>Do(e)==="[object Map]",Gc=e=>typeof e=="string",wr=e=>typeof e=="symbol",_n=e=>e!==null&&typeof e=="object",Xc=Object.prototype.toString,Do=e=>Xc.call(e),No=e=>Do(e).slice(8,-1),Or=e=>Gc(e)&&e!=="NaN"&&e[0]!=="-"&&""+parseInt(e,10)===e,Yc=e=>{const t=Object.create(null);return n=>t[n]||(t[n]=e(n))},Jc=Yc(e=>e.charAt(0).toUpperCase()+e.slice(1)),Lo=(e,t)=>e!==t&&(e===e||t===t),Zn=new WeakMap,At=[],Ae,nt=Symbol("iterate"),Qn=Symbol("Map key iterate");function Zc(e){return e&&e._isEffect===!0}function Qc(e,t=Vc){Zc(e)&&(e=e.raw);const n=nu(e,t);return t.lazy||n(),n}function eu(e){e.active&&(Po(e),e.options.onStop&&e.options.onStop(),e.active=!1)}var tu=0;function nu(e,t){const n=function(){if(!n.active)return e();if(!At.includes(n)){Po(n);try{return iu(),At.push(n),Ae=n,e()}finally{At.pop(),ko(),Ae=At[At.length-1]}}};return n.id=tu++,n.allowRecurse=!!t.allowRecurse,n._isEffect=!0,n.active=!0,n.raw=e,n.deps=[],n.options=t,n}function Po(e){const{deps:t}=e;if(t.length){for(let n=0;n<t.length;n++)t[n].delete(e);t.length=0}}var _t=!0,Ar=[];function ru(){Ar.push(_t),_t=!1}function iu(){Ar.push(_t),_t=!0}function ko(){const e=Ar.pop();_t=e===void 0?!0:e}function Ee(e,t,n){if(!_t||Ae===void 0)return;let r=Zn.get(e);r||Zn.set(e,r=new Map);let i=r.get(n);i||r.set(n,i=new Set),i.has(Ae)||(i.add(Ae),Ae.deps.push(i),Ae.options.onTrack&&Ae.options.onTrack({effect:Ae,target:e,type:t,key:n}))}function Ue(e,t,n,r,i,o){const a=Zn.get(e);if(!a)return;const s=new Set,c=l=>{l&&l.forEach(v=>{(v!==Ae||v.allowRecurse)&&s.add(v)})};if(t==="clear")a.forEach(c);else if(n==="length"&&tt(e))a.forEach((l,v)=>{(v==="length"||v>=r)&&c(l)});else switch(n!==void 0&&c(a.get(n)),t){case"add":tt(e)?Or(n)&&c(a.get("length")):(c(a.get(nt)),It(e)&&c(a.get(Qn)));break;case"delete":tt(e)||(c(a.get(nt)),It(e)&&c(a.get(Qn)));break;case"set":It(e)&&c(a.get(nt));break}const f=l=>{l.options.onTrigger&&l.options.onTrigger({effect:l,target:e,key:n,type:t,newValue:r,oldValue:i,oldTarget:o}),l.options.scheduler?l.options.scheduler(l):l()};s.forEach(f)}var ou=zc("__proto__,__v_isRef,__isVue"),Bo=new Set(Object.getOwnPropertyNames(Symbol).map(e=>Symbol[e]).filter(wr)),au=$o(),su=$o(!0),ii=cu();function cu(){const e={};return["includes","indexOf","lastIndexOf"].forEach(t=>{e[t]=function(...n){const r=X(this);for(let o=0,a=this.length;o<a;o++)Ee(r,"get",o+"");const i=r[t](...n);return i===-1||i===!1?r[t](...n.map(X)):i}}),["push","pop","shift","unshift","splice"].forEach(t=>{e[t]=function(...n){ru();const r=X(this)[t].apply(this,n);return ko(),r}}),e}function $o(e=!1,t=!1){return function(r,i,o){if(i==="__v_isReactive")return!e;if(i==="__v_isReadonly")return e;if(i==="__v_raw"&&o===(e?t?Eu:Uo:t?xu:Fo).get(r))return r;const a=tt(r);if(!e&&a&&vn(ii,i))return Reflect.get(ii,i,o);const s=Reflect.get(r,i,o);return(wr(i)?Bo.has(i):ou(i))||(e||Ee(r,"get",i),t)?s:er(s)?!a||!Or(i)?s.value:s:_n(s)?e?Wo(s):Cr(s):s}}var uu=lu();function lu(e=!1){return function(n,r,i,o){let a=n[r];if(!e&&(i=X(i),a=X(a),!tt(n)&&er(a)&&!er(i)))return a.value=i,!0;const s=tt(n)&&Or(r)?Number(r)<n.length:vn(n,r),c=Reflect.set(n,r,i,o);return n===X(o)&&(s?Lo(i,a)&&Ue(n,"set",r,i,a):Ue(n,"add",r,i)),c}}function fu(e,t){const n=vn(e,t),r=e[t],i=Reflect.deleteProperty(e,t);return i&&n&&Ue(e,"delete",t,void 0,r),i}function du(e,t){const n=Reflect.has(e,t);return(!wr(t)||!Bo.has(t))&&Ee(e,"has",t),n}function pu(e){return Ee(e,"iterate",tt(e)?"length":nt),Reflect.ownKeys(e)}var hu={get:au,set:uu,deleteProperty:fu,has:du,ownKeys:pu},gu={get:su,set(e,t){return console.warn(`Set operation on key "${String(t)}" failed: target is readonly.`,e),!0},deleteProperty(e,t){return console.warn(`Delete operation on key "${String(t)}" failed: target is readonly.`,e),!0}},Sr=e=>_n(e)?Cr(e):e,Tr=e=>_n(e)?Wo(e):e,Mr=e=>e,bn=e=>Reflect.getPrototypeOf(e);function Gt(e,t,n=!1,r=!1){e=e.__v_raw;const i=X(e),o=X(t);t!==o&&!n&&Ee(i,"get",t),!n&&Ee(i,"get",o);const{has:a}=bn(i),s=r?Mr:n?Tr:Sr;if(a.call(i,t))return s(e.get(t));if(a.call(i,o))return s(e.get(o));e!==i&&e.get(t)}function Xt(e,t=!1){const n=this.__v_raw,r=X(n),i=X(e);return e!==i&&!t&&Ee(r,"has",e),!t&&Ee(r,"has",i),e===i?n.has(e):n.has(e)||n.has(i)}function Yt(e,t=!1){return e=e.__v_raw,!t&&Ee(X(e),"iterate",nt),Reflect.get(e,"size",e)}function oi(e){e=X(e);const t=X(this);return bn(t).has.call(t,e)||(t.add(e),Ue(t,"add",e,e)),this}function ai(e,t){t=X(t);const n=X(this),{has:r,get:i}=bn(n);let o=r.call(n,e);o?Ho(n,r,e):(e=X(e),o=r.call(n,e));const a=i.call(n,e);return n.set(e,t),o?Lo(t,a)&&Ue(n,"set",e,t,a):Ue(n,"add",e,t),this}function si(e){const t=X(this),{has:n,get:r}=bn(t);let i=n.call(t,e);i?Ho(t,n,e):(e=X(e),i=n.call(t,e));const o=r?r.call(t,e):void 0,a=t.delete(e);return i&&Ue(t,"delete",e,void 0,o),a}function ci(){const e=X(this),t=e.size!==0,n=It(e)?new Map(e):new Set(e),r=e.clear();return t&&Ue(e,"clear",void 0,void 0,n),r}function Jt(e,t){return function(r,i){const o=this,a=o.__v_raw,s=X(a),c=t?Mr:e?Tr:Sr;return!e&&Ee(s,"iterate",nt),a.forEach((f,l)=>r.call(i,c(f),c(l),o))}}function Zt(e,t,n){return function(...r){const i=this.__v_raw,o=X(i),a=It(o),s=e==="entries"||e===Symbol.iterator&&a,c=e==="keys"&&a,f=i[e](...r),l=n?Mr:t?Tr:Sr;return!t&&Ee(o,"iterate",c?Qn:nt),{next(){const{value:v,done:b}=f.next();return b?{value:v,done:b}:{value:s?[l(v[0]),l(v[1])]:l(v),done:b}},[Symbol.iterator](){return this}}}}function $e(e){return function(...t){{const n=t[0]?`on key "${t[0]}" `:"";console.warn(`${Jc(e)} operation ${n}failed: target is readonly.`,X(this))}return e==="delete"?!1:this}}function vu(){const e={get(o){return Gt(this,o)},get size(){return Yt(this)},has:Xt,add:oi,set:ai,delete:si,clear:ci,forEach:Jt(!1,!1)},t={get(o){return Gt(this,o,!1,!0)},get size(){return Yt(this)},has:Xt,add:oi,set:ai,delete:si,clear:ci,forEach:Jt(!1,!0)},n={get(o){return Gt(this,o,!0)},get size(){return Yt(this,!0)},has(o){return Xt.call(this,o,!0)},add:$e("add"),set:$e("set"),delete:$e("delete"),clear:$e("clear"),forEach:Jt(!0,!1)},r={get(o){return Gt(this,o,!0,!0)},get size(){return Yt(this,!0)},has(o){return Xt.call(this,o,!0)},add:$e("add"),set:$e("set"),delete:$e("delete"),clear:$e("clear"),forEach:Jt(!0,!0)};return["keys","values","entries",Symbol.iterator].forEach(o=>{e[o]=Zt(o,!1,!1),n[o]=Zt(o,!0,!1),t[o]=Zt(o,!1,!0),r[o]=Zt(o,!0,!0)}),[e,n,t,r]}var[_u,bu,jl,Hl]=vu();function jo(e,t){const n=e?bu:_u;return(r,i,o)=>i==="__v_isReactive"?!e:i==="__v_isReadonly"?e:i==="__v_raw"?r:Reflect.get(vn(n,i)&&i in r?n:r,i,o)}var yu={get:jo(!1)},mu={get:jo(!0)};function Ho(e,t,n){const r=X(n);if(r!==n&&t.call(e,r)){const i=No(e);console.warn(`Reactive ${i} contains both the raw and reactive versions of the same object${i==="Map"?" as keys":""}, which can lead to inconsistencies. Avoid differentiating between the raw and reactive versions of an object and only use the reactive version if possible.`)}}var Fo=new WeakMap,xu=new WeakMap,Uo=new WeakMap,Eu=new WeakMap;function wu(e){switch(e){case"Object":case"Array":return 1;case"Map":case"Set":case"WeakMap":case"WeakSet":return 2;default:return 0}}function Ou(e){return e.__v_skip||!Object.isExtensible(e)?0:wu(No(e))}function Cr(e){return e&&e.__v_isReadonly?e:Ko(e,!1,hu,yu,Fo)}function Wo(e){return Ko(e,!0,gu,mu,Uo)}function Ko(e,t,n,r,i){if(!_n(e))return console.warn(`value cannot be made reactive: ${String(e)}`),e;if(e.__v_raw&&!(t&&e.__v_isReactive))return e;const o=i.get(e);if(o)return o;const a=Ou(e);if(a===0)return e;const s=new Proxy(e,a===2?r:n);return i.set(e,s),s}function X(e){return e&&X(e.__v_raw)||e}function er(e){return!!(e&&e.__v_isRef===!0)}we("nextTick",()=>xr);we("dispatch",e=>Rt.bind(Rt,e));we("watch",(e,{evaluateLater:t,cleanup:n})=>(r,i)=>{let o=t(r),s=Hi(()=>{let c;return o(f=>c=f),c},i);n(s)});we("store",jc);we("data",e=>Gi(e));we("root",e=>pn(e));we("refs",e=>(e._x_refs_proxy||(e._x_refs_proxy=Ft(Au(e))),e._x_refs_proxy));function Au(e){let t=[];return Ut(e,n=>{n._x_refs&&t.push(n._x_refs)}),t}var In={};function zo(e){return In[e]||(In[e]=0),++In[e]}function Su(e,t){return Ut(e,n=>{if(n._x_ids&&n._x_ids[t])return!0})}function Tu(e,t){e._x_ids||(e._x_ids={}),e._x_ids[t]||(e._x_ids[t]=zo(t))}we("id",(e,{cleanup:t})=>(n,r=null)=>{let i=`${n}${r?`-${r}`:""}`;return Mu(e,i,t,()=>{let o=Su(e,n),a=o?o._x_ids[n]:zo(n);return r?`${n}-${a}-${r}`:`${n}-${a}`})});gn((e,t)=>{e._x_id&&(t._x_id=e._x_id)});function Mu(e,t,n,r){if(e._x_id||(e._x_id={}),e._x_id[t])return e._x_id[t];let i=r();return e._x_id[t]=i,n(()=>{delete e._x_id[t]}),i}we("el",e=>e);Vo("Focus","focus","focus");Vo("Persist","persist","persist");function Vo(e,t,n){we(t,r=>ve(`You can't use [$${t}] without first installing the "${e}" plugin here: https://alpinejs.dev/plugins/${n}`,r))}Q("modelable",(e,{expression:t},{effect:n,evaluateLater:r,cleanup:i})=>{let o=r(t),a=()=>{let l;return o(v=>l=v),l},s=r(`${t} = __placeholder`),c=l=>s(()=>{},{scope:{__placeholder:l}}),f=a();c(f),queueMicrotask(()=>{if(!e._x_model)return;e._x_removeModelListeners.default();let l=e._x_model.get,v=e._x_model.set,b=Mo({get(){return l()},set(_){v(_)}},{get(){return a()},set(_){c(_)}});i(b)})});Q("teleport",(e,{modifiers:t,expression:n},{cleanup:r})=>{e.tagName.toLowerCase()!=="template"&&ve("x-teleport can only be used on a <template> tag",e);let i=ui(n),o=e.content.cloneNode(!0).firstElementChild;e._x_teleport=o,o._x_teleportBack=e,e.setAttribute("data-teleport-template",!0),o.setAttribute("data-teleport-target",!0),e._x_forwardEvents&&e._x_forwardEvents.forEach(s=>{o.addEventListener(s,c=>{c.stopPropagation(),e.dispatchEvent(new c.constructor(c.type,c))})}),Ht(o,{},e);let a=(s,c,f)=>{f.includes("prepend")?c.parentNode.insertBefore(s,c):f.includes("append")?c.parentNode.insertBefore(s,c.nextSibling):c.appendChild(s)};ee(()=>{a(o,i,t),Ke(()=>{Ne(o),o._x_ignore=!0})()}),e._x_teleportPutBack=()=>{let s=ui(n);ee(()=>{a(e._x_teleport,s,t)})},r(()=>o.remove())});var Cu=document.createElement("div");function ui(e){let t=Ke(()=>document.querySelector(e),()=>Cu)();return t||ve(`Cannot find x-teleport element for selector: "${e}"`),t}var qo=()=>{};qo.inline=(e,{modifiers:t},{cleanup:n})=>{t.includes("self")?e._x_ignoreSelf=!0:e._x_ignore=!0,n(()=>{t.includes("self")?delete e._x_ignoreSelf:delete e._x_ignore})};Q("ignore",qo);Q("effect",Ke((e,{expression:t},{effect:n})=>{n(ae(e,t))}));function tr(e,t,n,r){let i=e,o=c=>r(c),a={},s=(c,f)=>l=>f(c,l);if(n.includes("dot")&&(t=Ru(t)),n.includes("camel")&&(t=Iu(t)),n.includes("passive")&&(a.passive=!0),n.includes("capture")&&(a.capture=!0),n.includes("window")&&(i=window),n.includes("document")&&(i=document),n.includes("debounce")){let c=n[n.indexOf("debounce")+1]||"invalid-wait",f=un(c.split("ms")[0])?Number(c.split("ms")[0]):250;o=So(o,f)}if(n.includes("throttle")){let c=n[n.indexOf("throttle")+1]||"invalid-wait",f=un(c.split("ms")[0])?Number(c.split("ms")[0]):250;o=To(o,f)}return n.includes("prevent")&&(o=s(o,(c,f)=>{f.preventDefault(),c(f)})),n.includes("stop")&&(o=s(o,(c,f)=>{f.stopPropagation(),c(f)})),n.includes("once")&&(o=s(o,(c,f)=>{c(f),i.removeEventListener(t,o,a)})),(n.includes("away")||n.includes("outside"))&&(i=document,o=s(o,(c,f)=>{e.contains(f.target)||f.target.isConnected!==!1&&(e.offsetWidth<1&&e.offsetHeight<1||e._x_isShown!==!1&&c(f))})),n.includes("self")&&(o=s(o,(c,f)=>{f.target===e&&c(f)})),o=s(o,(c,f)=>{Nu(t)&&Lu(f,n)||c(f)}),i.addEventListener(t,o,a),()=>{i.removeEventListener(t,o,a)}}function Ru(e){return e.replace(/-/g,".")}function Iu(e){return e.toLowerCase().replace(/-(\w)/g,(t,n)=>n.toUpperCase())}function un(e){return!Array.isArray(e)&&!isNaN(e)}function Du(e){return[" ","_"].includes(e)?e:e.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/[_\s]/,"-").toLowerCase()}function Nu(e){return["keydown","keyup"].includes(e)}function Lu(e,t){let n=t.filter(o=>!["window","document","prevent","stop","once","capture"].includes(o));if(n.includes("debounce")){let o=n.indexOf("debounce");n.splice(o,un((n[o+1]||"invalid-wait").split("ms")[0])?2:1)}if(n.includes("throttle")){let o=n.indexOf("throttle");n.splice(o,un((n[o+1]||"invalid-wait").split("ms")[0])?2:1)}if(n.length===0||n.length===1&&li(e.key).includes(n[0]))return!1;const i=["ctrl","shift","alt","meta","cmd","super"].filter(o=>n.includes(o));return n=n.filter(o=>!i.includes(o)),!(i.length>0&&i.filter(a=>((a==="cmd"||a==="super")&&(a="meta"),e[`${a}Key`])).length===i.length&&li(e.key).includes(n[0]))}function li(e){if(!e)return[];e=Du(e);let t={ctrl:"control",slash:"/",space:" ",spacebar:" ",cmd:"meta",esc:"escape",up:"arrow-up",down:"arrow-down",left:"arrow-left",right:"arrow-right",period:".",comma:",",equal:"=",minus:"-",underscore:"_"};return t[e]=e,Object.keys(t).map(n=>{if(t[n]===e)return n}).filter(n=>n)}Q("model",(e,{modifiers:t,expression:n},{effect:r,cleanup:i})=>{let o=e;t.includes("parent")&&(o=e.parentNode);let a=ae(o,n),s;typeof n=="string"?s=ae(o,`${n} = __placeholder`):typeof n=="function"&&typeof n()=="string"?s=ae(o,`${n()} = __placeholder`):s=()=>{};let c=()=>{let b;return a(_=>b=_),fi(b)?b.get():b},f=b=>{let _;a(A=>_=A),fi(_)?_.set(b):s(()=>{},{scope:{__placeholder:b}})};typeof n=="string"&&e.type==="radio"&&ee(()=>{e.hasAttribute("name")||e.setAttribute("name",n)});var l=e.tagName.toLowerCase()==="select"||["checkbox","radio"].includes(e.type)||t.includes("lazy")?"change":"input";let v=Fe?()=>{}:tr(e,l,t,b=>{f(Dn(e,t,b,c()))});if(t.includes("fill")&&([void 0,null,""].includes(c())||e.type==="checkbox"&&Array.isArray(c())||e.tagName.toLowerCase()==="select"&&e.multiple)&&f(Dn(e,t,{target:e},c())),e._x_removeModelListeners||(e._x_removeModelListeners={}),e._x_removeModelListeners.default=v,i(()=>e._x_removeModelListeners.default()),e.form){let b=tr(e.form,"reset",[],_=>{xr(()=>e._x_model&&e._x_model.set(Dn(e,t,{target:e},c())))});i(()=>b())}e._x_model={get(){return c()},set(b){f(b)}},e._x_forceModelUpdate=b=>{b===void 0&&typeof n=="string"&&n.match(/\./)&&(b=""),window.fromModel=!0,ee(()=>Eo(e,"value",b)),delete window.fromModel},r(()=>{let b=c();t.includes("unintrusive")&&document.activeElement.isSameNode(e)||e._x_forceModelUpdate(b)})});function Dn(e,t,n,r){return ee(()=>{if(n instanceof CustomEvent&&n.detail!==void 0)return n.detail!==null&&n.detail!==void 0?n.detail:n.target.value;if(e.type==="checkbox")if(Array.isArray(r)){let i=null;return t.includes("number")?i=Nn(n.target.value):t.includes("boolean")?i=nn(n.target.value):i=n.target.value,n.target.checked?r.includes(i)?r:r.concat([i]):r.filter(o=>!Pu(o,i))}else return n.target.checked;else{if(e.tagName.toLowerCase()==="select"&&e.multiple)return t.includes("number")?Array.from(n.target.selectedOptions).map(i=>{let o=i.value||i.text;return Nn(o)}):t.includes("boolean")?Array.from(n.target.selectedOptions).map(i=>{let o=i.value||i.text;return nn(o)}):Array.from(n.target.selectedOptions).map(i=>i.value||i.text);{let i;return e.type==="radio"?n.target.checked?i=n.target.value:i=r:i=n.target.value,t.includes("number")?Nn(i):t.includes("boolean")?nn(i):t.includes("trim")?i.trim():i}}})}function Nn(e){let t=e?parseFloat(e):null;return ku(t)?t:e}function Pu(e,t){return e==t}function ku(e){return!Array.isArray(e)&&!isNaN(e)}function fi(e){return e!==null&&typeof e=="object"&&typeof e.get=="function"&&typeof e.set=="function"}Q("cloak",e=>queueMicrotask(()=>ee(()=>e.removeAttribute(xt("cloak")))));go(()=>`[${xt("init")}]`);Q("init",Ke((e,{expression:t},{evaluate:n})=>typeof t=="string"?!!t.trim()&&n(t,{},!1):n(t,{},!1)));Q("text",(e,{expression:t},{effect:n,evaluateLater:r})=>{let i=r(t);n(()=>{i(o=>{ee(()=>{e.textContent=o})})})});Q("html",(e,{expression:t},{effect:n,evaluateLater:r})=>{let i=r(t);n(()=>{i(o=>{ee(()=>{e.innerHTML=o,e._x_ignoreSelf=!0,Ne(e),delete e._x_ignoreSelf})})})});br(io(":",oo(xt("bind:"))));var Go=(e,{value:t,modifiers:n,expression:r,original:i},{effect:o,cleanup:a})=>{if(!t){let c={};Fc(c),ae(e,r)(l=>{Ro(e,l,i)},{scope:c});return}if(t==="key")return Bu(e,r);if(e._x_inlineBindings&&e._x_inlineBindings[t]&&e._x_inlineBindings[t].extract)return;let s=ae(e,r);o(()=>s(c=>{c===void 0&&typeof r=="string"&&r.match(/\./)&&(c=""),ee(()=>Eo(e,t,c,n))})),a(()=>{e._x_undoAddedClasses&&e._x_undoAddedClasses(),e._x_undoAddedStyles&&e._x_undoAddedStyles()})};Go.inline=(e,{value:t,modifiers:n,expression:r})=>{t&&(e._x_inlineBindings||(e._x_inlineBindings={}),e._x_inlineBindings[t]={expression:r,extract:!1})};Q("bind",Go);function Bu(e,t){e._x_keyExpression=t}ho(()=>`[${xt("data")}]`);Q("data",(e,{expression:t},{cleanup:n})=>{if($u(e))return;t=t===""?"{}":t;let r={};Kn(r,e);let i={};Wc(i,r);let o=et(e,t,{scope:i});(o===void 0||o===!0)&&(o={}),Kn(o,e);let a=yt(o);Xi(a);let s=Ht(e,a);a.init&&et(e,a.init),n(()=>{a.destroy&&et(e,a.destroy),s()})});gn((e,t)=>{e._x_dataStack&&(t._x_dataStack=e._x_dataStack,t.setAttribute("data-has-alpine-state",!0))});function $u(e){return Fe?Jn?!0:e.hasAttribute("data-has-alpine-state"):!1}Q("show",(e,{modifiers:t,expression:n},{effect:r})=>{let i=ae(e,n);e._x_doHide||(e._x_doHide=()=>{ee(()=>{e.style.setProperty("display","none",t.includes("important")?"important":void 0)})}),e._x_doShow||(e._x_doShow=()=>{ee(()=>{e.style.length===1&&e.style.display==="none"?e.removeAttribute("style"):e.style.removeProperty("display")})});let o=()=>{e._x_doHide(),e._x_isShown=!1},a=()=>{e._x_doShow(),e._x_isShown=!0},s=()=>setTimeout(a),c=Xn(v=>v?a():o(),v=>{typeof e._x_toggleAndCascadeWithTransitions=="function"?e._x_toggleAndCascadeWithTransitions(e,v,a,o):v?s():o()}),f,l=!0;r(()=>i(v=>{!l&&v===f||(t.includes("immediate")&&(v?s():o()),c(v),f=v,l=!1)}))});Q("for",(e,{expression:t},{effect:n,cleanup:r})=>{let i=Hu(t),o=ae(e,i.items),a=ae(e,e._x_keyExpression||"index");e._x_prevKeys=[],e._x_lookup={},n(()=>ju(e,i,o,a)),r(()=>{Object.values(e._x_lookup).forEach(s=>s.remove()),delete e._x_prevKeys,delete e._x_lookup})});function ju(e,t,n,r){let i=a=>typeof a=="object"&&!Array.isArray(a),o=e;n(a=>{Fu(a)&&a>=0&&(a=Array.from(Array(a).keys(),h=>h+1)),a===void 0&&(a=[]);let s=e._x_lookup,c=e._x_prevKeys,f=[],l=[];if(i(a))a=Object.entries(a).map(([h,E])=>{let O=di(t,E,h,a);r(M=>{l.includes(M)&&ve("Duplicate key on x-for",e),l.push(M)},{scope:{index:h,...O}}),f.push(O)});else for(let h=0;h<a.length;h++){let E=di(t,a[h],h,a);r(O=>{l.includes(O)&&ve("Duplicate key on x-for",e),l.push(O)},{scope:{index:h,...E}}),f.push(E)}let v=[],b=[],_=[],A=[];for(let h=0;h<c.length;h++){let E=c[h];l.indexOf(E)===-1&&_.push(E)}c=c.filter(h=>!_.includes(h));let S="template";for(let h=0;h<l.length;h++){let E=l[h],O=c.indexOf(E);if(O===-1)c.splice(h,0,E),v.push([S,h]);else if(O!==h){let M=c.splice(h,1)[0],u=c.splice(O-1,1)[0];c.splice(h,0,u),c.splice(O,0,M),b.push([M,u])}else A.push(E);S=E}for(let h=0;h<_.length;h++){let E=_[h];s[E]._x_effects&&s[E]._x_effects.forEach($i),s[E].remove(),s[E]=null,delete s[E]}for(let h=0;h<b.length;h++){let[E,O]=b[h],M=s[E],u=s[O],I=document.createElement("div");ee(()=>{u||ve('x-for ":key" is undefined or invalid',o,O,s),u.after(I),M.after(u),u._x_currentIfEl&&u.after(u._x_currentIfEl),I.before(M),M._x_currentIfEl&&M.after(M._x_currentIfEl),I.remove()}),u._x_refreshXForScope(f[l.indexOf(O)])}for(let h=0;h<v.length;h++){let[E,O]=v[h],M=E==="template"?o:s[E];M._x_currentIfEl&&(M=M._x_currentIfEl);let u=f[O],I=l[O],m=document.importNode(o.content,!0).firstElementChild,L=yt(u);Ht(m,L,o),m._x_refreshXForScope=K=>{Object.entries(K).forEach(([P,H])=>{L[P]=H})},ee(()=>{M.after(m),Ke(()=>Ne(m))()}),typeof I=="object"&&ve("x-for key cannot be an object, it must be a string or an integer",o),s[I]=m}for(let h=0;h<A.length;h++)s[A[h]]._x_refreshXForScope(f[l.indexOf(A[h])]);o._x_prevKeys=l})}function Hu(e){let t=/,([^,\}\]]*)(?:,([^,\}\]]*))?$/,n=/^\s*\(|\)\s*$/g,r=/([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/,i=e.match(r);if(!i)return;let o={};o.items=i[2].trim();let a=i[1].replace(n,"").trim(),s=a.match(t);return s?(o.item=a.replace(t,"").trim(),o.index=s[1].trim(),s[2]&&(o.collection=s[2].trim())):o.item=a,o}function di(e,t,n,r){let i={};return/^\[.*\]$/.test(e.item)&&Array.isArray(t)?e.item.replace("[","").replace("]","").split(",").map(a=>a.trim()).forEach((a,s)=>{i[a]=t[s]}):/^\{.*\}$/.test(e.item)&&!Array.isArray(t)&&typeof t=="object"?e.item.replace("{","").replace("}","").split(",").map(a=>a.trim()).forEach(a=>{i[a]=t[a]}):i[e.item]=t,e.index&&(i[e.index]=n),e.collection&&(i[e.collection]=r),i}function Fu(e){return!Array.isArray(e)&&!isNaN(e)}function Xo(){}Xo.inline=(e,{expression:t},{cleanup:n})=>{let r=pn(e);r._x_refs||(r._x_refs={}),r._x_refs[t]=e,n(()=>delete r._x_refs[t])};Q("ref",Xo);Q("if",(e,{expression:t},{effect:n,cleanup:r})=>{e.tagName.toLowerCase()!=="template"&&ve("x-if can only be used on a <template> tag",e);let i=ae(e,t),o=()=>{if(e._x_currentIfEl)return e._x_currentIfEl;let s=e.content.cloneNode(!0).firstElementChild;return Ht(s,{},e),ee(()=>{e.after(s),Ke(()=>Ne(s))()}),e._x_currentIfEl=s,e._x_undoIf=()=>{He(s,c=>{c._x_effects&&c._x_effects.forEach($i)}),s.remove(),delete e._x_currentIfEl},s},a=()=>{e._x_undoIf&&(e._x_undoIf(),delete e._x_undoIf)};n(()=>i(s=>{s?o():a()})),r(()=>e._x_undoIf&&e._x_undoIf())});Q("id",(e,{expression:t},{evaluate:n})=>{n(t).forEach(i=>Tu(e,i))});gn((e,t)=>{e._x_ids&&(t._x_ids=e._x_ids)});br(io("@",oo(xt("on:"))));Q("on",Ke((e,{value:t,modifiers:n,expression:r},{cleanup:i})=>{let o=r?ae(e,r):()=>{};e.tagName.toLowerCase()==="template"&&(e._x_forwardEvents||(e._x_forwardEvents=[]),e._x_forwardEvents.includes(t)||e._x_forwardEvents.push(t));let a=tr(e,t,n,s=>{o(()=>{},{scope:{$event:s},params:[s]})});i(()=>a())}));yn("Collapse","collapse","collapse");yn("Intersect","intersect","intersect");yn("Focus","trap","focus");yn("Mask","mask","mask");function yn(e,t,n){Q(t,r=>ve(`You can't use [x-${t}] without first installing the "${e}" plugin here: https://alpinejs.dev/plugins/${n}`,r))}Wt.setEvaluator(eo);Wt.setReactivityEngine({reactive:Cr,effect:Qc,release:eu,raw:X});var Uu=Wt,Wu=Uu;function Ku(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function Yo(e){return e instanceof Map?e.clear=e.delete=e.set=function(){throw new Error("map is read-only")}:e instanceof Set&&(e.add=e.clear=e.delete=function(){throw new Error("set is read-only")}),Object.freeze(e),Object.getOwnPropertyNames(e).forEach(t=>{const n=e[t],r=typeof n;(r==="object"||r==="function")&&!Object.isFrozen(n)&&Yo(n)}),e}class pi{constructor(t){t.data===void 0&&(t.data={}),this.data=t.data,this.isMatchIgnored=!1}ignoreMatch(){this.isMatchIgnored=!0}}function Jo(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function je(e,...t){const n=Object.create(null);for(const r in e)n[r]=e[r];return t.forEach(function(r){for(const i in r)n[i]=r[i]}),n}const zu="</span>",hi=e=>!!e.scope,Vu=(e,{prefix:t})=>{if(e.startsWith("language:"))return e.replace("language:","language-");if(e.includes(".")){const n=e.split(".");return[`${t}${n.shift()}`,...n.map((r,i)=>`${r}${"_".repeat(i+1)}`)].join(" ")}return`${t}${e}`};class qu{constructor(t,n){this.buffer="",this.classPrefix=n.classPrefix,t.walk(this)}addText(t){this.buffer+=Jo(t)}openNode(t){if(!hi(t))return;const n=Vu(t.scope,{prefix:this.classPrefix});this.span(n)}closeNode(t){hi(t)&&(this.buffer+=zu)}value(){return this.buffer}span(t){this.buffer+=`<span class="${t}">`}}const gi=(e={})=>{const t={children:[]};return Object.assign(t,e),t};class Rr{constructor(){this.rootNode=gi(),this.stack=[this.rootNode]}get top(){return this.stack[this.stack.length-1]}get root(){return this.rootNode}add(t){this.top.children.push(t)}openNode(t){const n=gi({scope:t});this.add(n),this.stack.push(n)}closeNode(){if(this.stack.length>1)return this.stack.pop()}closeAllNodes(){for(;this.closeNode(););}toJSON(){return JSON.stringify(this.rootNode,null,4)}walk(t){return this.constructor._walk(t,this.rootNode)}static _walk(t,n){return typeof n=="string"?t.addText(n):n.children&&(t.openNode(n),n.children.forEach(r=>this._walk(t,r)),t.closeNode(n)),t}static _collapse(t){typeof t!="string"&&t.children&&(t.children.every(n=>typeof n=="string")?t.children=[t.children.join("")]:t.children.forEach(n=>{Rr._collapse(n)}))}}class Gu extends Rr{constructor(t){super(),this.options=t}addText(t){t!==""&&this.add(t)}startScope(t){this.openNode(t)}endScope(){this.closeNode()}__addSublanguage(t,n){const r=t.root;n&&(r.scope=`language:${n}`),this.add(r)}toHTML(){return new qu(this,this.options).value()}finalize(){return this.closeAllNodes(),!0}}function Pt(e){return e?typeof e=="string"?e:e.source:null}function Zo(e){return at("(?=",e,")")}function Xu(e){return at("(?:",e,")*")}function Yu(e){return at("(?:",e,")?")}function at(...e){return e.map(n=>Pt(n)).join("")}function Ju(e){const t=e[e.length-1];return typeof t=="object"&&t.constructor===Object?(e.splice(e.length-1,1),t):{}}function Ir(...e){return"("+(Ju(e).capture?"":"?:")+e.map(r=>Pt(r)).join("|")+")"}function Qo(e){return new RegExp(e.toString()+"|").exec("").length-1}function Zu(e,t){const n=e&&e.exec(t);return n&&n.index===0}const Qu=/\[(?:[^\\\]]|\\.)*\]|\(\??|\\([1-9][0-9]*)|\\./;function Dr(e,{joinWith:t}){let n=0;return e.map(r=>{n+=1;const i=n;let o=Pt(r),a="";for(;o.length>0;){const s=Qu.exec(o);if(!s){a+=o;break}a+=o.substring(0,s.index),o=o.substring(s.index+s[0].length),s[0][0]==="\\"&&s[1]?a+="\\"+String(Number(s[1])+i):(a+=s[0],s[0]==="("&&n++)}return a}).map(r=>`(${r})`).join(t)}const el=/\b\B/,ea="[a-zA-Z]\\w*",Nr="[a-zA-Z_]\\w*",ta="\\b\\d+(\\.\\d+)?",na="(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)",ra="\\b(0b[01]+)",tl="!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~",nl=(e={})=>{const t=/^#![ ]*\//;return e.binary&&(e.begin=at(t,/.*\b/,e.binary,/\b.*/)),je({scope:"meta",begin:t,end:/$/,relevance:0,"on:begin":(n,r)=>{n.index!==0&&r.ignoreMatch()}},e)},kt={begin:"\\\\[\\s\\S]",relevance:0},rl={scope:"string",begin:"'",end:"'",illegal:"\\n",contains:[kt]},il={scope:"string",begin:'"',end:'"',illegal:"\\n",contains:[kt]},ol={begin:/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/},mn=function(e,t,n={}){const r=je({scope:"comment",begin:e,end:t,contains:[]},n);r.contains.push({scope:"doctag",begin:"[ ]*(?=(TODO|FIXME|NOTE|BUG|OPTIMIZE|HACK|XXX):)",end:/(TODO|FIXME|NOTE|BUG|OPTIMIZE|HACK|XXX):/,excludeBegin:!0,relevance:0});const i=Ir("I","a","is","so","us","to","at","if","in","it","on",/[A-Za-z]+['](d|ve|re|ll|t|s|n)/,/[A-Za-z]+[-][a-z]+/,/[A-Za-z][a-z]{2,}/);return r.contains.push({begin:at(/[ ]+/,"(",i,/[.]?[:]?([.][ ]|[ ])/,"){3}")}),r},al=mn("//","$"),sl=mn("/\\*","\\*/"),cl=mn("#","$"),ul={scope:"number",begin:ta,relevance:0},ll={scope:"number",begin:na,relevance:0},fl={scope:"number",begin:ra,relevance:0},dl={scope:"regexp",begin:/\/(?=[^/\n]*\/)/,end:/\/[gimuy]*/,contains:[kt,{begin:/\[/,end:/\]/,relevance:0,contains:[kt]}]},pl={scope:"title",begin:ea,relevance:0},hl={scope:"title",begin:Nr,relevance:0},gl={begin:"\\.\\s*"+Nr,relevance:0},vl=function(e){return Object.assign(e,{"on:begin":(t,n)=>{n.data._beginMatch=t[1]},"on:end":(t,n)=>{n.data._beginMatch!==t[1]&&n.ignoreMatch()}})};var Qt=Object.freeze({__proto__:null,APOS_STRING_MODE:rl,BACKSLASH_ESCAPE:kt,BINARY_NUMBER_MODE:fl,BINARY_NUMBER_RE:ra,COMMENT:mn,C_BLOCK_COMMENT_MODE:sl,C_LINE_COMMENT_MODE:al,C_NUMBER_MODE:ll,C_NUMBER_RE:na,END_SAME_AS_BEGIN:vl,HASH_COMMENT_MODE:cl,IDENT_RE:ea,MATCH_NOTHING_RE:el,METHOD_GUARD:gl,NUMBER_MODE:ul,NUMBER_RE:ta,PHRASAL_WORDS_MODE:ol,QUOTE_STRING_MODE:il,REGEXP_MODE:dl,RE_STARTERS_RE:tl,SHEBANG:nl,TITLE_MODE:pl,UNDERSCORE_IDENT_RE:Nr,UNDERSCORE_TITLE_MODE:hl});function _l(e,t){e.input[e.index-1]==="."&&t.ignoreMatch()}function bl(e,t){e.className!==void 0&&(e.scope=e.className,delete e.className)}function yl(e,t){t&&e.beginKeywords&&(e.begin="\\b("+e.beginKeywords.split(" ").join("|")+")(?!\\.)(?=\\b|\\s)",e.__beforeBegin=_l,e.keywords=e.keywords||e.beginKeywords,delete e.beginKeywords,e.relevance===void 0&&(e.relevance=0))}function ml(e,t){Array.isArray(e.illegal)&&(e.illegal=Ir(...e.illegal))}function xl(e,t){if(e.match){if(e.begin||e.end)throw new Error("begin & end are not supported with match");e.begin=e.match,delete e.match}}function El(e,t){e.relevance===void 0&&(e.relevance=1)}const wl=(e,t)=>{if(!e.beforeMatch)return;if(e.starts)throw new Error("beforeMatch cannot be used with starts");const n=Object.assign({},e);Object.keys(e).forEach(r=>{delete e[r]}),e.keywords=n.keywords,e.begin=at(n.beforeMatch,Zo(n.begin)),e.starts={relevance:0,contains:[Object.assign(n,{endsParent:!0})]},e.relevance=0,delete n.beforeMatch},Ol=["of","and","for","in","not","or","if","then","parent","list","value"],Al="keyword";function ia(e,t,n=Al){const r=Object.create(null);return typeof e=="string"?i(n,e.split(" ")):Array.isArray(e)?i(n,e):Object.keys(e).forEach(function(o){Object.assign(r,ia(e[o],t,o))}),r;function i(o,a){t&&(a=a.map(s=>s.toLowerCase())),a.forEach(function(s){const c=s.split("|");r[c[0]]=[o,Sl(c[0],c[1])]})}}function Sl(e,t){return t?Number(t):Tl(e)?0:1}function Tl(e){return Ol.includes(e.toLowerCase())}const vi={},rt=e=>{console.error(e)},_i=(e,...t)=>{console.log(`WARN: ${e}`,...t)},lt=(e,t)=>{vi[`${e}/${t}`]||(console.log(`Deprecated as of ${e}. ${t}`),vi[`${e}/${t}`]=!0)},ln=new Error;function oa(e,t,{key:n}){let r=0;const i=e[n],o={},a={};for(let s=1;s<=t.length;s++)a[s+r]=i[s],o[s+r]=!0,r+=Qo(t[s-1]);e[n]=a,e[n]._emit=o,e[n]._multi=!0}function Ml(e){if(Array.isArray(e.begin)){if(e.skip||e.excludeBegin||e.returnBegin)throw rt("skip, excludeBegin, returnBegin not compatible with beginScope: {}"),ln;if(typeof e.beginScope!="object"||e.beginScope===null)throw rt("beginScope must be object"),ln;oa(e,e.begin,{key:"beginScope"}),e.begin=Dr(e.begin,{joinWith:""})}}function Cl(e){if(Array.isArray(e.end)){if(e.skip||e.excludeEnd||e.returnEnd)throw rt("skip, excludeEnd, returnEnd not compatible with endScope: {}"),ln;if(typeof e.endScope!="object"||e.endScope===null)throw rt("endScope must be object"),ln;oa(e,e.end,{key:"endScope"}),e.end=Dr(e.end,{joinWith:""})}}function Rl(e){e.scope&&typeof e.scope=="object"&&e.scope!==null&&(e.beginScope=e.scope,delete e.scope)}function Il(e){Rl(e),typeof e.beginScope=="string"&&(e.beginScope={_wrap:e.beginScope}),typeof e.endScope=="string"&&(e.endScope={_wrap:e.endScope}),Ml(e),Cl(e)}function Dl(e){function t(a,s){return new RegExp(Pt(a),"m"+(e.case_insensitive?"i":"")+(e.unicodeRegex?"u":"")+(s?"g":""))}class n{constructor(){this.matchIndexes={},this.regexes=[],this.matchAt=1,this.position=0}addRule(s,c){c.position=this.position++,this.matchIndexes[this.matchAt]=c,this.regexes.push([c,s]),this.matchAt+=Qo(s)+1}compile(){this.regexes.length===0&&(this.exec=()=>null);const s=this.regexes.map(c=>c[1]);this.matcherRe=t(Dr(s,{joinWith:"|"}),!0),this.lastIndex=0}exec(s){this.matcherRe.lastIndex=this.lastIndex;const c=this.matcherRe.exec(s);if(!c)return null;const f=c.findIndex((v,b)=>b>0&&v!==void 0),l=this.matchIndexes[f];return c.splice(0,f),Object.assign(c,l)}}class r{constructor(){this.rules=[],this.multiRegexes=[],this.count=0,this.lastIndex=0,this.regexIndex=0}getMatcher(s){if(this.multiRegexes[s])return this.multiRegexes[s];const c=new n;return this.rules.slice(s).forEach(([f,l])=>c.addRule(f,l)),c.compile(),this.multiRegexes[s]=c,c}resumingScanAtSamePosition(){return this.regexIndex!==0}considerAll(){this.regexIndex=0}addRule(s,c){this.rules.push([s,c]),c.type==="begin"&&this.count++}exec(s){const c=this.getMatcher(this.regexIndex);c.lastIndex=this.lastIndex;let f=c.exec(s);if(this.resumingScanAtSamePosition()&&!(f&&f.index===this.lastIndex)){const l=this.getMatcher(0);l.lastIndex=this.lastIndex+1,f=l.exec(s)}return f&&(this.regexIndex+=f.position+1,this.regexIndex===this.count&&this.considerAll()),f}}function i(a){const s=new r;return a.contains.forEach(c=>s.addRule(c.begin,{rule:c,type:"begin"})),a.terminatorEnd&&s.addRule(a.terminatorEnd,{type:"end"}),a.illegal&&s.addRule(a.illegal,{type:"illegal"}),s}function o(a,s){const c=a;if(a.isCompiled)return c;[bl,xl,Il,wl].forEach(l=>l(a,s)),e.compilerExtensions.forEach(l=>l(a,s)),a.__beforeBegin=null,[yl,ml,El].forEach(l=>l(a,s)),a.isCompiled=!0;let f=null;return typeof a.keywords=="object"&&a.keywords.$pattern&&(a.keywords=Object.assign({},a.keywords),f=a.keywords.$pattern,delete a.keywords.$pattern),f=f||/\w+/,a.keywords&&(a.keywords=ia(a.keywords,e.case_insensitive)),c.keywordPatternRe=t(f,!0),s&&(a.begin||(a.begin=/\B|\b/),c.beginRe=t(c.begin),!a.end&&!a.endsWithParent&&(a.end=/\B|\b/),a.end&&(c.endRe=t(c.end)),c.terminatorEnd=Pt(c.end)||"",a.endsWithParent&&s.terminatorEnd&&(c.terminatorEnd+=(a.end?"|":"")+s.terminatorEnd)),a.illegal&&(c.illegalRe=t(a.illegal)),a.contains||(a.contains=[]),a.contains=[].concat(...a.contains.map(function(l){return Nl(l==="self"?a:l)})),a.contains.forEach(function(l){o(l,c)}),a.starts&&o(a.starts,s),c.matcher=i(c),c}if(e.compilerExtensions||(e.compilerExtensions=[]),e.contains&&e.contains.includes("self"))throw new Error("ERR: contains `self` is not supported at the top-level of a language. See documentation.");return e.classNameAliases=je(e.classNameAliases||{}),o(e)}function aa(e){return e?e.endsWithParent||aa(e.starts):!1}function Nl(e){return e.variants&&!e.cachedVariants&&(e.cachedVariants=e.variants.map(function(t){return je(e,{variants:null},t)})),e.cachedVariants?e.cachedVariants:aa(e)?je(e,{starts:e.starts?je(e.starts):null}):Object.isFrozen(e)?je(e):e}var Ll="11.9.0";class Pl extends Error{constructor(t,n){super(t),this.name="HTMLInjectionError",this.html=n}}const Ln=Jo,bi=je,yi=Symbol("nomatch"),kl=7,sa=function(e){const t=Object.create(null),n=Object.create(null),r=[];let i=!0;const o="Could not find the language '{}', did you forget to load/include a language module?",a={disableAutodetect:!0,name:"Plain text",contains:[]};let s={ignoreUnescapedHTML:!1,throwUnescapedHTML:!1,noHighlightRe:/^(no-?highlight)$/i,languageDetectRe:/\blang(?:uage)?-([\w-]+)\b/i,classPrefix:"hljs-",cssSelector:"pre code",languages:null,__emitter:Gu};function c(p){return s.noHighlightRe.test(p)}function f(p){let y=p.className+" ";y+=p.parentNode?p.parentNode.className:"";const C=s.languageDetectRe.exec(y);if(C){const N=P(C[1]);return N||(_i(o.replace("{}",C[1])),_i("Falling back to no-highlight mode for this block.",p)),N?C[1]:"no-highlight"}return y.split(/\s+/).find(N=>c(N)||P(N))}function l(p,y,C){let N="",W="";typeof y=="object"?(N=p,C=y.ignoreIllegals,W=y.language):(lt("10.7.0","highlight(lang, code, ...args) has been deprecated."),lt("10.7.0",`Please use highlight(code, options) instead. https://github.com/highlightjs/highlight.js/issues/2277`),W=p,N=y),C===void 0&&(C=!0);const J={code:N,language:W};Y("before:highlight",J);const re=J.result?J.result:v(J.language,J.code,C);return re.code=J.code,Y("after:highlight",re),re}function v(p,y,C,N){const W=Object.create(null);function J(g,x){return g.keywords[x]}function re(){if(!T.keywords){V.addText($);return}let g=0;T.keywordPatternRe.lastIndex=0;let x=T.keywordPatternRe.exec($),D="";for(;x;){D+=$.substring(g,x.index);const j=ie.case_insensitive?x[0].toLowerCase():x[0],ne=J(T,j);if(ne){const[me,En]=ne;if(V.addText(D),D="",W[j]=(W[j]||0)+1,W[j]<=kl&&(he+=En),me.startsWith("_"))D+=x[0];else{const d=ie.classNameAliases[me]||me;Z(x[0],d)}}else D+=x[0];g=T.keywordPatternRe.lastIndex,x=T.keywordPatternRe.exec($)}D+=$.substring(g),V.addText(D)}function se(){if($==="")return;let g=null;if(typeof T.subLanguage=="string"){if(!t[T.subLanguage]){V.addText($);return}g=v(T.subLanguage,$,!0,pe[T.subLanguage]),pe[T.subLanguage]=g._top}else g=_($,T.subLanguage.length?T.subLanguage:null);T.relevance>0&&(he+=g.relevance),V.__addSublanguage(g._emitter,g.language)}function G(){T.subLanguage!=null?se():re(),$=""}function Z(g,x){g!==""&&(V.startScope(x),V.addText(g),V.endScope())}function Ce(g,x){let D=1;const j=x.length-1;for(;D<=j;){if(!g._emit[D]){D++;continue}const ne=ie.classNameAliases[g[D]]||g[D],me=x[D];ne?Z(me,ne):($=me,re(),$=""),D++}}function de(g,x){return g.scope&&typeof g.scope=="string"&&V.openNode(ie.classNameAliases[g.scope]||g.scope),g.beginScope&&(g.beginScope._wrap?(Z($,ie.classNameAliases[g.beginScope._wrap]||g.beginScope._wrap),$=""):g.beginScope._multi&&(Ce(g.beginScope,x),$="")),T=Object.create(g,{parent:{value:T}}),T}function Re(g,x,D){let j=Zu(g.endRe,D);if(j){if(g["on:end"]){const ne=new pi(g);g["on:end"](x,ne),ne.isMatchIgnored&&(j=!1)}if(j){for(;g.endsParent&&g.parent;)g=g.parent;return g}}if(g.endsWithParent)return Re(g.parent,x,D)}function Oe(g){return T.matcher.regexIndex===0?($+=g[0],1):(Be=!0,0)}function Le(g){const x=g[0],D=g.rule,j=new pi(D),ne=[D.__beforeBegin,D["on:begin"]];for(const me of ne)if(me&&(me(g,j),j.isMatchIgnored))return Oe(x);return D.skip?$+=x:(D.excludeBegin&&($+=x),G(),!D.returnBegin&&!D.excludeBegin&&($=x)),de(D,g),D.returnBegin?0:x.length}function st(g){const x=g[0],D=y.substring(g.index),j=Re(T,g,D);if(!j)return yi;const ne=T;T.endScope&&T.endScope._wrap?(G(),Z(x,T.endScope._wrap)):T.endScope&&T.endScope._multi?(G(),Ce(T.endScope,g)):ne.skip?$+=x:(ne.returnEnd||ne.excludeEnd||($+=x),G(),ne.excludeEnd&&($=x));do T.scope&&V.closeNode(),!T.skip&&!T.subLanguage&&(he+=T.relevance),T=T.parent;while(T!==j.parent);return j.starts&&de(j.starts,g),ne.returnEnd?0:x.length}function ze(){const g=[];for(let x=T;x!==ie;x=x.parent)x.scope&&g.unshift(x.scope);g.forEach(x=>V.openNode(x))}let Ie={};function Ve(g,x){const D=x&&x[0];if($+=g,D==null)return G(),0;if(Ie.type==="begin"&&x.type==="end"&&Ie.index===x.index&&D===""){if($+=y.slice(x.index,x.index+1),!i){const j=new Error(`0 width match regex (${p})`);throw j.languageName=p,j.badRule=Ie.rule,j}return 1}if(Ie=x,x.type==="begin")return Le(x);if(x.type==="illegal"&&!C){const j=new Error('Illegal lexeme "'+D+'" for mode "'+(T.scope||"<unnamed>")+'"');throw j.mode=T,j}else if(x.type==="end"){const j=st(x);if(j!==yi)return j}if(x.type==="illegal"&&D==="")return 1;if(ke>1e5&&ke>x.index*3)throw new Error("potential infinite loop, way more iterations than matches");return $+=D,D.length}const ie=P(p);if(!ie)throw rt(o.replace("{}",p)),new Error('Unknown language: "'+p+'"');const qe=Dl(ie);let Pe="",T=N||qe;const pe={},V=new s.__emitter(s);ze();let $="",he=0,le=0,ke=0,Be=!1;try{if(ie.__emitTokens)ie.__emitTokens(y,V);else{for(T.matcher.considerAll();;){ke++,Be?Be=!1:T.matcher.considerAll(),T.matcher.lastIndex=le;const g=T.matcher.exec(y);if(!g)break;const x=y.substring(le,g.index),D=Ve(x,g);le=g.index+D}Ve(y.substring(le))}return V.finalize(),Pe=V.toHTML(),{language:p,value:Pe,relevance:he,illegal:!1,_emitter:V,_top:T}}catch(g){if(g.message&&g.message.includes("Illegal"))return{language:p,value:Ln(y),illegal:!0,relevance:0,_illegalBy:{message:g.message,index:le,context:y.slice(le-100,le+100),mode:g.mode,resultSoFar:Pe},_emitter:V};if(i)return{language:p,value:Ln(y),illegal:!1,relevance:0,errorRaised:g,_emitter:V,_top:T};throw g}}function b(p){const y={value:Ln(p),illegal:!1,relevance:0,_top:a,_emitter:new s.__emitter(s)};return y._emitter.addText(p),y}function _(p,y){y=y||s.languages||Object.keys(t);const C=b(p),N=y.filter(P).filter(q).map(G=>v(G,p,!1));N.unshift(C);const W=N.sort((G,Z)=>{if(G.relevance!==Z.relevance)return Z.relevance-G.relevance;if(G.language&&Z.language){if(P(G.language).supersetOf===Z.language)return 1;if(P(Z.language).supersetOf===G.language)return-1}return 0}),[J,re]=W,se=J;return se.secondBest=re,se}function A(p,y,C){const N=y&&n[y]||C;p.classList.add("hljs"),p.classList.add(`language-${N}`)}function S(p){let y=null;const C=f(p);if(c(C))return;if(Y("before:highlightElement",{el:p,language:C}),p.dataset.highlighted){console.log("Element previously highlighted. To highlight again, first unset `dataset.highlighted`.",p);return}if(p.children.length>0&&(s.ignoreUnescapedHTML||(console.warn("One of your code blocks includes unescaped HTML. This is a potentially serious security risk."),console.warn("https://github.com/highlightjs/highlight.js/wiki/security"),console.warn("The element with unescaped HTML:"),console.warn(p)),s.throwUnescapedHTML))throw new Pl("One of your code blocks includes unescaped HTML.",p.innerHTML);y=p;const N=y.textContent,W=C?l(N,{language:C,ignoreIllegals:!0}):_(N);p.innerHTML=W.value,p.dataset.highlighted="yes",A(p,C,W.language),p.result={language:W.language,re:W.relevance,relevance:W.relevance},W.secondBest&&(p.secondBest={language:W.secondBest.language,relevance:W.secondBest.relevance}),Y("after:highlightElement",{el:p,result:W,text:N})}function h(p){s=bi(s,p)}const E=()=>{u(),lt("10.6.0","initHighlighting() deprecated. Use highlightAll() now.")};function O(){u(),lt("10.6.0","initHighlightingOnLoad() deprecated. Use highlightAll() now.")}let M=!1;function u(){if(document.readyState==="loading"){M=!0;return}document.querySelectorAll(s.cssSelector).forEach(S)}function I(){M&&u()}typeof window<"u"&&window.addEventListener&&window.addEventListener("DOMContentLoaded",I,!1);function m(p,y){let C=null;try{C=y(e)}catch(N){if(rt("Language definition for '{}' could not be registered.".replace("{}",p)),i)rt(N);else throw N;C=a}C.name||(C.name=p),t[p]=C,C.rawDefinition=y.bind(null,e),C.aliases&&H(C.aliases,{languageName:p})}function L(p){delete t[p];for(const y of Object.keys(n))n[y]===p&&delete n[y]}function K(){return Object.keys(t)}function P(p){return p=(p||"").toLowerCase(),t[p]||t[n[p]]}function H(p,{languageName:y}){typeof p=="string"&&(p=[p]),p.forEach(C=>{n[C.toLowerCase()]=y})}function q(p){const y=P(p);return y&&!y.disableAutodetect}function z(p){p["before:highlightBlock"]&&!p["before:highlightElement"]&&(p["before:highlightElement"]=y=>{p["before:highlightBlock"](Object.assign({block:y.el},y))}),p["after:highlightBlock"]&&!p["after:highlightElement"]&&(p["after:highlightElement"]=y=>{p["after:highlightBlock"](Object.assign({block:y.el},y))})}function k(p){z(p),r.push(p)}function U(p){const y=r.indexOf(p);y!==-1&&r.splice(y,1)}function Y(p,y){const C=p;r.forEach(function(N){N[C]&&N[C](y)})}function te(p){return lt("10.7.0","highlightBlock will be removed entirely in v12.0"),lt("10.7.0","Please use highlightElement now."),S(p)}Object.assign(e,{highlight:l,highlightAuto:_,highlightAll:u,highlightElement:S,highlightBlock:te,configure:h,initHighlighting:E,initHighlightingOnLoad:O,registerLanguage:m,unregisterLanguage:L,listLanguages:K,getLanguage:P,registerAliases:H,autoDetection:q,inherit:bi,addPlugin:k,removePlugin:U}),e.debugMode=function(){i=!1},e.safeMode=function(){i=!0},e.versionString=Ll,e.regex={concat:at,lookahead:Zo,either:Ir,optional:Yu,anyNumberOfTimes:Xu};for(const p in Qt)typeof Qt[p]=="object"&&Yo(Qt[p]);return Object.assign(e,Qt),e},bt=sa({});bt.newInstance=()=>sa({});var Bl=bt;bt.HighlightJS=bt;bt.default=bt;const xn=Ku(Bl);function $l(e){const t=e.regex,n=/(?![A-Za-z0-9])(?![$])/,r=t.concat(/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/,n),i=t.concat(/(\\?[A-Z][a-z0-9_\x7f-\xff]+|\\?[A-Z]+(?=[A-Z][a-z0-9_\x7f-\xff])){1,}/,n),o={scope:"variable",match:"\\$+"+r},a={scope:"meta",variants:[{begin:/<\?php/,relevance:10},{begin:/<\?=/},{begin:/<\?/,relevance:.1},{begin:/\?>/}]},s={scope:"subst",variants:[{begin:/\$\w+/},{begin:/\{\$/,end:/\}/}]},c=e.inherit(e.APOS_STRING_MODE,{illegal:null}),f=e.inherit(e.QUOTE_STRING_MODE,{illegal:null,contains:e.QUOTE_STRING_MODE.contains.concat(s)}),l={begin:/<<<[ \t]*(?:(\w+)|"(\w+)")\n/,end:/[ \t]*(\w+)\b/,contains:e.QUOTE_STRING_MODE.contains.concat(s),"on:begin":(k,U)=>{U.data._beginMatch=k[1]||k[2]},"on:end":(k,U)=>{U.data._beginMatch!==k[1]&&U.ignoreMatch()}},v=e.END_SAME_AS_BEGIN({begin:/<<<[ \t]*'(\w+)'\n/,end:/[ \t]*(\w+)\b/}),b=`[ ]`,_={scope:"string",variants:[f,c,l,v]},A={scope:"number",variants:[{begin:"\\b0[bB][01]+(?:_[01]+)*\\b"},{begin:"\\b0[oO][0-7]+(?:_[0-7]+)*\\b"},{begin:"\\b0[xX][\\da-fA-F]+(?:_[\\da-fA-F]+)*\\b"},{begin:"(?:\\b\\d+(?:_\\d+)*(\\.(?:\\d+(?:_\\d+)*))?|\\B\\.\\d+)(?:[eE][+-]?\\d+)?"}],relevance:0},S=["false","null","true"],h=["__CLASS__","__DIR__","__FILE__","__FUNCTION__","__COMPILER_HALT_OFFSET__","__LINE__","__METHOD__","__NAMESPACE__","__TRAIT__","die","echo","exit","include","include_once","print","require","require_once","array","abstract","and","as","binary","bool","boolean","break","callable","case","catch","class","clone","const","continue","declare","default","do","double","else","elseif","empty","enddeclare","endfor","endforeach","endif","endswitch","endwhile","enum","eval","extends","final","finally","float","for","foreach","from","global","goto","if","implements","instanceof","insteadof","int","integer","interface","isset","iterable","list","match|0","mixed","new","never","object","or","private","protected","public","readonly","real","return","string","switch","throw","trait","try","unset","use","var","void","while","xor","yield"],E=["Error|0","AppendIterator","ArgumentCountError","ArithmeticError","ArrayIterator","ArrayObject","AssertionError","BadFunctionCallException","BadMethodCallException","CachingIterator","CallbackFilterIterator","CompileError","Countable","DirectoryIterator","DivisionByZeroError","DomainException","EmptyIterator","ErrorException","Exception","FilesystemIterator","FilterIterator","GlobIterator","InfiniteIterator","InvalidArgumentException","IteratorIterator","LengthException","LimitIterator","LogicException","MultipleIterator","NoRewindIterator","OutOfBoundsException","OutOfRangeException","OuterIterator","OverflowException","ParentIterator","ParseError","RangeException","RecursiveArrayIterator","RecursiveCachingIterator","RecursiveCallbackFilterIterator","RecursiveDirectoryIterator","RecursiveFilterIterator","RecursiveIterator","RecursiveIteratorIterator","RecursiveRegexIterator","RecursiveTreeIterator","RegexIterator","RuntimeException","SeekableIterator","SplDoublyLinkedList","SplFileInfo","SplFileObject","SplFixedArray","SplHeap","SplMaxHeap","SplMinHeap","SplObjectStorage","SplObserver","SplPriorityQueue","SplQueue","SplStack","SplSubject","SplTempFileObject","TypeError","UnderflowException","UnexpectedValueException","UnhandledMatchError","ArrayAccess","BackedEnum","Closure","Fiber","Generator","Iterator","IteratorAggregate","Serializable","Stringable","Throwable","Traversable","UnitEnum","WeakReference","WeakMap","Directory","__PHP_Incomplete_Class","parent","php_user_filter","self","static","stdClass"],M={keyword:h,literal:(k=>{const U=[];return k.forEach(Y=>{U.push(Y),Y.toLowerCase()===Y?U.push(Y.toUpperCase()):U.push(Y.toLowerCase())}),U})(S),built_in:E},u=k=>k.map(U=>U.replace(/\|\d+$/,"")),I={variants:[{match:[/new/,t.concat(b,"+"),t.concat("(?!",u(E).join("\\b|"),"\\b)"),i],scope:{1:"keyword",4:"title.class"}}]},m=t.concat(r,"\\b(?!\\()"),L={variants:[{match:[t.concat(/::/,t.lookahead(/(?!class\b)/)),m],scope:{2:"variable.constant"}},{match:[/::/,/class/],scope:{2:"variable.language"}},{match:[i,t.concat(/::/,t.lookahead(/(?!class\b)/)),m],scope:{1:"title.class",3:"variable.constant"}},{match:[i,t.concat("::",t.lookahead(/(?!class\b)/))],scope:{1:"title.class"}},{match:[i,/::/,/class/],scope:{1:"title.class",3:"variable.language"}}]},K={scope:"attr",match:t.concat(r,t.lookahead(":"),t.lookahead(/(?!::)/))},P={relevance:0,begin:/\(/,end:/\)/,keywords:M,contains:[K,o,L,e.C_BLOCK_COMMENT_MODE,_,A,I]},H={relevance:0,match:[/\b/,t.concat("(?!fn\\b|function\\b|",u(h).join("\\b|"),"|",u(E).join("\\b|"),"\\b)"),r,t.concat(b,"*"),t.lookahead(/(?=\()/)],scope:{3:"title.function.invoke"},contains:[P]};P.contains.push(H);const q=[K,L,e.C_BLOCK_COMMENT_MODE,_,A,I],z={begin:t.concat(/#\[\s*/,i),beginScope:"meta",end:/]/,endScope:"meta",keywords:{literal:S,keyword:["new","array"]},contains:[{begin:/\[/,end:/]/,keywords:{literal:S,keyword:["new","array"]},contains:["self",...q]},...q,{scope:"meta",match:i}]};return{case_insensitive:!1,keywords:M,contains:[z,e.HASH_COMMENT_MODE,e.COMMENT("//","$"),e.COMMENT("/\\*","\\*/",{contains:[{scope:"doctag",match:"@[A-Za-z]+"}]}),{match:/__halt_compiler\(\);/,keywords:"__halt_compiler",starts:{scope:"comment",end:e.MATCH_NOTHING_RE,contains:[{match:/\?>/,scope:"meta",endsParent:!0}]}},a,{scope:"variable.language",match:/\$this\b/},o,H,L,{match:[/const/,/\s/,r],scope:{1:"keyword",3:"variable.constant"}},I,{scope:"function",relevance:0,beginKeywords:"fn function",end:/[;{]/,excludeEnd:!0,illegal:"[$%\\[]",contains:[{beginKeywords:"use"},e.UNDERSCORE_TITLE_MODE,{begin:"=>",endsParent:!0},{scope:"params",begin:"\\(",end:"\\)",excludeBegin:!0,excludeEnd:!0,keywords:M,contains:["self",o,L,e.C_BLOCK_COMMENT_MODE,_,A]}]},{scope:"class",variants:[{beginKeywords:"enum",illegal:/[($"]/},{beginKeywords:"class interface trait",illegal:/[:($"]/}],relevance:0,end:/\{/,excludeEnd:!0,contains:[{beginKeywords:"extends implements"},e.UNDERSCORE_TITLE_MODE]},{beginKeywords:"namespace",relevance:0,end:";",illegal:/[.']/,contains:[e.inherit(e.UNDERSCORE_TITLE_MODE,{scope:"title.class"})]},{beginKeywords:"use",relevance:0,end:";",contains:[{match:/\b(as|const|function)\b/,scope:"keyword"},e.UNDERSCORE_TITLE_MODE]},_,A]}}Wu.start();xn.registerLanguage("php",$l);window.hljs=xn;xn.highlightElement(document.querySelector(".default-highlightable-code"));document.querySelectorAll(".highlightable-code").forEach(e=>{e.dataset.highlighted!=="yes"&&xn.highlightElement(e)});jt("[data-tippy-content]",{trigger:"click",arrow:!0,theme:"material",animation:"scale"}); framework/src/Illuminate/Foundation/resources/exceptions/renderer/dist/styles.css 0000644 00000041736 15060132305 0024550 0 ustar 00 .tippy-box[data-animation=fade][data-state=hidden]{opacity:0}[data-tippy-root]{max-width:calc(100vw - 10px)}.tippy-box{position:relative;background-color:#333;color:#fff;border-radius:4px;font-size:14px;line-height:1.4;white-space:normal;outline:0;transition-property:transform,visibility,opacity}.tippy-box[data-placement^=top]>.tippy-arrow{bottom:0}.tippy-box[data-placement^=top]>.tippy-arrow:before{bottom:-7px;left:0;border-width:8px 8px 0;border-top-color:initial;transform-origin:center top}.tippy-box[data-placement^=bottom]>.tippy-arrow{top:0}.tippy-box[data-placement^=bottom]>.tippy-arrow:before{top:-7px;left:0;border-width:0 8px 8px;border-bottom-color:initial;transform-origin:center bottom}.tippy-box[data-placement^=left]>.tippy-arrow{right:0}.tippy-box[data-placement^=left]>.tippy-arrow:before{border-width:8px 0 8px 8px;border-left-color:initial;right:-7px;transform-origin:center left}.tippy-box[data-placement^=right]>.tippy-arrow{left:0}.tippy-box[data-placement^=right]>.tippy-arrow:before{left:-7px;border-width:8px 8px 8px 0;border-right-color:initial;transform-origin:center right}.tippy-box[data-inertia][data-state=visible]{transition-timing-function:cubic-bezier(.54,1.5,.38,1.11)}.tippy-arrow{width:16px;height:16px;color:#333}.tippy-arrow:before{content:"";position:absolute;border-color:transparent;border-style:solid}.tippy-content{position:relative;padding:5px 9px;z-index:1}.tippy-box[data-theme~=material]{background-color:#505355;font-weight:600}.tippy-box[data-theme~=material][data-placement^=top]>.tippy-arrow:before{border-top-color:#505355}.tippy-box[data-theme~=material][data-placement^=bottom]>.tippy-arrow:before{border-bottom-color:#505355}.tippy-box[data-theme~=material][data-placement^=left]>.tippy-arrow:before{border-left-color:#505355}.tippy-box[data-theme~=material][data-placement^=right]>.tippy-arrow:before{border-right-color:#505355}.tippy-box[data-theme~=material]>.tippy-backdrop{background-color:#505355}.tippy-box[data-theme~=material]>.tippy-svg-arrow{fill:#505355}.tippy-box[data-animation=scale][data-placement^=top]{transform-origin:bottom}.tippy-box[data-animation=scale][data-placement^=bottom]{transform-origin:top}.tippy-box[data-animation=scale][data-placement^=left]{transform-origin:right}.tippy-box[data-animation=scale][data-placement^=right]{transform-origin:left}.tippy-box[data-animation=scale][data-state=hidden]{transform:scale(.5);opacity:0}*,:before,:after{box-sizing:border-box;border-width:0;border-style:solid;border-color:#e5e7eb}:before,:after{--tw-content: ""}html,:host{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:Figtree,ui-sans-serif,system-ui,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji";font-feature-settings:normal;font-variation-settings:normal;-webkit-tap-highlight-color:transparent}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,samp,pre{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-feature-settings:normal;font-variation-settings:normal;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,input:where([type=button]),input:where([type=reset]),input:where([type=submit]){-webkit-appearance:button;background-color:transparent;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:baseline}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dl,dd,h1,h2,h3,h4,h5,h6,hr,figure,p,pre{margin:0}fieldset{margin:0;padding:0}legend{padding:0}ol,ul,menu{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}button,[role=button]{cursor:pointer}:disabled{cursor:default}img,svg,video,canvas,audio,iframe,embed,object{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,:before,:after{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:rgb(59 130 246 / .5);--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }::backdrop{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:rgb(59 130 246 / .5);--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }.container{width:100%}@media (min-width: 640px){.container{max-width:640px}}@media (min-width: 768px){.container{max-width:768px}}@media (min-width: 1024px){.container{max-width:1024px}}@media (min-width: 1280px){.container{max-width:1280px}}@media (min-width: 1536px){.container{max-width:1536px}}.absolute{position:absolute}.relative{position:relative}.right-0{right:0}.z-10{z-index:10}.mx-5{margin-left:1.25rem;margin-right:1.25rem}.mx-auto{margin-left:auto;margin-right:auto}.my-3{margin-top:.75rem;margin-bottom:.75rem}.-mt-2{margin-top:-.5rem}.mb-12{margin-bottom:3rem}.mb-2{margin-bottom:.5rem}.mb-3{margin-bottom:.75rem}.ml-1{margin-left:.25rem}.ml-3{margin-left:.75rem}.mt-1{margin-top:.25rem}.mt-2{margin-top:.5rem}.mt-3{margin-top:.75rem}.mt-4{margin-top:1rem}.mt-6{margin-top:1.5rem}.block{display:block}.inline-block{display:inline-block}.flex{display:flex}.inline-flex{display:inline-flex}.table{display:table}.grid{display:grid}.hidden{display:none}.h-4{height:1rem}.h-5{height:1.25rem}.h-6{height:1.5rem}.h-\[32\.5rem\]{height:32.5rem}.h-\[35\.5rem\]{height:35.5rem}.max-h-32{max-height:8rem}.w-4{width:1rem}.w-5{width:1.25rem}.w-6{width:1.5rem}.w-\[8rem\]{width:8rem}.w-full{width:100%}.min-w-0{min-width:0px}.flex-none{flex:none}.flex-grow{flex-grow:1}.origin-top-right{transform-origin:top right}.cursor-pointer{cursor:pointer}.grid-cols-1{grid-template-columns:repeat(1,minmax(0,1fr))}.flex-col{flex-direction:column}.items-center{align-items:center}.items-baseline{align-items:baseline}.justify-between{justify-content:space-between}.gap-2{gap:.5rem}.gap-3{gap:.75rem}.gap-6{gap:1.5rem}.space-y-2>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.5rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.5rem * var(--tw-space-y-reverse))}.overflow-x-auto{overflow-x:auto}.overflow-y-hidden{overflow-y:hidden}.overflow-x-scroll{overflow-x:scroll}.truncate{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.rounded{border-radius:.25rem}.rounded-full{border-radius:9999px}.rounded-lg{border-radius:.5rem}.rounded-md{border-radius:.375rem}.rounded-r-md{border-top-right-radius:.375rem;border-bottom-right-radius:.375rem}.border{border-width:1px}.border-l{border-left-width:1px}.border-l-2{border-left-width:2px}.border-r{border-right-width:1px}.border-t{border-top-width:1px}.border-transparent{border-color:transparent}.border-l-red-500{--tw-border-opacity:1;border-left-color:rgb(239 68 68 / var(--tw-border-opacity))}.bg-gray-100{--tw-bg-opacity:1;background-color:rgb(243 244 246 / var(--tw-bg-opacity))}.bg-gray-200{--tw-bg-opacity:1;background-color:rgb(229 231 235 / var(--tw-bg-opacity))}.bg-gray-200\/80{background-color:#e5e7ebcc}.bg-red-500\/20{background-color:#ef444433}.bg-white{--tw-bg-opacity:1;background-color:rgb(255 255 255 / var(--tw-bg-opacity))}.fill-red-500{fill:#ef4444}.p-1{padding:.25rem}.p-2{padding:.5rem}.p-4{padding:1rem}.p-6{padding:1.5rem}.px-3{padding-left:.75rem;padding-right:.75rem}.px-4{padding-left:1rem;padding-right:1rem}.px-5{padding-left:1.25rem;padding-right:1.25rem}.px-6{padding-left:1.5rem;padding-right:1.5rem}.py-2{padding-top:.5rem;padding-bottom:.5rem}.py-3{padding-top:.75rem;padding-bottom:.75rem}.pb-12{padding-bottom:3rem}.pt-4{padding-top:1rem}.pt-6{padding-top:1.5rem}.text-left{text-align:left}.text-right{text-align:right}.font-mono{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace}.font-sans{font-family:Figtree,ui-sans-serif,system-ui,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji"}.text-2xl{font-size:1.5rem;line-height:2rem}.text-lg{font-size:1.125rem;line-height:1.75rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xl{font-size:1.25rem;line-height:1.75rem}.text-xs{font-size:.75rem;line-height:1rem}.font-bold{font-weight:700}.font-semibold{font-weight:600}.leading-5{line-height:1.25rem}.text-blue-500{--tw-text-opacity:1;color:rgb(59 130 246 / var(--tw-text-opacity))}.text-gray-400{--tw-text-opacity:1;color:rgb(156 163 175 / var(--tw-text-opacity))}.text-gray-50{--tw-text-opacity:1;color:rgb(249 250 251 / var(--tw-text-opacity))}.text-gray-500{--tw-text-opacity:1;color:rgb(107 114 128 / var(--tw-text-opacity))}.text-gray-700{--tw-text-opacity:1;color:rgb(55 65 81 / var(--tw-text-opacity))}.text-gray-900{--tw-text-opacity:1;color:rgb(17 24 39 / var(--tw-text-opacity))}.text-red-500{--tw-text-opacity:1;color:rgb(239 68 68 / var(--tw-text-opacity))}.antialiased{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.shadow-xl{--tw-shadow:0 20px 25px -5px rgb(0 0 0 / .1), 0 8px 10px -6px rgb(0 0 0 / .1);--tw-shadow-colored:0 20px 25px -5px var(--tw-shadow-color), 0 8px 10px -6px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000),var(--tw-ring-shadow, 0 0 #0000),var(--tw-shadow)}.ring-1{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow, 0 0 #0000)}.ring-gray-900\/5{--tw-ring-color:rgb(17 24 39 / .05)}.filter{filter:var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow)}[x-cloak]{display:none}html{-moz-tab-size:4;-o-tab-size:4;tab-size:4}table.hljs-ln{color:inherit;font-size:inherit;border-spacing:2px}pre code.hljs{background:none;padding:.5em 0 0;width:100%}.hljs-ln-line{white-space-collapse:preserve;text-wrap:nowrap}.trace{-webkit-mask-image:linear-gradient(180deg,#000 calc(100% - 4rem),transparent)}.scrollbar-hidden{-ms-overflow-style:none;scrollbar-width:none;overflow-x:scroll}.scrollbar-hidden::-webkit-scrollbar{-webkit-appearance:none;width:0;height:0}.hljs-ln .hljs-ln-numbers{padding:5px;border-right-color:transparent;margin-right:5px}.hljs-ln-n{width:50px}.hljs-ln-numbers{-webkit-touch-callout:none;-webkit-user-select:none;-moz-user-select:none;user-select:none;text-align:center;border-right:1px solid #ccc;vertical-align:top;padding-right:5px}.hljs-ln-code{width:100%;padding-left:10px;padding-right:10px}.hljs-ln-code:hover{background-color:#ef444433}.default\:col-span-full:default{grid-column:1 / -1}.default\:row-span-1:default{grid-row:span 1 / span 1}.hover\:bg-gray-100:hover{--tw-bg-opacity:1;background-color:rgb(243 244 246 / var(--tw-bg-opacity))}.hover\:bg-gray-100\/75:hover{background-color:#f3f4f6bf}.hover\:text-gray-500:hover{--tw-text-opacity:1;color:rgb(107 114 128 / var(--tw-text-opacity))}.hover\:underline:hover{text-decoration-line:underline}.focus\:text-gray-500:focus{--tw-text-opacity:1;color:rgb(107 114 128 / var(--tw-text-opacity))}.dark\:block:is(.dark *){display:block}.dark\:hidden:is(.dark *){display:none}.dark\:border:is(.dark *){border-width:1px}.dark\:border-gray-700:is(.dark *){--tw-border-opacity:1;border-color:rgb(55 65 81 / var(--tw-border-opacity))}.dark\:border-gray-800:is(.dark *){--tw-border-opacity:1;border-color:rgb(31 41 55 / var(--tw-border-opacity))}.dark\:border-gray-900:is(.dark *){--tw-border-opacity:1;border-color:rgb(17 24 39 / var(--tw-border-opacity))}.dark\:border-l-red-500:is(.dark *){--tw-border-opacity:1;border-left-color:rgb(239 68 68 / var(--tw-border-opacity))}.dark\:bg-gray-800:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55 / var(--tw-bg-opacity))}.dark\:bg-gray-900\/80:is(.dark *){background-color:#111827cc}.dark\:bg-gray-950\/95:is(.dark *){background-color:#030712f2}.dark\:bg-red-500\/20:is(.dark *){background-color:#ef444433}.dark\:text-gray-100:is(.dark *){--tw-text-opacity:1;color:rgb(243 244 246 / var(--tw-text-opacity))}.dark\:text-gray-300:is(.dark *){--tw-text-opacity:1;color:rgb(209 213 219 / var(--tw-text-opacity))}.dark\:text-gray-400:is(.dark *){--tw-text-opacity:1;color:rgb(156 163 175 / var(--tw-text-opacity))}.dark\:text-gray-600:is(.dark *){--tw-text-opacity:1;color:rgb(75 85 99 / var(--tw-text-opacity))}.dark\:text-gray-950:is(.dark *){--tw-text-opacity:1;color:rgb(3 7 18 / var(--tw-text-opacity))}.dark\:text-white:is(.dark *){--tw-text-opacity:1;color:rgb(255 255 255 / var(--tw-text-opacity))}.dark\:ring-1:is(.dark *){--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow, 0 0 #0000)}.dark\:ring-gray-800:is(.dark *){--tw-ring-opacity:1;--tw-ring-color:rgb(31 41 55 / var(--tw-ring-opacity))}.dark\:hover\:bg-gray-700:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(55 65 81 / var(--tw-bg-opacity))}.dark\:hover\:bg-gray-800:hover:is(.dark *){--tw-bg-opacity:1;background-color:rgb(31 41 55 / var(--tw-bg-opacity))}.dark\:hover\:bg-gray-800\/75:hover:is(.dark *){background-color:#1f2937bf}.dark\:hover\:text-gray-500:hover:is(.dark *){--tw-text-opacity:1;color:rgb(107 114 128 / var(--tw-text-opacity))}.dark\:focus\:text-gray-500:focus:is(.dark *){--tw-text-opacity:1;color:rgb(107 114 128 / var(--tw-text-opacity))}@media (min-width: 640px){.sm\:col-span-1{grid-column:span 1 / span 1}.sm\:col-span-2{grid-column:span 2 / span 2}.sm\:mt-10{margin-top:2.5rem}.sm\:gap-6{gap:1.5rem}.sm\:p-12{padding:3rem}.sm\:py-5{padding-top:1.25rem;padding-bottom:1.25rem}.sm\:text-3xl{font-size:1.875rem;line-height:2.25rem}}@media (min-width: 768px){.md\:block{display:block}.md\:inline-block{display:inline-block}.md\:flex{display:flex}.md\:hidden{display:none}.md\:min-w-64{min-width:16rem}.md\:items-center{align-items:center}.md\:justify-between{justify-content:space-between}.md\:gap-2{gap:.5rem}}@media (min-width: 1024px){.lg\:block{display:block}.lg\:inline-block{display:inline-block}.lg\:w-\[12rem\]{width:12rem}.lg\:grid-cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}.lg\:px-8{padding-left:2rem;padding-right:2rem}.lg\:text-2xl{font-size:1.5rem;line-height:2rem}.lg\:text-base{font-size:1rem;line-height:1.5rem}.lg\:text-sm{font-size:.875rem;line-height:1.25rem}.default\:lg\:col-span-6:default{grid-column:span 6 / span 6}} framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/header.blade.php 0000644 00000002537 15060132305 0026740 0 ustar 00 <x-laravel-exceptions-renderer::card> <div class="md:flex md:items-center md:justify-between md:gap-2"> <div> <div class="inline-block rounded-full bg-red-500/20 px-3 py-2 dark:bg-red-500/20"> <span class="hidden text-sm font-bold leading-5 text-red-500 md:inline-block lg:text-base"> {{ $exception->class() }} </span> <span class="text-sm font-bold leading-5 text-red-500 md:hidden lg:text-base"> {{ implode(' ', array_slice(explode('\\', $exception->class()), -1)) }} </span> </div> <div class="mt-4 text-lg font-semibold text-gray-900 dark:text-white lg:text-2xl">{{ $exception->message() }}</div> </div> <div class="hidden text-right md:block md:min-w-64"> <div> <span class="rounded-full bg-gray-200 px-3 py-2 text-sm leading-5 text-gray-900 dark:bg-gray-800 dark:text-white"> {{ $exception->request()->method() }} {{ $exception->request()->httpHost() }} </span> </div> <div class="mt-4 px-4"> <span class="text-sm text-gray-500 dark:text-gray-400">PHP {{ PHP_VERSION }} — Laravel {{ app()->version() }}</span> </div> </div> </div> </x-laravel-exceptions-renderer::card> framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/layout.blade.php 0000644 00000015167 15060132305 0027030 0 ustar 00 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>{{ config('app.name', 'Laravel') }}</title> <link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg viewBox='0 -.11376601 49.74245785 51.31690859' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='m49.626 11.564a.809.809 0 0 1 .028.209v10.972a.8.8 0 0 1 -.402.694l-9.209 5.302v10.509c0 .286-.152.55-.4.694l-19.223 11.066c-.044.025-.092.041-.14.058-.018.006-.035.017-.054.022a.805.805 0 0 1 -.41 0c-.022-.006-.042-.018-.063-.026-.044-.016-.09-.03-.132-.054l-19.219-11.066a.801.801 0 0 1 -.402-.694v-32.916c0-.072.01-.142.028-.21.006-.023.02-.044.028-.067.015-.042.029-.085.051-.124.015-.026.037-.047.055-.071.023-.032.044-.065.071-.093.023-.023.053-.04.079-.06.029-.024.055-.05.088-.069h.001l9.61-5.533a.802.802 0 0 1 .8 0l9.61 5.533h.002c.032.02.059.045.088.068.026.02.055.038.078.06.028.029.048.062.072.094.017.024.04.045.054.071.023.04.036.082.052.124.008.023.022.044.028.068a.809.809 0 0 1 .028.209v20.559l8.008-4.611v-10.51c0-.07.01-.141.028-.208.007-.024.02-.045.028-.068.016-.042.03-.085.052-.124.015-.026.037-.047.054-.071.024-.032.044-.065.072-.093.023-.023.052-.04.078-.06.03-.024.056-.05.088-.069h.001l9.611-5.533a.801.801 0 0 1 .8 0l9.61 5.533c.034.02.06.045.09.068.025.02.054.038.077.06.028.029.048.062.072.094.018.024.04.045.054.071.023.039.036.082.052.124.009.023.022.044.028.068zm-1.574 10.718v-9.124l-3.363 1.936-4.646 2.675v9.124l8.01-4.611zm-9.61 16.505v-9.13l-4.57 2.61-13.05 7.448v9.216zm-36.84-31.068v31.068l17.618 10.143v-9.214l-9.204-5.209-.003-.002-.004-.002c-.031-.018-.057-.044-.086-.066-.025-.02-.054-.036-.076-.058l-.002-.003c-.026-.025-.044-.056-.066-.084-.02-.027-.044-.05-.06-.078l-.001-.003c-.018-.03-.029-.066-.042-.1-.013-.03-.03-.058-.038-.09v-.001c-.01-.038-.012-.078-.016-.117-.004-.03-.012-.06-.012-.09v-21.483l-4.645-2.676-3.363-1.934zm8.81-5.994-8.007 4.609 8.005 4.609 8.006-4.61-8.006-4.608zm4.164 28.764 4.645-2.674v-20.096l-3.363 1.936-4.646 2.675v20.096zm24.667-23.325-8.006 4.609 8.006 4.609 8.005-4.61zm-.801 10.605-4.646-2.675-3.363-1.936v9.124l4.645 2.674 3.364 1.937zm-18.422 20.561 11.743-6.704 5.87-3.35-8-4.606-9.211 5.303-8.395 4.833z' fill='%23ff2d20'/%3E%3C/svg%3E" /> <link href="https://fonts.bunny.net/css?family=figtree:300,400,500,600" rel="stylesheet" /> {!! Illuminate\Foundation\Exceptions\Renderer\Renderer::css() !!} <style type="text/css"> @foreach ($exception->frames() as $frame) #frame-{{ $loop->index }} .hljs-ln-line[data-line-number='{{ $frame->line() }}'] { background-color: rgba(242, 95, 95, 0.4); } @endforeach </style> </head> <body class="bg-gray-200/80 font-sans antialiased dark:bg-gray-950/95"> {{ $slot }} {!! Illuminate\Foundation\Exceptions\Renderer\Renderer::js() !!} <script type="text/javascript"> !function(r,o){"use strict";var e,i="hljs-ln",l="hljs-ln-line",h="hljs-ln-code",s="hljs-ln-numbers",c="hljs-ln-n",m="data-line-number",a=/\r\n|\r|\n/g;function u(e){for(var n=e.toString(),t=e.anchorNode;"TD"!==t.nodeName;)t=t.parentNode;for(var r=e.focusNode;"TD"!==r.nodeName;)r=r.parentNode;var o=parseInt(t.dataset.lineNumber),a=parseInt(r.dataset.lineNumber);if(o==a)return n;var i,l=t.textContent,s=r.textContent;for(a<o&&(i=o,o=a,a=i,i=l,l=s,s=i);0!==n.indexOf(l);)l=l.slice(1);for(;-1===n.lastIndexOf(s);)s=s.slice(0,-1);for(var c=l,u=function(e){for(var n=e;"TABLE"!==n.nodeName;)n=n.parentNode;return n}(t),d=o+1;d<a;++d){var f=p('.{0}[{1}="{2}"]',[h,m,d]);c+="\n"+u.querySelector(f).textContent}return c+="\n"+s}function n(e){try{var n=o.querySelectorAll("code.hljs,code.nohighlight");for(var t in n)n.hasOwnProperty(t)&&(n[t].classList.contains("nohljsln")||d(n[t],e))}catch(e){r.console.error("LineNumbers error: ",e)}}function d(e,n){"object"==typeof e&&r.setTimeout(function(){e.innerHTML=f(e,n)},0)}function f(e,n){var t,r,o=(t=e,{singleLine:function(e){return!!e.singleLine&&e.singleLine}(r=(r=n)||{}),startFrom:function(e,n){var t=1;isFinite(n.startFrom)&&(t=n.startFrom);var r=function(e,n){return e.hasAttribute(n)?e.getAttribute(n):null}(e,"data-ln-start-from");return null!==r&&(t=function(e,n){if(!e)return n;var t=Number(e);return isFinite(t)?t:n}(r,1)),t}(t,r)});return function e(n){var t=n.childNodes;for(var r in t){var o;t.hasOwnProperty(r)&&(o=t[r],0<(o.textContent.trim().match(a)||[]).length&&(0<o.childNodes.length?e(o):v(o.parentNode)))}}(e),function(e,n){var t=g(e);""===t[t.length-1].trim()&&t.pop();if(1<t.length||n.singleLine){for(var r="",o=0,a=t.length;o<a;o++)r+=p('<tr><td class="{0} {1}" {3}="{5}"><div class="{2}" {3}="{5}"></div></td><td class="{0} {4}" {3}="{5}">{6}</td></tr>',[l,s,c,m,h,o+n.startFrom,0<t[o].length?t[o]:" "]);return p('<table class="{0}">{1}</table>',[i,r])}return e}(e.innerHTML,o)}function v(e){var n=e.className;if(/hljs-/.test(n)){for(var t=g(e.innerHTML),r=0,o="";r<t.length;r++){o+=p('<span class="{0}">{1}</span>\n',[n,0<t[r].length?t[r]:" "])}e.innerHTML=o.trim()}}function g(e){return 0===e.length?[]:e.split(a)}function p(e,t){return e.replace(/\{(\d+)\}/g,function(e,n){return void 0!==t[n]?t[n]:e})}r.hljs?(r.hljs.initLineNumbersOnLoad=function(e){"interactive"===o.readyState||"complete"===o.readyState?n(e):r.addEventListener("DOMContentLoaded",function(){n(e)})},r.hljs.lineNumbersBlock=d,r.hljs.lineNumbersValue=function(e,n){if("string"!=typeof e)return;var t=document.createElement("code");return t.innerHTML=e,f(t,n)},(e=o.createElement("style")).type="text/css",e.innerHTML=p(".{0}{border-collapse:collapse}.{0} td{padding:0}.{1}:before{content:attr({2})}",[i,c,m]),o.getElementsByTagName("head")[0].appendChild(e)):r.console.error("highlight.js not detected!"),document.addEventListener("copy",function(e){var n,t=window.getSelection();!function(e){for(var n=e;n;){if(n.className&&-1!==n.className.indexOf("hljs-ln-code"))return 1;n=n.parentNode}}(t.anchorNode)||(n=-1!==window.navigator.userAgent.indexOf("Edge")?u(t):t.toString(),e.clipboardData.setData("text/plain",n),e.preventDefault())})}(window,document); hljs.initLineNumbersOnLoad() window.addEventListener('load', function() { document.querySelectorAll('.renderer').forEach(function(element, index) { if (index > 0) { element.remove(); } }); document.querySelector('.default-highlightable-code').style.display = 'block'; document.querySelectorAll('.highlightable-code').forEach(function(element) { element.style.display = 'block'; }) }); </script> </body> </html> framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/editor.blade.php 0000644 00000003004 15060132305 0026764 0 ustar 00 @foreach ($exception->frames() as $frame) <div class="sm:col-span-2" x-show="index === {{ $loop->index }}" > <div class="mb-3"> <div class="text-md text-gray-500 dark:text-gray-400"> <div class="mb-2"> @if (config('app.editor')) <a href="{{ $frame->editorHref() }}" class="text-blue-500 hover:underline"> <span class="wrap text-gray-900 dark:text-gray-300">{{ $frame->file() }}</span> </a> @else <span class="wrap text-gray-900 dark:text-gray-300">{{ $frame->file() }}</span> @endif <span class="font-mono text-xs">:{{ $frame->line() }}</span> </div> </div> </div> <div class="pt-4 text-sm text-gray-500 dark:text-gray-400"> <pre class="h-[32.5rem] rounded-md dark:bg-gray-800 border dark:border-gray-700"><template x-if="true"><code style="display: none;" id="frame-{{ $loop->index }}" class="language-php highlightable-code @if($loop->index === $exception->defaultFrame()) default-highlightable-code @endif scrollbar-hidden overflow-y-hidden" data-line-number="{{ $frame->line() }}" data-ln-start-from="{{ max($frame->line() - 5, 1) }}" >{{ $frame->snippet() }}</code></template></pre> </div> </div> @endforeach framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/context.blade.php 0000644 00000016603 15060132305 0027173 0 ustar 00 <x-laravel-exceptions-renderer::card class="mt-6 overflow-x-auto"> <div> <span class="text-xl font-bold lg:text-2xl">Request</span> </div> <div class="mt-2"> <span>{{ $exception->request()->method() }}</span> <span class="text-gray-500">{{ $exception->request()->httpHost() }}</span> </div> <div class="mt-4"> <span class="font-semibold text-gray-900 dark:text-white">Headers</span> </div> <dl class="mt-1 grid grid-cols-1 rounded border dark:border-gray-800"> @forelse ($exception->requestHeaders() as $key => $value) <div class="flex items-center gap-2 {{ $loop->first ? '' : 'border-t' }} dark:border-gray-800"> <span data-tippy-content="{{ $key }}" class="lg:text-md w-[8rem] flex-none cursor-pointer truncate border-r px-5 py-3 text-sm dark:border-gray-800 lg:w-[12rem]" > {{ $key }} </span> <span class="min-w-0 flex-grow" style=" -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem)); " > <pre class="scrollbar-hidden overflow-y-hidden text-xs lg:text-sm"><code class="px-5 py-3 overflow-y-hidden scrollbar-hidden max-h-32 overflow-x-scroll scrollbar-hidden-x">{{ $value }}</code></pre> </span> </div> @empty <span class="min-w-0 flex-grow" style="-webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem))" > <pre class="scrollbar-hidden mx-5 my-3 overflow-y-hidden text-xs lg:text-sm"><code class="overflow-y-hidden scrollbar-hidden overflow-x-scroll scrollbar-hidden-x">No headers data</code></pre> </span> @endforelse </dl> <div class="mt-4"> <span class="font-semibold text-gray-900 dark:text-white">Body</span> </div> <div class="mt-1 rounded border dark:border-gray-800"> <div class="flex items-center"> <span class="min-w-0 flex-grow" style="-webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem))" > <pre class="scrollbar-hidden mx-5 my-3 overflow-y-hidden text-xs lg:text-sm"><code class="overflow-y-hidden scrollbar-hidden overflow-x-scroll scrollbar-hidden-x">{!! $exception->requestBody() ?: 'No body data' !!}</code></pre> </span> </div> </div> </x-laravel-exceptions-renderer::card> <x-laravel-exceptions-renderer::card class="mt-6 overflow-x-auto"> <div> <span class="text-xl font-bold lg:text-2xl">Application</span> </div> <div class="mt-4"> <span class="font-semibold text-gray-900 dark:text-white"> Routing </span> </div> <dl class="mt-1 grid grid-cols-1 rounded border dark:border-gray-800"> @forelse ($exception->applicationRouteContext() as $name => $value) <div class="flex items-center gap-2 {{ $loop->first ? '' : 'border-t' }} dark:border-gray-800"> <span data-tippy-content="{{ $name }}" class="lg:text-md w-[8rem] flex-none cursor-pointer truncate border-r px-5 py-3 text-sm dark:border-gray-800 lg:w-[12rem]" >{{ $name }}</span > <span class="min-w-0 flex-grow" style=" -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem)); " > <pre class="scrollbar-hidden overflow-y-hidden text-xs lg:text-sm"><code class="px-5 py-3 overflow-y-hidden scrollbar-hidden max-h-32 overflow-x-scroll scrollbar-hidden-x">{{ $value }}</code></pre> </span> </div> @empty <span class="min-w-0 flex-grow" style="-webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem))" > <pre class="scrollbar-hidden mx-5 my-3 overflow-y-hidden text-xs lg:text-sm"><code class="overflow-y-hidden scrollbar-hidden overflow-x-scroll scrollbar-hidden-x">No routing data</code></pre> </span> @endforelse </dl> @if ($routeParametersContext = $exception->applicationRouteParametersContext()) <div class="mt-4"> <span class="text-gray-900 dark:text-white text-sm"> Routing Parameters </span> </div> <div class="mt-1 rounded border dark:border-gray-800"> <div class="flex items-center"> <span class="min-w-0 flex-grow" style="-webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem))" > <pre class="scrollbar-hidden mx-5 my-3 overflow-y-hidden text-xs lg:text-sm"><code class="overflow-y-hidden scrollbar-hidden overflow-x-scroll scrollbar-hidden-x">{!! $routeParametersContext !!}</code></pre> </span> </div> </div> @endif <div class="mt-4"> <span class="font-semibold text-gray-900 dark:text-white"> Database Queries </span> <span class="text-xs text-gray-500 dark:text-gray-400"> @if (count($exception->applicationQueries()) === 100) only the first 100 queries are displayed @endif </span> </div> <dl class="mt-1 grid grid-cols-1 rounded border dark:border-gray-800"> @forelse ($exception->applicationQueries() as ['connectionName' => $connectionName, 'sql' => $sql, 'time' => $time]) <div class="flex items-center gap-2 {{ $loop->first ? '' : 'border-t' }} dark:border-gray-800"> <div class="lg:text-md w-[8rem] flex-none truncate border-r px-5 py-3 text-sm dark:border-gray-800 lg:w-[12rem]"> <span>{{ $connectionName }}</span> <span class="hidden text-xs text-gray-500 lg:inline-block">({{ $time }} ms)</span> </div> <span class="min-w-0 flex-grow" style=" -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem)); " > <pre class="scrollbar-hidden overflow-y-hidden text-xs lg:text-sm"><code class="px-5 py-3 overflow-y-hidden scrollbar-hidden max-h-32 overflow-x-scroll scrollbar-hidden-x">{{ $sql }}</code></pre> </span> </div> @empty <span class="min-w-0 flex-grow" style="-webkit-mask-image: linear-gradient(90deg, transparent 0, #000 1rem, #000 calc(100% - 3rem), transparent calc(100% - 1rem))" > <pre class="scrollbar-hidden mx-5 my-3 overflow-y-hidden text-xs lg:text-sm"><code class="overflow-y-hidden scrollbar-hidden overflow-x-scroll scrollbar-hidden-x">No query data</code></pre> </span> @endforelse </dl> </x-laravel-exceptions-renderer::card> framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/icons/sun.blade.php 0000644 00000000646 15060132305 0027427 0 ustar 00 <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes }} > <path stroke-linecap="round" stroke-linejoin="round" d="M12 3v2.25m6.364.386l-1.591 1.591M21 12h-2.25m-.386 6.364l-1.591-1.591M12 18.75V21m-4.773-4.227l-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0z" /> </svg> src/Illuminate/Foundation/resources/exceptions/renderer/components/icons/computer-desktop.blade.php 0000644 00000000744 15060132305 0032047 0 ustar 00 framework <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes }} > <path stroke-linecap="round" stroke-linejoin="round" d="M9 17.25v1.007a3 3 0 01-.879 2.122L7.5 21h9l-.621-.621A3 3 0 0115 18.257V17.25m6-12V15a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 15V5.25m18 0A2.25 2.25 0 0018.75 3H5.25A2.25 2.25 0 003 5.25m18 0V12a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 12V5.25" /> </svg> src/Illuminate/Foundation/resources/exceptions/renderer/components/icons/chevron-down.blade.php 0000644 00000000505 15060132305 0031146 0 ustar 00 framework <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-4 h-4" style="margin-bottom: -8px;"> <path fill-rule="evenodd" d="M5.22 8.22a.75.75 0 0 1 1.06 0L10 11.94l3.72-3.72a.75.75 0 1 1 1.06 1.06l-4.25 4.25a.75.75 0 0 1-1.06 0L5.22 9.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd" /> </svg> framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/icons/moon.blade.php 0000644 00000000625 15060132305 0027567 0 ustar 00 <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" {{ $attributes }} > <path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.718 9.718 0 0118 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 003 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 009.002-5.998z" /> </svg> src/Illuminate/Foundation/resources/exceptions/renderer/components/icons/chevron-up.blade.php 0000644 00000000461 15060132305 0030624 0 ustar 00 framework <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-4 h-4" style="margin-bottom: -8px;"> <path fill-rule="evenodd" d="M9.47 6.47a.75.75 0 0 1 1.06 0l4.25 4.25a.75.75 0 1 1-1.06 1.06L10 8.06l-3.72 3.72a.75.75 0 0 1-1.06-1.06l4.25-4.25Z" clip-rule="evenodd" /> </svg> framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/card.blade.php 0000644 00000000451 15060132305 0026412 0 ustar 00 <section {{ $attributes->merge(['class' => "@container flex flex-col p-6 sm:p-12 bg-white dark:bg-gray-900/80 text-gray-900 dark:text-gray-100 rounded-lg default:col-span-full default:lg:col-span-6 default:row-span-1 dark:ring-1 dark:ring-gray-800 shadow-xl"]) }} > {{ $slot }} </section> framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/navigation.blade.php 0000644 00000002262 15060132305 0027642 0 ustar 00 <header class="mt-3 px-5 sm:mt-10"> <div class="py-3 dark:border-gray-900 sm:py-5"> <div class="flex items-center justify-between"> <div class="flex items-center"> <div class="rounded-full bg-red-500/20 p-4 dark:bg-red-500/20"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="h-6 w-6 fill-red-500 text-gray-50 dark:text-gray-950" > <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m9-.75a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9 3.75h.008v.008H12v-.008Z" /> </svg> </div> <span class="text-dark ml-3 text-2xl font-bold dark:text-white sm:text-3xl"> {{ $exception->title() }} </span> </div> <div class="flex items-center gap-3 sm:gap-6"> <x-laravel-exceptions-renderer::theme-switcher /> </div> </div> </div> </header> framework/src/Illuminate/Foundation/resources/exceptions/renderer/components/trace.blade.php 0000644 00000010365 15060132305 0026604 0 ustar 00 <div class="hidden overflow-x-auto sm:col-span-1 lg:block"> <div class="h-[35.5rem] scrollbar-hidden trace text-sm text-gray-400 dark:text-gray-300" > <div class="mb-2 inline-block rounded-full bg-red-500/20 px-3 py-2 dark:bg-red-500/20 sm:col-span-1"> <button @click="includeVendorFrames = !includeVendorFrames" class="inline-flex items-center font-bold leading-5 text-red-500" > <span x-show="includeVendorFrames">Collapse</span> <span x-cloak x-show="!includeVendorFrames" >Expand</span > <span class="ml-1">vendor frames</span> <div class="flex flex-col ml-1 -mt-2" x-cloak x-show="includeVendorFrames"> <x-laravel-exceptions-renderer::icons.chevron-down /> <x-laravel-exceptions-renderer::icons.chevron-up /> </div> <div class="flex flex-col ml-1 -mt-2" x-cloak x-show="! includeVendorFrames"> <x-laravel-exceptions-renderer::icons.chevron-up /> <x-laravel-exceptions-renderer::icons.chevron-down /> </div> </button> </div> <div class="mb-12 space-y-2"> @foreach ($exception->frames() as $frame) @if (! $frame->isFromVendor()) @php $vendorFramesCollapsed = $exception->frames()->take($loop->index)->reverse()->takeUntil(fn ($frame) => ! $frame->isFromVendor()); @endphp <div x-show="! includeVendorFrames"> @if ($vendorFramesCollapsed->isNotEmpty()) <div class="text-gray-500"> {{ $vendorFramesCollapsed->count() }} vendor frame{{ $vendorFramesCollapsed->count() > 1 ? 's' : '' }} collapsed </div> @endif </div> @endif <button class="w-full text-left dark:border-gray-900" x-show="{{ $frame->isFromVendor() ? 'includeVendorFrames' : 'true' }}" @click="index = {{ $loop->index }}" > <div x-bind:class=" index === {{ $loop->index }} ? 'rounded-r-md bg-gray-100 dark:bg-gray-800 border-l dark:border dark:border-gray-700 border-l-red-500 dark:border-l-red-500' : 'hover:bg-gray-100/75 dark:hover:bg-gray-800/75' " > <div class="scrollbar-hidden overflow-x-auto border-l-2 border-transparent p-2"> <div class="nowrap text-gray-900 dark:text-gray-300"> <span class="inline-flex items-baseline"> <span class="text-gray-900 dark:text-gray-300">{{ $frame->source() }}</span> <span class="font-mono text-xs">:{{ $frame->line() }}</span> </span> </div> <div class="text-gray-500 dark:text-gray-400"> {{ $exception->frames()->get($loop->index + 1)?->callable() }} </div> </div> </div> </button> @if (! $frame->isFromVendor() && $exception->frames()->slice($loop->index + 1)->filter(fn ($frame) => ! $frame->isFromVendor())->isEmpty()) @if ($exception->frames()->slice($loop->index + 1)->count()) <div x-show="! includeVendorFrames"> <div class="text-gray-500"> {{ $exception->frames()->slice($loop->index + 1)->count() }} vendor frame{{ $exception->frames()->slice($loop->index + 1)->count() > 1 ? 's' : '' }} collapsed </div> </div> @endif @endif @endforeach </div> </div> </div> src/Illuminate/Foundation/resources/exceptions/renderer/components/theme-switcher.blade.php 0000644 00000007121 15060132305 0030353 0 ustar 00 framework <script> (function () { const darkStyles = document.querySelector('style[data-theme="dark"]')?.textContent const lightStyles = document.querySelector('style[data-theme="light"]')?.textContent const removeStyles = () => { document.querySelector('style[data-theme="dark"]')?.remove() document.querySelector('style[data-theme="light"]')?.remove() } removeStyles() setDarkClass = () => { removeStyles() const isDark = localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches) isDark ? document.documentElement.classList.add('dark') : document.documentElement.classList.remove('dark') if (isDark) { document.head.insertAdjacentHTML('beforeend', `<style data-theme="dark">${darkStyles}</style>`) } else { document.head.insertAdjacentHTML('beforeend', `<style data-theme="light">${lightStyles}</style>`) } } setDarkClass() window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', setDarkClass) })(); </script> <div class="relative" x-data="{ menu: false, theme: localStorage.theme, darkMode() { this.theme = 'dark' localStorage.theme = 'dark' setDarkClass() }, lightMode() { this.theme = 'light' localStorage.theme = 'light' setDarkClass() }, systemMode() { this.theme = undefined localStorage.removeItem('theme') setDarkClass() }, }" @click.outside="menu = false" > <button x-cloak class="block rounded p-1 hover:bg-gray-100 dark:hover:bg-gray-800" :class="theme ? 'text-gray-700 dark:text-gray-300' : 'text-gray-400 dark:text-gray-600 hover:text-gray-500 focus:text-gray-500 dark:hover:text-gray-500 dark:focus:text-gray-500'" @click="menu = ! menu" > <x-laravel-exceptions-renderer::icons.sun class="block h-5 w-5 dark:hidden" /> <x-laravel-exceptions-renderer::icons.moon class="hidden h-5 w-5 dark:block" /> </button> <div x-show="menu" class="absolute right-0 z-10 flex origin-top-right flex-col rounded-md bg-white shadow-xl ring-1 ring-gray-900/5 dark:bg-gray-800" style="display: none" @click="menu = false" > <button class="flex items-center gap-3 px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-700" :class="theme === 'light' ? 'text-gray-900 dark:text-gray-100' : 'text-gray-500 dark:text-gray-400'" @click="lightMode()" > <x-laravel-exceptions-renderer::icons.sun class="h-5 w-5" /> Light </button> <button class="flex items-center gap-3 px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-700" :class="theme === 'dark' ? 'text-gray-900 dark:text-gray-100' : 'text-gray-500 dark:text-gray-400'" @click="darkMode()" > <x-laravel-exceptions-renderer::icons.moon class="h-5 w-5" /> Dark </button> <button class="flex items-center gap-3 px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-700" :class="theme === undefined ? 'text-gray-900 dark:text-gray-100' : 'text-gray-500 dark:text-gray-400'" @click="systemMode()" > <x-laravel-exceptions-renderer::icons.computer-desktop class="h-5 w-5" /> System </button> </div> </div> src/Illuminate/Foundation/resources/exceptions/renderer/components/trace-and-editor.blade.php 0000644 00000000724 15060132305 0030547 0 ustar 00 framework <x-laravel-exceptions-renderer::card class="mt-6 overflow-x-auto"> <div x-data="{ includeVendorFrames: false, index: {{ $exception->defaultFrame() }}, }" > <div class="grid grid-cols-1 gap-6 lg:grid-cols-3" x-clock> <x-laravel-exceptions-renderer::trace :$exception /> <x-laravel-exceptions-renderer::editor :$exception /> </div> </div> </x-laravel-exceptions-renderer::card> framework/src/Illuminate/Foundation/resources/exceptions/renderer/package.json 0000644 00000000661 15060132305 0024026 0 ustar 00 { "private": true, "entry": "show.blade.php", "scripts": { "build": "vite build", "watch": "vite build --watch" }, "dependencies": { "alpinejs": "^3.13.10", "autoprefixer": "^10.4.19", "highlight.js": "^11.9.0", "postcss": "^8.4.38", "tailwindcss": "^3.4.3", "tippy.js": "^6.3.7", "vite": "^5.2.10", "vite-require": "^0.2.3" } } framework/src/Illuminate/Foundation/resources/exceptions/renderer/styles.css 0000644 00000002576 15060132305 0023604 0 ustar 00 @import 'tippy.js/dist/tippy.css'; @import 'tippy.js/themes/material.css'; @import 'tippy.js/animations/scale.css'; @tailwind base; @tailwind components; @tailwind utilities; [x-cloak] { display: none; } html { tab-size: 4; } table.hljs-ln { color: inherit; font-size: inherit; border-spacing: 2px; } pre code.hljs { background: none; padding: 0em; padding-top: 0.5em; width: 100%; } .hljs-ln-line { white-space-collapse: preserve; text-wrap: nowrap; } .trace { -webkit-mask-image: linear-gradient(180deg, #000 calc(100% - 4rem), transparent); } .scrollbar-hidden { -ms-overflow-style: none; scrollbar-width: none; overflow-x: scroll; } .scrollbar-hidden::-webkit-scrollbar { -webkit-appearance: none; width: 0; height: 0; } .hljs-ln .hljs-ln-numbers { padding: 5px; border-right-color: transparent; margin-right: 5px; } .hljs-ln-n { width: 50px; } .hljs-ln-numbers { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; text-align: center; border-right: 1px solid #ccc; vertical-align: top; padding-right: 5px; } .hljs-ln-code { width: 100%; padding-left: 10px; padding-right: 10px; } .hljs-ln-code:hover { background-color: rgba(239, 68, 68, 0.2); } framework/src/Illuminate/Foundation/resources/exceptions/renderer/tailwind.config.js 0000644 00000000546 15060132305 0025157 0 ustar 00 const defaultTheme = require('tailwindcss/defaultTheme'); /** @type {import('tailwindcss').Config} */ module.exports = { content: ['./**/*.blade.php'], darkMode: 'class', theme: { extend: { fontFamily: { sans: ['Figtree', ...defaultTheme.fontFamily.sans], }, }, }, plugins: [], }; framework/src/Illuminate/Foundation/resources/exceptions/renderer/show.blade.php 0000644 00000001065 15060132305 0024276 0 ustar 00 <x-laravel-exceptions-renderer::layout :$exception> <div class="renderer container mx-auto lg:px-8"> <x-laravel-exceptions-renderer::navigation :$exception /> <main class="px-6 pb-12 pt-6"> <div class="container mx-auto"> <x-laravel-exceptions-renderer::header :$exception /> <x-laravel-exceptions-renderer::trace-and-editor :$exception /> <x-laravel-exceptions-renderer::context :$exception /> </div> </main> </div> </x-laravel-exceptions-renderer::layout> framework/src/Illuminate/Foundation/resources/server.php 0000644 00000000712 15060132305 0017565 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'; framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php 0000644 00000013513 15060132305 0021634 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; 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; $app->instance('config_loaded_from_cache', $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); $shouldMerge = method_exists($app, 'shouldMergeFrameworkConfiguration') ? $app->shouldMergeFrameworkConfiguration() : true; $base = $shouldMerge ? $this->getBaseConfiguration() : []; foreach (array_diff(array_keys($base), array_keys($files)) as $name => $config) { $repository->set($name, $config); } foreach ($files as $name => $path) { $base = $this->loadConfigurationFile($repository, $name, $path, $base); } foreach ($base as $name => $config) { $repository->set($name, $config); } } /** * Load the given configuration file. * * @param \Illuminate\Contracts\Config\Repository $repository * @param string $name * @param string $path * @param array $base * @return array */ protected function loadConfigurationFile(RepositoryContract $repository, $name, $path, array $base) { $config = require $path; if (isset($base[$name])) { $config = array_merge($base[$name], $config); foreach ($this->mergeableOptions($name) as $option) { if (isset($config[$option])) { $config[$option] = array_merge($base[$name][$option], $config[$option]); } } unset($base[$name]); } $repository->set($name, $config); return $base; } /** * Get the options within the configuration file that should be merged again. * * @param string $name * @return array */ protected function mergeableOptions($name) { return [ 'auth' => ['guards', 'providers', 'passwords'], 'broadcasting' => ['connections'], 'cache' => ['stores'], 'database' => ['connections'], 'filesystems' => ['disks'], 'logging' => ['channels'], 'mail' => ['mailers'], 'queue' => ['connections'], ][$name] ?? []; } /** * 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()); if (! $configPath) { return []; } 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; } /** * Get the base configuration files. * * @return array */ protected function getBaseConfiguration() { $config = []; foreach (Finder::create()->files()->name('*.php')->in(__DIR__.'/../../../../config') as $file) { $config[basename($file->getRealPath(), '.php')] = require $file->getRealPath(); } return $config; } } framework/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php 0000644 00000001333 15060132305 0021255 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(); } } framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php 0000644 00000000544 15060132305 0021026 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(); } } framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php 0000644 00000022330 15060132305 0021457 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 Illuminate\Support\Env; use Monolog\Handler\NullHandler; use PHPUnit\Runner\ErrorHandler; 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) { static::$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() && ! Env::get('LOG_DEPRECATIONS_WHILE_TESTING')); } /** * 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) { static::$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() { static::$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 * * @deprecated This method will be removed in a future Laravel version. */ public static function forgetApp() { static::$app = null; } /** * Flush the bootstrapper's global state. * * @return void */ public static function flushState() { if (is_null(static::$app)) { return; } static::flushHandlersState(); static::$app = null; static::$reservedMemory = null; } /** * Flush the bootstrapper's global handlers state. * * @return void */ public static function flushHandlersState() { while (true) { $previousHandler = set_exception_handler(static fn () => null); restore_exception_handler(); if ($previousHandler === null) { break; } restore_exception_handler(); } while (true) { $previousHandler = set_error_handler(static fn () => null); restore_error_handler(); if ($previousHandler === null) { break; } restore_error_handler(); } if (class_exists(ErrorHandler::class)) { $instance = ErrorHandler::instance(); if ((fn () => $this->enabled ?? false)->call($instance)) { $instance->disable(); $instance->enable(); } } } } framework/src/Illuminate/Foundation/Bootstrap/RegisterProviders.php 0000644 00000004662 15060132305 0021714 0 ustar 00 <?php namespace Illuminate\Foundation\Bootstrap; use Illuminate\Contracts\Foundation\Application; class RegisterProviders { /** * The service providers that should be merged before registration. * * @var array */ protected static $merge = []; /** * The path to the bootstrap provider configuration file. * * @var string|null */ protected static $bootstrapProviderPath; /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { if (! $app->bound('config_loaded_from_cache') || $app->make('config_loaded_from_cache') === false) { $this->mergeAdditionalProviders($app); } $app->registerConfiguredProviders(); } /** * Merge the additional configured providers into the configuration. * * @param \Illuminate\Foundation\Application $app */ protected function mergeAdditionalProviders(Application $app) { if (static::$bootstrapProviderPath && file_exists(static::$bootstrapProviderPath)) { $packageProviders = require static::$bootstrapProviderPath; foreach ($packageProviders as $index => $provider) { if (! class_exists($provider)) { unset($packageProviders[$index]); } } } $app->make('config')->set( 'app.providers', array_merge( $app->make('config')->get('app.providers'), static::$merge, array_values($packageProviders ?? []), ), ); } /** * Merge the given providers into the provider configuration before registration. * * @param array $providers * @param string|null $bootstrapProviderPath * @return void */ public static function merge(array $providers, ?string $bootstrapProviderPath = null) { static::$bootstrapProviderPath = $bootstrapProviderPath; static::$merge = array_values(array_filter(array_unique( array_merge(static::$merge, $providers) ))); } /** * Flush the bootstrapper's global state. * * @return void */ public static function flushState() { static::$bootstrapProviderPath = null; static::$merge = []; } } framework/src/Illuminate/Foundation/Bootstrap/SetRequestForConsole.php 0000644 00000001517 15060132305 0022324 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 )); } } framework/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php 0000644 00000005326 15060132305 0023165 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 never */ 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); } } framework/src/Illuminate/Foundation/ProviderRepository.php 0000755 00000014362 15060132305 0020150 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); } } framework/src/Illuminate/Foundation/Application.php 0000755 00000127720 15060132305 0016524 0 ustar 00 <?php namespace Illuminate\Foundation; use Closure; use Composer\Autoload\ClassLoader; use Illuminate\Container\Container; use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract; 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\Context\ContextServiceProvider; 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\Console\Input\InputInterface; use Symfony\Component\Console\Output\ConsoleOutput; 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; use function Illuminate\Filesystem\join_paths; class Application extends Container implements ApplicationContract, CachesConfiguration, CachesRoutes, HttpKernelInterface { use Macroable; /** * The Laravel framework version. * * @var string */ const VERSION = '11.10.0'; /** * The base path for the Laravel installation. * * @var string */ protected $basePath; /** * The array of registered callbacks. * * @var callable[] */ protected $registeredCallbacks = []; /** * 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 array<string, \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; /** * Indicates if the framework's base configuration should be merged. * * @var bool */ protected $mergeFrameworkConfiguration = true; /** * 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(); } /** * Begin configuring a new Laravel application instance. * * @param string|null $basePath * @return \Illuminate\Foundation\Configuration\ApplicationBuilder */ public static function configure(?string $basePath = null) { $basePath = match (true) { is_string($basePath) => $basePath, default => static::inferBasePath(), }; return (new Configuration\ApplicationBuilder(new static($basePath))) ->withKernels() ->withEvents() ->withCommands() ->withProviders(); } /** * Infer the application's base directory from the environment. * * @return string */ public static function inferBasePath() { return match (true) { isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'], default => dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]), }; } /** * 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 ContextServiceProvider($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); } /** * Get the path to the service provider list in the bootstrap directory. * * @return string */ public function getBootstrapProvidersPath() { return $this->bootstrapPath('providers.php'); } /** * 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); } if (isset($_SERVER['LARAVEL_STORAGE_PATH'])) { return $this->joinPaths($this->storagePath ?: $_SERVER['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 join_paths($basePath, $path); } /** * 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 a new registered listener. * * @param callable $callback * @return void */ public function registered($callback) { $this->registeredCallbacks[] = $callback; } /** * 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()); $this->fireAppCallbacks($this->registeredCallbacks); } /** * 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) { $name = is_string($provider) ? $provider : get_class($provider); return $this->serviceProviders[$name] ?? 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) { $class = get_class($provider); $this->serviceProviders[$class] = $provider; $this->loadedProviders[$class] = 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)); } /** * Handle the incoming HTTP request and send the response to the browser. * * @param \Illuminate\Http\Request $request * @return void */ public function handleRequest(Request $request) { $kernel = $this->make(HttpKernelContract::class); $response = $kernel->handle($request)->send(); $kernel->terminate($request, $response); } /** * Handle the incoming Artisan command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @return int */ public function handleCommand(InputInterface $input) { $kernel = $this->make(ConsoleKernelContract::class); $status = $kernel->handle( $input, new ConsoleOutput ); $kernel->terminate($input, $status); return $status; } /** * Determine if the framework's base configuration should be merged. * * @return bool */ public function shouldMergeFrameworkConfiguration() { return $this->mergeFrameworkConfiguration; } /** * Indicate that the framework's base configuration should not be merged. * * @return $this */ public function dontMergeFrameworkConfiguration() { $this->mergeFrameworkConfiguration = false; return $this; } /** * 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<string, boolean> */ 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.'); } } framework/src/Illuminate/Foundation/MixManifestNotFoundException.php 0000644 00000000171 15060132305 0022024 0 ustar 00 <?php namespace Illuminate\Foundation; use Exception; class MixManifestNotFoundException extends Exception { // } framework/src/Illuminate/Foundation/Console/CastMakeCommand.php 0000644 00000003547 15060132305 0020647 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'], ]; } } framework/src/Illuminate/Foundation/Console/EnvironmentCommand.php 0000644 00000001326 15060132305 0021454 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'], )); } } framework/src/Illuminate/Foundation/Console/InterfaceMakeCommand.php 0000644 00000003020 15060132305 0021637 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:interface')] class InterfaceMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:interface'; /** * The console command description. * * @var string */ protected $description = 'Create a new interface'; /** * The type of class being generated. * * @var string */ protected $type = 'Interface'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return __DIR__.'/stubs/interface.stub'; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return match (true) { is_dir(app_path('Contracts')) => $rootNamespace.'\\Contracts', is_dir(app_path('Interfaces')) => $rootNamespace.'\\Interfaces', default => $rootNamespace, }; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the interface even if the interface already exists'], ]; } } framework/src/Illuminate/Foundation/Console/RequestMakeCommand.php 0000644 00000003277 15060132305 0021405 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'], ]; } } framework/src/Illuminate/Foundation/Console/ResourceMakeCommand.php 0000644 00000004624 15060132305 0021541 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'], ]; } } framework/src/Illuminate/Foundation/Console/ConfigShowCommand.php 0000644 00000005635 15060132305 0021225 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), }; } } framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php 0000644 00000005350 15060132305 0021173 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); } } framework/src/Illuminate/Foundation/Console/RuleMakeCommand.php 0000644 00000004004 15060132305 0020651 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'], ]; } } framework/src/Illuminate/Foundation/Console/stubs/enum.stub 0000644 00000000077 15060132305 0020145 0 ustar 00 <?php namespace {{ namespace }}; enum {{ class }} { // } framework/src/Illuminate/Foundation/Console/stubs/exception-render-report.stub 0000644 00000000615 15060132305 0023763 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/trait.stub 0000644 00000000100 15060132305 0020307 0 ustar 00 <?php namespace {{ namespace }}; trait {{ class }} { // } framework/src/Illuminate/Foundation/Console/stubs/scope.stub 0000644 00000000546 15060132305 0020313 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/cast.inbound.stub 0000644 00000000671 15060132305 0021570 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; } } framework/src/Illuminate/Foundation/Console/stubs/class.invokable.stub 0000644 00000000413 15060132305 0022251 0 ustar 00 <?php namespace {{ namespace }}; class {{ class }} { /** * Create a new class instance. */ public function __construct() { // } /** * Invoke the class instance. */ public function __invoke(): void { } } framework/src/Illuminate/Foundation/Console/stubs/pest.stub 0000644 00000000155 15060132305 0020151 0 ustar 00 <?php test('example', function () { $response = $this->get('/'); $response->assertStatus(200); }); framework/src/Illuminate/Foundation/Console/stubs/provider.stub 0000644 00000000504 15060132305 0021026 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/markdown-mail.stub 0000644 00000001772 15060132305 0021746 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 []; } } framework/src/Illuminate/Foundation/Console/stubs/listener.typed.stub 0000644 00000000612 15060132305 0022145 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/routes.stub 0000644 00000000073 15060132305 0020516 0 ustar 00 <?php app('router')->setCompiledRoutes( {{routes}} ); framework/src/Illuminate/Foundation/Console/stubs/exception-render.stub 0000644 00000000445 15060132305 0022453 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/pest.unit.stub 0000644 00000000107 15060132305 0021124 0 ustar 00 <?php test('example', function () { expect(true)->toBeTrue(); }); framework/src/Illuminate/Foundation/Console/stubs/model.pivot.stub 0000644 00000000201 15060132305 0021426 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Relations\Pivot; class {{ class }} extends Pivot { // } framework/src/Illuminate/Foundation/Console/stubs/console.stub 0000644 00000000746 15060132305 0020646 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() { // } } framework/src/Illuminate/Foundation/Console/stubs/class.stub 0000644 00000000246 15060132305 0020304 0 ustar 00 <?php namespace {{ namespace }}; class {{ class }} { /** * Create a new class instance. */ public function __construct() { // } } framework/src/Illuminate/Foundation/Console/stubs/observer.stub 0000644 00000001550 15060132305 0021025 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/exception-report.stub 0000644 00000000302 15060132305 0022477 0 ustar 00 <?php namespace {{ namespace }}; use Exception; class {{ class }} extends Exception { /** * Report the exception. */ public function report(): void { // } } framework/src/Illuminate/Foundation/Console/stubs/channel.stub 0000644 00000000542 15060132305 0020606 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/echo-bootstrap-js.stub 0000644 00000000366 15060132305 0022545 0 ustar 00 /** * Echo exposes an expressive API for subscribing to channels and listening * for events that are broadcast by Laravel. Echo and event broadcasting * allow your team to quickly build robust real-time web applications. */ import './echo'; framework/src/Illuminate/Foundation/Console/stubs/view.test.stub 0000644 00000000472 15060132305 0021130 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(''); } } framework/src/Illuminate/Foundation/Console/stubs/interface.stub 0000644 00000000104 15060132305 0021130 0 ustar 00 <?php namespace {{ namespace }}; interface {{ class }} { // } framework/src/Illuminate/Foundation/Console/stubs/cast.stub 0000644 00000001217 15060132305 0020130 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; } } framework/src/Illuminate/Foundation/Console/stubs/echo-js.stub 0000644 00000000712 15060132305 0020525 0 ustar 00 import Echo from 'laravel-echo'; import Pusher from 'pusher-js'; window.Pusher = Pusher; window.Echo = new Echo({ broadcaster: 'reverb', key: import.meta.env.VITE_REVERB_APP_KEY, wsHost: import.meta.env.VITE_REVERB_HOST, wsPort: import.meta.env.VITE_REVERB_PORT ?? 80, wssPort: import.meta.env.VITE_REVERB_PORT ?? 443, forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https', enabledTransports: ['ws', 'wss'], }); framework/src/Illuminate/Foundation/Console/stubs/markdown.stub 0000644 00000000257 15060132305 0021023 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> framework/src/Illuminate/Foundation/Console/stubs/view.stub 0000644 00000000046 15060132305 0020147 0 ustar 00 <div> <!-- {{ quote }} --> </div> framework/src/Illuminate/Foundation/Console/stubs/markdown-notification.stub 0000644 00000001760 15060132305 0023507 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 [ // ]; } } framework/src/Illuminate/Foundation/Console/stubs/resource-collection.stub 0000644 00000000613 15060132305 0023155 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); } } framework/src/Illuminate/Foundation/Console/stubs/policy.plain.stub 0000644 00000000307 15060132305 0021576 0 ustar 00 <?php namespace {{ namespace }}; use {{ namespacedUserModel }}; class {{ class }} { /** * Create a new policy instance. */ public function __construct() { // } } framework/src/Illuminate/Foundation/Console/stubs/maintenance-mode.stub 0000644 00000004041 15060132306 0022401 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; framework/src/Illuminate/Foundation/Console/stubs/listener.queued.stub 0000644 00000000637 15060132306 0022320 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/job.stub 0000644 00000000513 15060132306 0017747 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/test.unit.stub 0000644 00000000363 15060132306 0021135 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); } } framework/src/Illuminate/Foundation/Console/stubs/model.morph-pivot.stub 0000644 00000000213 15060132306 0022555 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Relations\MorphPivot; class {{ class }} extends MorphPivot { // } framework/src/Illuminate/Foundation/Console/stubs/broadcasting-routes.stub 0000644 00000000243 15060132306 0023154 0 ustar 00 <?php use Illuminate\Support\Facades\Broadcast; Broadcast::channel('App.Models.User.{id}', function ($user, $id) { return (int) $user->id === (int) $id; }); framework/src/Illuminate/Foundation/Console/stubs/rule.implicit.stub 0000644 00000001016 15060132306 0021754 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/listener.stub 0000644 00000000553 15060132306 0021026 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/test.stub 0000644 00000000565 15060132306 0020163 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); } } framework/src/Illuminate/Foundation/Console/stubs/view-mail.stub 0000644 00000001766 15060132306 0021102 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 }}', ); } /** * Get the attachments for the message. * * @return array<int, \Illuminate\Mail\Mailables\Attachment> */ public function attachments(): array { return []; } } framework/src/Illuminate/Foundation/Console/stubs/job.queued.stub 0000644 00000001055 15060132306 0021240 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Bus\Queueable; 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/observer.plain.stub 0000644 00000000100 15060132306 0022116 0 ustar 00 <?php namespace {{ namespace }}; class {{ class }} { // } framework/src/Illuminate/Foundation/Console/stubs/exception.stub 0000644 00000000142 15060132306 0021171 0 ustar 00 <?php namespace {{ namespace }}; use Exception; class {{ class }} extends Exception { // } framework/src/Illuminate/Foundation/Console/stubs/enum.backed.stub 0000644 00000000113 15060132306 0021345 0 ustar 00 <?php namespace {{ namespace }}; enum {{ class }}: {{ type }} { // } framework/src/Illuminate/Foundation/Console/stubs/request.stub 0000644 00000001042 15060132306 0020663 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 [ // ]; } } framework/src/Illuminate/Foundation/Console/stubs/event.stub 0000644 00000001457 15060132306 0020326 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'), ]; } } framework/src/Illuminate/Foundation/Console/stubs/api-routes.stub 0000644 00000000272 15060132306 0021267 0 ustar 00 <?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::get('/user', function (Request $request) { return $request->user(); })->middleware('auth:sanctum'); framework/src/Illuminate/Foundation/Console/stubs/listener.typed.queued.stub 0000644 00000000676 15060132306 0023447 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/notification.stub 0000644 00000002235 15060132306 0021666 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 [ // ]; } } framework/src/Illuminate/Foundation/Console/stubs/view.pest.stub 0000644 00000000210 15060132306 0021113 0 ustar 00 <?php it('can render', function () { $contents = $this->view('{{ name }}', [ // ]); $contents->assertSee(''); }); framework/src/Illuminate/Foundation/Console/stubs/policy.stub 0000644 00000002523 15060132306 0020477 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/model.stub 0000644 00000000273 15060132306 0020300 0 ustar 00 <?php namespace {{ namespace }}; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class {{ class }} extends Model { use HasFactory; } framework/src/Illuminate/Foundation/Console/stubs/view-component.stub 0000644 00000000664 15060132306 0022156 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 }}; } } framework/src/Illuminate/Foundation/Console/stubs/rule.stub 0000644 00000000622 15060132306 0020145 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 { // } } framework/src/Illuminate/Foundation/Console/stubs/resource.stub 0000644 00000000560 15060132306 0021026 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); } } framework/src/Illuminate/Foundation/Console/stubs/mail.stub 0000644 00000001765 15060132306 0020131 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 []; } } framework/src/Illuminate/Foundation/Console/EventListCommand.php 0000644 00000013166 15060132306 0021073 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; } } framework/src/Illuminate/Foundation/Console/TestMakeCommand.php 0000644 00000007634 15060132306 0020676 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->usingPest() ? $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 test even if the test already exists'], ['unit', 'u', InputOption::VALUE_NONE, 'Create a unit test'], ['pest', null, InputOption::VALUE_NONE, 'Create a Pest test'], ['phpunit', null, InputOption::VALUE_NONE, 'Create a PHPUnit 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', 'unit' => 'Unit', ]); match ($type) { 'feature' => null, 'unit' => $input->setOption('unit', true), }; } /** * Determine if Pest is being used by the application. * * @return bool */ protected function usingPest() { if ($this->option('phpunit')) { return false; } return $this->option('pest') || (function_exists('\Pest\\version') && file_exists(base_path('tests').'/Pest.php')); } } framework/src/Illuminate/Foundation/Console/CliDumper.php 0000644 00000006440 15060132306 0017540 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; $this->setColors($this->supportsColors()); } /** * 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(); } } framework/src/Illuminate/Foundation/Console/ViewClearCommand.php 0000644 00000003023 15060132306 0021026 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.'); } } framework/src/Illuminate/Foundation/Console/ConfigPublishCommand.php 0000644 00000005632 15060132306 0021711 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Finder\Finder; use function Laravel\Prompts\select; #[AsCommand(name: 'config:publish')] class ConfigPublishCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'config:publish {name? : The name of the configuration file to publish} {--all : Publish all configuration files} {--force : Overwrite any existing configuration files}'; /** * The console command description. * * @var string */ protected $description = 'Publish configuration files to your application'; /** * Execute the console command. * * @return int */ public function handle() { $config = $this->getBaseConfigurationFiles(); if (is_null($this->argument('name')) && $this->option('all')) { foreach ($config as $key => $file) { $this->publish($key, $file, $this->laravel->configPath().'/'.$key.'.php'); } return; } $name = (string) (is_null($this->argument('name')) ? select( label: 'Which configuration file would you like to publish?', options: collect($config)->map(function (string $path) { return basename($path, '.php'); }), ) : $this->argument('name')); if (! is_null($name) && ! isset($config[$name])) { $this->components->error('Unrecognized configuration file.'); return 1; } $this->publish($name, $config[$name], $this->laravel->configPath().'/'.$name.'.php'); } /** * Publish the given file to the given destination. * * @param string $name * @param string $file * @param string $destination * @return void */ protected function publish(string $name, string $file, string $destination) { if (file_exists($destination) && ! $this->option('force')) { $this->components->error("The '{$name}' configuration file already exists."); return; } copy($file, $destination); $this->components->info("Published '{$name}' configuration file."); } /** * Get an array containing the base configuration files. * * @return array */ protected function getBaseConfigurationFiles() { $config = []; foreach (Finder::create()->files()->name('*.php')->in(__DIR__.'/../../../../config') as $file) { $name = basename($file->getRealPath(), '.php'); $config[$name] = file_exists($stubPath = (__DIR__.'/../../../../config-stubs/'.$name.'.php')) ? $stubPath : $file->getRealPath(); } return collect($config)->sortKeys()->all(); } } framework/src/Illuminate/Foundation/Console/EventMakeCommand.php 0000644 00000003725 15060132306 0021035 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'], ]; } } framework/src/Illuminate/Foundation/Console/ApiInstallCommand.php 0000644 00000012027 15060132306 0021211 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Process; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Process\PhpExecutableFinder; #[AsCommand(name: 'install:api')] class ApiInstallCommand extends Command { use InteractsWithComposerPackages; /** * The name and signature of the console command. * * @var string */ protected $signature = 'install:api {--composer=global : Absolute path to the Composer binary which should be used to install packages} {--force : Overwrite any existing API routes file} {--passport : Install Laravel Passport instead of Laravel Sanctum} {--without-migration-prompt : Do not prompt to run pending migrations}'; /** * The console command description. * * @var string */ protected $description = 'Create an API routes file and install Laravel Sanctum or Laravel Passport'; /** * Execute the console command. * * @return int */ public function handle() { if ($this->option('passport')) { $this->installPassport(); } else { $this->installSanctum(); } if (file_exists($apiRoutesPath = $this->laravel->basePath('routes/api.php')) && ! $this->option('force')) { $this->components->error('API routes file already exists.'); } else { $this->components->info('Published API routes file.'); copy(__DIR__.'/stubs/api-routes.stub', $apiRoutesPath); if ($this->option('passport')) { (new Filesystem)->replaceInFile( 'auth:sanctum', 'auth:api', $apiRoutesPath, ); } $this->uncommentApiRoutesFile(); } if ($this->option('passport')) { Process::run(array_filter([ (new PhpExecutableFinder())->find(false) ?: 'php', defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan', 'passport:install', $this->confirm('Would you like to use UUIDs for all client IDs?') ? '--uuids' : null, ])); $this->components->info('API scaffolding installed. Please add the [Laravel\Passport\HasApiTokens] trait to your User model.'); } else { if (! $this->option('without-migration-prompt')) { if ($this->confirm('One new database migration has been published. Would you like to run all pending database migrations?', true)) { $this->call('migrate'); } } $this->components->info('API scaffolding installed. Please add the [Laravel\Sanctum\HasApiTokens] trait to your User model.'); } } /** * Uncomment the API routes file in the application bootstrap file. * * @return void */ protected function uncommentApiRoutesFile() { $appBootstrapPath = $this->laravel->bootstrapPath('app.php'); $content = file_get_contents($appBootstrapPath); if (str_contains($content, '// api: ')) { (new Filesystem)->replaceInFile( '// api: ', 'api: ', $appBootstrapPath, ); } elseif (str_contains($content, 'web: __DIR__.\'/../routes/web.php\',')) { (new Filesystem)->replaceInFile( 'web: __DIR__.\'/../routes/web.php\',', 'web: __DIR__.\'/../routes/web.php\','.PHP_EOL.' api: __DIR__.\'/../routes/api.php\',', $appBootstrapPath, ); } else { $this->components->warn('Unable to automatically add API route definition to bootstrap file. API route file should be registered manually.'); return; } } /** * Install Laravel Sanctum into the application. * * @return void */ protected function installSanctum() { $this->requireComposerPackages($this->option('composer'), [ 'laravel/sanctum:^4.0', ]); $migrationPublished = collect(scandir($this->laravel->databasePath('migrations')))->contains(function ($migration) { return preg_match('/\d{4}_\d{2}_\d{2}_\d{6}_create_personal_access_tokens_table.php/', $migration); }); if (! $migrationPublished) { Process::run([ (new PhpExecutableFinder())->find(false) ?: 'php', defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan', 'vendor:publish', '--provider', 'Laravel\\Sanctum\\SanctumServiceProvider', ]); } } /** * Install Laravel Passport into the application. * * @return void */ protected function installPassport() { $this->requireComposerPackages($this->option('composer'), [ 'laravel/passport:^12.0', ]); } } framework/src/Illuminate/Foundation/Console/EventGenerateCommand.php 0000644 00000004112 15060132306 0021701 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'; /** * Indicates whether the command should be shown in the Artisan command list. * * @var bool */ protected $hidden = true; /** * 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] )); } } } framework/src/Illuminate/Foundation/Console/ClearCompiledCommand.php 0000644 00000001664 15060132306 0021661 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.'); } } framework/src/Illuminate/Foundation/Console/OptimizeClearCommand.php 0000644 00000002334 15060132306 0021720 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([ 'cache' => fn () => $this->callSilent('cache:clear') == 0, 'compiled' => fn () => $this->callSilent('clear-compiled') == 0, 'config' => fn () => $this->callSilent('config:clear') == 0, 'events' => fn () => $this->callSilent('event:clear') == 0, 'routes' => fn () => $this->callSilent('route:clear') == 0, 'views' => fn () => $this->callSilent('view:clear') == 0, ])->each(fn ($task, $description) => $this->components->task($description, $task)); $this->newLine(); } } framework/src/Illuminate/Foundation/Console/TraitMakeCommand.php 0000644 00000003474 15060132306 0021040 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:trait')] class TraitMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:trait'; /** * The console command description. * * @var string */ protected $description = 'Create a new trait'; /** * The type of class being generated. * * @var string */ protected $type = 'Trait'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->resolveStubPath('/stubs/trait.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 match (true) { is_dir(app_path('Concerns')) => $rootNamespace.'\\Concerns', is_dir(app_path('Traits')) => $rootNamespace.'\\Traits', default => $rootNamespace, }; } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['force', 'f', InputOption::VALUE_NONE, 'Create the trait even if the trait already exists'], ]; } } framework/src/Illuminate/Foundation/Console/DocsCommand.php 0000644 00000032626 15060132306 0020050 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; } } framework/src/Illuminate/Foundation/Console/PolicyMakeCommand.php 0000644 00000014006 15060132306 0021205 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); } } } framework/src/Illuminate/Foundation/Console/EventCacheCommand.php 0000644 00000002733 15060132306 0021161 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; } } framework/src/Illuminate/Foundation/Console/ListenerMakeCommand.php 0000644 00000010216 15060132306 0021532 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 ); } /** * 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 stub file for the generator. * * @return string */ protected function getStub() { if ($this->option('queued')) { return $this->option('event') ? $this->resolveStubPath('/stubs/listener.typed.queued.stub') : $this->resolveStubPath('/stubs/listener.queued.stub'); } return $this->option('event') ? $this->resolveStubPath('/stubs/listener.typed.stub') : $this->resolveStubPath('/stubs/listener.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); } } } framework/src/Illuminate/Foundation/Console/NotificationMakeCommand.php 0000644 00000006261 15060132306 0022400 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'], ]; } } framework/src/Illuminate/Foundation/Console/ProviderMakeCommand.php 0000644 00000004327 15060132306 0021545 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; use Illuminate\Support\ServiceProvider; 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'; /** * Execute the console command. * * @return bool|null * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function handle() { $result = parent::handle(); if ($result === false) { return $result; } ServiceProvider::addProviderToBootstrapFile( $this->qualifyClass($this->getNameInput()), $this->laravel->getBootstrapProvidersPath(), ); return $result; } /** * 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'], ]; } } framework/src/Illuminate/Foundation/Console/EnvironmentDecryptCommand.php 0000644 00000007721 15060132306 0023015 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') ? Str::finish(dirname($this->laravel->environmentFilePath()), DIRECTORY_SEPARATOR).'.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') ?: dirname($this->laravel->environmentFilePath()), DIRECTORY_SEPARATOR); $outputFile = $this->option('filename') ?: ('.env'.($this->option('env') ? '.'.$this->option('env') : '')); $outputFile = ltrim($outputFile, DIRECTORY_SEPARATOR); return $path.$outputFile; } } framework/src/Illuminate/Foundation/Console/DownCommand.php 0000644 00000011545 15060132306 0020064 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 Illuminate\Support\Str; 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} {--with-secret : Generate a random 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; } $downFilePayload = $this->getDownFilePayload(); $this->laravel->maintenanceMode()->activate($downFilePayload); 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.'); if ($downFilePayload['secret'] !== null) { $this->components->info('You may bypass maintenance mode via ['.config('app.url')."/{$downFilePayload['secret']}]."); } } 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->getSecret(), '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; } /** * Get the secret phrase that may be used to bypass maintenance mode. * * @return string|null */ protected function getSecret() { return match (true) { ! is_null($this->option('secret')) => $this->option('secret'), $this->option('with-secret') => Str::random(), default => null, }; } } framework/src/Illuminate/Foundation/Console/MailMakeCommand.php 0000644 00000011130 15060132306 0020623 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: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(); } if ($this->option('view') !== false) { $this->writeView(); } } /** * Write the Markdown template for the mailable. * * @return void */ protected function writeMarkdownTemplate() { $path = $this->viewPath( str_replace('.', '/', $this->getView()).'.blade.php' ); $this->files->ensureDirectoryExists(dirname($path)); $this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub')); } /** * Write the Blade template for the mailable. * * @return void */ protected function writeView() { $path = $this->viewPath( str_replace('.', '/', $this->getView()).'.blade.php' ); $this->files->ensureDirectoryExists(dirname($path)); $stub = str_replace( '{{ quote }}', Inspiring::quotes()->random(), file_get_contents(__DIR__.'/stubs/view.stub') ); $this->files->put($path, $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 || $this->option('view') !== false) { $class = str_replace(['DummyView', '{{ view }}'], $this->getView(), $class); } return $class; } /** * Get the view name. * * @return string */ protected function getView() { $view = $this->option('markdown') ?: $this->option('view'); 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() { if ($this->option('markdown') !== false) { return $this->resolveStubPath('/stubs/markdown-mail.stub'); } if ($this->option('view') !== false) { return $this->resolveStubPath('/stubs/view-mail.stub'); } return $this->resolveStubPath('/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], ['view', null, InputOption::VALUE_OPTIONAL, 'Create a new Blade template for the mailable', false], ]; } } framework/src/Illuminate/Foundation/Console/UpCommand.php 0000644 00000002647 15060132306 0017544 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; } } framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php 0000644 00000006243 15060132306 0021357 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"; } } framework/src/Illuminate/Foundation/Console/ConfigClearCommand.php 0000644 00000002242 15060132306 0021323 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.'); } } framework/src/Illuminate/Foundation/Console/ConfigCacheCommand.php 0000644 00000004167 15060132306 0021310 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(); } } framework/src/Illuminate/Foundation/Console/Kernel.php 0000644 00000040705 15060132306 0017076 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 = []; /** * The paths where Artisan commands should be automatically discovered. * * @var array */ protected $commandPaths = []; /** * The paths where Artisan "routes" should be automatically discovered. * * @var array */ protected $commandRoutePaths = []; /** * Indicates if the Closure commands have been loaded. * * @var bool */ protected $commandsLoaded = false; /** * The commands paths that have been "loaded". * * @var array */ protected $loadedPaths = []; /** * 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(); } }); } /** * 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; } /** * 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) { // } /** * Resolve a console schedule instance. * * @return \Illuminate\Console\Scheduling\Schedule */ public function resolveConsoleSchedule() { return tap(new Schedule($this->scheduleTimezone()), function ($schedule) { $this->schedule($schedule->useCache($this->scheduleCache())); }); } /** * 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')); } /** * Get the name of the cache store that should manage scheduling mutexes. * * @return string|null */ protected function scheduleCache() { return $this->app['config']->get('cache.schedule_store', Env::get('SCHEDULE_CACHE_DRIVER', function () { return Env::get('SCHEDULE_CACHE_STORE'); })); } /** * 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; } $this->loadedPaths = array_values( array_unique(array_merge($this->loadedPaths, $paths)) ); $namespace = $this->app->getNamespace(); foreach (Finder::create()->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(); if ($this->shouldDiscoverCommands()) { $this->discoverCommands(); } $this->commandsLoaded = true; } } /** * Discover the commands that should be automatically loaded. * * @return void */ protected function discoverCommands() { foreach ($this->commandPaths as $path) { $this->load($path); } foreach ($this->commandRoutePaths as $path) { if (file_exists($path)) { require $path; } } } /** * 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() ); } /** * Determine if the kernel should discover commands. * * @return bool */ protected function shouldDiscoverCommands() { return get_class($this) === __CLASS__; } /** * 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; } /** * Set the Artisan commands provided by the application. * * @param array $commands * @return $this */ public function addCommands(array $commands) { $this->commands = array_values(array_unique(array_merge($this->commands, $commands))); return $this; } /** * Set the paths that should have their Artisan commands automatically discovered. * * @param array $paths * @return $this */ public function addCommandPaths(array $paths) { $this->commandPaths = array_values(array_unique(array_merge($this->commandPaths, $paths))); return $this; } /** * Set the paths that should have their Artisan "routes" automatically discovered. * * @param array $paths * @return $this */ public function addCommandRoutePaths(array $paths) { $this->commandRoutePaths = array_values(array_unique(array_merge($this->commandRoutePaths, $paths))); return $this; } /** * 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); } } framework/src/Illuminate/Foundation/Console/StubPublishCommand.php 0000644 00000013417 15060132306 0021421 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/class.stub' => 'class.stub', __DIR__.'/stubs/class.invokable.stub' => 'class.invokable.stub', __DIR__.'/stubs/console.stub' => 'console.stub', __DIR__.'/stubs/enum.stub' => 'enum.stub', __DIR__.'/stubs/enum.backed.stub' => 'enum.backed.stub', __DIR__.'/stubs/event.stub' => 'event.stub', __DIR__.'/stubs/job.queued.stub' => 'job.queued.stub', __DIR__.'/stubs/job.stub' => 'job.stub', __DIR__.'/stubs/listener.typed.queued.stub' => 'listener.typed.queued.stub', __DIR__.'/stubs/listener.queued.stub' => 'listener.queued.stub', __DIR__.'/stubs/listener.typed.stub' => 'listener.typed.stub', __DIR__.'/stubs/listener.stub' => 'listener.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/trait.stub' => 'trait.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.'); } } framework/src/Illuminate/Foundation/Console/ModelMakeCommand.php 0000644 00000017130 15060132306 0021007 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, ]); } /** * 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)); } } framework/src/Illuminate/Foundation/Console/ObserverMakeCommand.php 0000644 00000010654 15060132306 0021542 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); } } } framework/src/Illuminate/Foundation/Console/ClosureCommand.php 0000644 00000005664 15060132306 0020576 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Closure; use Illuminate\Console\Command; use Illuminate\Console\ManuallyFailedException; use Illuminate\Support\Facades\Schedule; use Illuminate\Support\Traits\ForwardsCalls; use ReflectionFunction; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; /** * @mixin \Illuminate\Console\Scheduling\Event */ class ClosureCommand extends Command { use ForwardsCalls; /** * 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): int { $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()]; } } try { return (int) $this->laravel->call( $this->callback->bindTo($this, $this), $parameters ); } catch (ManuallyFailedException $e) { $this->components->error($e->getMessage()); return static::FAILURE; } } /** * 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; } /** * Create a new scheduled event for the command. * * @param array $parameters * @return \Illuminate\Console\Scheduling\Event */ public function schedule($parameters = []) { return Schedule::command($this->name, $parameters); } /** * Dynamically proxy calls to a new scheduled event. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { return $this->forwardCallTo($this->schedule(), $method, $parameters); } } framework/src/Illuminate/Foundation/Console/LangPublishCommand.php 0000644 00000003513 15060132306 0021361 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.'); } } framework/src/Illuminate/Foundation/Console/StorageUnlinkCommand.php 0000644 00000002322 15060132306 0021733 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'storage:unlink')] class StorageUnlinkCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'storage:unlink'; /** * The console command description. * * @var string */ protected $description = 'Delete existing symbolic links configured for the application'; /** * Execute the console command. * * @return void */ public function handle() { foreach ($this->links() as $link => $target) { if (! file_exists($link) || ! is_link($link)) { continue; } $this->laravel->make('files')->delete($link); $this->components->info("The [$link] link has been deleted."); } } /** * 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')]; } } framework/src/Illuminate/Foundation/Console/ScopeMakeCommand.php 0000644 00000003316 15060132306 0021021 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'], ]; } } framework/src/Illuminate/Foundation/Console/PackageDiscoverCommand.php 0000644 00000002010 15060132306 0022172 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()); } } framework/src/Illuminate/Foundation/Console/ServeCommand.php 0000644 00000025534 15060132306 0020244 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 lines that are pending to be output. * * @var string */ protected $outputBuffer = ''; /** * 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', 'HERD_PHP_81_INI_SCAN_DIR', 'HERD_PHP_82_INI_SCAN_DIR', 'HERD_PHP_83_INI_SCAN_DIR', '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()); $this->trap(fn () => [SIGTERM, SIGINT, SIGHUP, SIGUSR1, SIGUSR2, SIGQUIT], function ($signal) use ($process) { if ($process->isRunning()) { $process->stop(10, $signal); } exit; }); $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 function ($type, $buffer) { $this->outputBuffer .= $buffer; $this->flushOutputBuffer(); }; } /** * Flush the output buffer. * * @return void */ protected function flushOutputBuffer() { $lines = str($this->outputBuffer)->explode("\n"); $this->outputBuffer = (string) $lines->pop(); $lines ->map(fn ($line) => trim($line)) ->filter() ->each(function ($line) { if (str($line)->contains('Development Server (http')) { if ($this->serverRunningHasBeenDisplayed === false) { $this->serverRunningHasBeenDisplayed = true; $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(); } return; } if (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])) { $this->requestsPool[$requestPort] = [ $this->getDateFromLine($line), false, ]; } [$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', 'Failed to poll event'])) { // ... } elseif (! empty($line)) { if (str($line)->startsWith('[')) { $line = str($line)->after('] '); } $this->output->writeln(" <fg=gray>$line</>"); } }); } /** * 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'], ]; } } framework/src/Illuminate/Foundation/Console/ComponentMakeCommand.php 0000644 00000010554 15060132306 0021714 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'], ]; } } framework/src/Illuminate/Foundation/Console/ViewMakeCommand.php 0000644 00000013754 15060132306 0020671 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') && ! $this->option('phpunit')) { 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->usingPest() ? '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(); } /** * Determine if Pest is being used by the application. * * @return bool */ protected function usingPest() { if ($this->option('phpunit')) { return false; } return $this->option('pest') || (function_exists('\Pest\\version') && file_exists(base_path('tests').'/Pest.php')); } /** * 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'], ]; } } framework/src/Illuminate/Foundation/Console/EnumMakeCommand.php 0000644 00000007072 15060132306 0020657 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; 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:enum')] class EnumMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:enum'; /** * The console command description. * * @var string */ protected $description = 'Create a new enum'; /** * The type of class being generated. * * @var string */ protected $type = 'Enum'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { if ($this->option('string') || $this->option('int')) { return $this->resolveStubPath('/stubs/enum.backed.stub'); } return $this->resolveStubPath('/stubs/enum.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 match (true) { is_dir(app_path('Enums')) => $rootNamespace.'\\Enums', is_dir(app_path('Enumerations')) => $rootNamespace.'\\Enumerations', default => $rootNamespace, }; } /** * Build the class with the given name. * * @param string $name * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ protected function buildClass($name) { if ($this->option('string') || $this->option('int')) { return str_replace( ['{{ type }}'], $this->option('string') ? 'string' : 'int', parent::buildClass($name) ); } return parent::buildClass($name); } /** * 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 enum would you like?', [ 'pure' => 'Pure enum', 'string' => 'Backed enum (String)', 'int' => 'Backed enum (Integer)', ]); if ($type !== 'pure') { $input->setOption($type, true); } } /** * Get the console command arguments. * * @return array */ protected function getOptions() { return [ ['string', 's', InputOption::VALUE_NONE, 'Generate a string backed enum.'], ['int', 'i', InputOption::VALUE_NONE, 'Generate an integer backed enum.'], ['force', 'f', InputOption::VALUE_NONE, 'Create the enum even if the enum already exists'], ]; } } framework/src/Illuminate/Foundation/Console/AboutCommand.php 0000644 00000023143 15060132306 0020224 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Closure; 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, false)); }); }); } /** * 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], true), ]), ]; }); $this->output->writeln(strip_tags(json_encode($output))); } /** * Gather information about the application. * * @return void */ protected function gatherApplicationInformation() { self::$data = []; $formatEnabledStatus = fn ($value) => $value ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF'; $formatCachedStatus = fn ($value) => $value ? '<fg=green;options=bold>CACHED</>' : '<fg=yellow;options=bold>NOT CACHED</>'; 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' => static::format(config('app.debug'), console: $formatEnabledStatus), 'URL' => Str::of(config('app.url'))->replace(['http://', 'https://'], ''), 'Maintenance Mode' => static::format($this->laravel->isDownForMaintenance(), console: $formatEnabledStatus), ]); static::addToSection('Cache', fn () => [ 'Config' => static::format($this->laravel->configurationIsCached(), console: $formatCachedStatus), 'Events' => static::format($this->laravel->eventsAreCached(), console: $formatCachedStatus), 'Routes' => static::format($this->laravel->routesAreCached(), console: $formatCachedStatus), 'Views' => static::format($this->hasPhpFiles($this->laravel->storagePath('framework/views')), console: $formatCachedStatus), ]); static::addToSection('Drivers', fn () => array_filter([ 'Broadcasting' => config('broadcasting.default'), 'Cache' => config('cache.default'), 'Database' => config('database.default'), 'Logs' => function ($json) { $logChannel = config('logging.default'); if (config('logging.channels.'.$logChannel.'.driver') === 'stack') { $secondary = collect(config('logging.channels.'.$logChannel.'.channels')); return value(static::format( value: $logChannel, console: fn ($value) => '<fg=yellow;options=bold>'.$value.'</> <fg=gray;options=bold>/</> '.$secondary->implode(', '), json: fn () => $secondary->all(), ), $json); } else { $logs = $logChannel; } return $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(); } /** * Materialize a function that formats a given value for CLI or JSON output. * * @param mixed $value * @param (\Closure(mixed):(mixed))|null $console * @param (\Closure(mixed):(mixed))|null $json * @return \Closure(bool):mixed */ public static function format($value, ?Closure $console = null, ?Closure $json = null) { return function ($isJson) use ($value, $console, $json) { if ($isJson === true && $json instanceof Closure) { return value($json, $value); } elseif ($isJson === false && $console instanceof Closure) { return value($console, $value); } return value($value); }; } /** * Format the given string for searching. * * @param string $value * @return string */ protected function toSearchKeyword(string $value) { return (string) Str::of($value)->lower()->snake(); } /** * Flush the registered about data. * * @return void */ public static function flushState() { static::$data = []; static::$customDataResolvers = []; } } framework/src/Illuminate/Foundation/Console/RouteClearCommand.php 0000644 00000002216 15060132306 0021215 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.'); } } framework/src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php 0000644 00000006670 15060132306 0023031 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} {--prune : Delete the original environment file} {--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') ? Str::finish(dirname($this->laravel->environmentFilePath()), DIRECTORY_SEPARATOR).'.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; } if ($this->option('prune')) { $this->files->delete($environmentFile); } $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; } } framework/src/Illuminate/Foundation/Console/OptimizeCommand.php 0000644 00000002175 15060132306 0020754 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 framework bootstrap, configuration, and metadata to increase performance'; /** * Execute the console command. * * @return void */ public function handle() { $this->components->info('Caching framework bootstrap, configuration, and metadata.'); collect([ 'config' => fn () => $this->callSilent('config:cache') == 0, 'events' => fn () => $this->callSilent('event:cache') == 0, 'routes' => fn () => $this->callSilent('route:cache') == 0, 'views' => fn () => $this->callSilent('view:cache') == 0, ])->each(fn ($task, $description) => $this->components->task($description, $task)); $this->newLine(); } } framework/src/Illuminate/Foundation/Console/InteractsWithComposerPackages.php 0000644 00000002156 15060132306 0023613 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; trait InteractsWithComposerPackages { /** * Installs the given Composer Packages into the application. * * @param string $composer * @param array $packages * @return bool */ protected function requireComposerPackages(string $composer, array $packages) { if ($composer !== 'global') { $command = [$this->phpBinary(), $composer, 'require']; } $command = array_merge( $command ?? ['composer', 'require'], $packages, ); return ! (new Process($command, $this->laravel->basePath(), ['COMPOSER_MEMORY_LIMIT' => '-1'])) ->setTimeout(null) ->run(function ($type, $output) { $this->output->write($output); }); } /** * Get the path to the appropriate PHP binary. * * @return string */ protected function phpBinary() { return (new PhpExecutableFinder())->find(false) ?: 'php'; } } framework/src/Illuminate/Foundation/Console/EventClearCommand.php 0000644 00000002303 15060132306 0021175 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.'); } } framework/src/Illuminate/Foundation/Console/VendorPublishCommand.php 0000644 00000025370 15060132306 0021742 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 time the command started. * * @var \Illuminate\Support\Carbon|null */ protected $publishedAt; /** * 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'; /** * Indicates if migration dates should be updated while publishing. * * @var bool */ protected static $updateMigrationDates = true; /** * 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->publishedAt = now(); $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))) { $to = $this->ensureMigrationNameIsUpToDate($from, $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($from, 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 string $from * @param \League\Flysystem\MountManager $manager * @return void */ protected function moveManagedFiles($from, $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)) ) ) { $path = $this->ensureMigrationNameIsUpToDate($from, $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); } } /** * Ensure the given migration name is up-to-date. * * @param string $from * @param string $to * @return string */ protected function ensureMigrationNameIsUpToDate($from, $to) { if (static::$updateMigrationDates === false) { return $to; } $from = realpath($from); foreach (ServiceProvider::publishableMigrationPaths() as $path) { $path = realpath($path); if ($from === $path && preg_match('/\d{4}_(\d{2})_(\d{2})_(\d{6})_/', $to)) { $this->publishedAt->addSecond(); return preg_replace( '/\d{4}_(\d{2})_(\d{2})_(\d{6})_/', $this->publishedAt->format('Y_m_d_His').'_', $to, ); } } return $to; } /** * 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, )); } /** * Intruct the command to not update the dates on migrations when publishing. * * @return void */ public static function dontUpdateMigrationDates() { static::$updateMigrationDates = false; } } framework/src/Illuminate/Foundation/Console/ChannelMakeCommand.php 0000644 00000003252 15060132306 0021317 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'], ]; } } framework/src/Illuminate/Foundation/Console/QueuedCommand.php 0000644 00000002011 15060132306 0020371 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]; } } framework/src/Illuminate/Foundation/Console/ConsoleMakeCommand.php 0000644 00000004720 15060132306 0021352 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'], ]; } } framework/src/Illuminate/Foundation/Console/ExceptionMakeCommand.php 0000644 00000005645 15060132306 0021715 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Illuminate\Console\GeneratorCommand; 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}; #[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'; } /** * 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; } $input->setOption('report', confirm('Should the exception have a report method?', default: false)); $input->setOption('render', confirm('Should the exception have a render method?', default: false)); } /** * 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'], ]; } } framework/src/Illuminate/Foundation/Console/ViewCacheCommand.php 0000644 00000005442 15060132306 0021012 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() ); } } framework/src/Illuminate/Foundation/Console/BroadcastingInstallCommand.php 0000644 00000015174 15060132306 0023106 0 ustar 00 <?php namespace Illuminate\Foundation\Console; use Composer\InstalledVersions; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Process; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Process\PhpExecutableFinder; use function Laravel\Prompts\confirm; #[AsCommand(name: 'install:broadcasting')] class BroadcastingInstallCommand extends Command { use InteractsWithComposerPackages; /** * The name and signature of the console command. * * @var string */ protected $signature = 'install:broadcasting {--composer=global : Absolute path to the Composer binary which should be used to install packages} {--force : Overwrite any existing broadcasting routes file} {--without-reverb : Do not prompt to install Laravel Reverb} {--without-node : Do not prompt to install Node dependencies}'; /** * The console command description. * * @var string */ protected $description = 'Create a broadcasting channel routes file'; /** * Execute the console command. * * @return int */ public function handle() { $this->call('config:publish', ['name' => 'broadcasting']); // Install channel routes file... if (! file_exists($broadcastingRoutesPath = $this->laravel->basePath('routes/channels.php')) || $this->option('force')) { $this->components->info("Published 'channels' route file."); copy(__DIR__.'/stubs/broadcasting-routes.stub', $broadcastingRoutesPath); } $this->uncommentChannelsRoutesFile(); $this->enableBroadcastServiceProvider(); // Install bootstrapping... if (! file_exists($echoScriptPath = $this->laravel->resourcePath('js/echo.js'))) { if (! is_dir($directory = $this->laravel->resourcePath('js'))) { mkdir($directory, 0755, true); } copy(__DIR__.'/stubs/echo-js.stub', $echoScriptPath); } if (file_exists($bootstrapScriptPath = $this->laravel->resourcePath('js/bootstrap.js'))) { $bootstrapScript = file_get_contents( $bootstrapScriptPath ); if (! str_contains($bootstrapScript, './echo')) { file_put_contents( $bootstrapScriptPath, trim($bootstrapScript.PHP_EOL.file_get_contents(__DIR__.'/stubs/echo-bootstrap-js.stub')).PHP_EOL, ); } } $this->installReverb(); $this->installNodeDependencies(); } /** * Uncomment the "channels" routes file in the application bootstrap file. * * @return void */ protected function uncommentChannelsRoutesFile() { $appBootstrapPath = $this->laravel->bootstrapPath('app.php'); $content = file_get_contents($appBootstrapPath); if (str_contains($content, '// channels: ')) { (new Filesystem)->replaceInFile( '// channels: ', 'channels: ', $appBootstrapPath, ); } elseif (str_contains($content, 'channels: ')) { return; } elseif (str_contains($content, 'commands: __DIR__.\'/../routes/console.php\',')) { (new Filesystem)->replaceInFile( 'commands: __DIR__.\'/../routes/console.php\',', 'commands: __DIR__.\'/../routes/console.php\','.PHP_EOL.' channels: __DIR__.\'/../routes/channels.php\',', $appBootstrapPath, ); } } /** * Uncomment the "BroadcastServiceProvider" in the application configuration. * * @return void */ protected function enableBroadcastServiceProvider() { $config = ($filesystem = new Filesystem)->get(app()->configPath('app.php')); if (str_contains($config, '// App\Providers\BroadcastServiceProvider::class')) { $filesystem->replaceInFile( '// App\Providers\BroadcastServiceProvider::class', 'App\Providers\BroadcastServiceProvider::class', app()->configPath('app.php'), ); } } /** * Install Laravel Reverb into the application if desired. * * @return void */ protected function installReverb() { if ($this->option('without-reverb') || InstalledVersions::isInstalled('laravel/reverb')) { return; } $install = confirm('Would you like to install Laravel Reverb?', default: true); if (! $install) { return; } $this->requireComposerPackages($this->option('composer'), [ 'laravel/reverb:@beta', ]); $php = (new PhpExecutableFinder())->find(false) ?: 'php'; Process::run([ $php, defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan', 'reverb:install', ]); $this->components->info('Reverb installed successfully.'); } /** * Install and build Node dependencies. * * @return void */ protected function installNodeDependencies() { if ($this->option('without-node') || ! confirm('Would you like to install and build the Node dependencies required for broadcasting?', default: true)) { return; } $this->components->info('Installing and building Node dependencies.'); if (file_exists(base_path('pnpm-lock.yaml'))) { $commands = [ 'pnpm add --save-dev laravel-echo pusher-js', 'pnpm run build', ]; } elseif (file_exists(base_path('yarn.lock'))) { $commands = [ 'yarn add --dev laravel-echo pusher-js', 'yarn run build', ]; } elseif (file_exists(base_path('bun.lockb'))) { $commands = [ 'bun add --dev laravel-echo pusher-js', 'bun run build', ]; } else { $commands = [ 'npm install --save-dev laravel-echo pusher-js', 'npm run build', ]; } $command = Process::command(implode(' && ', $commands)) ->path(base_path()); if (! windows_os()) { $command->tty(true); } if ($command->run()->failed()) { $this->components->warn("Node dependency installation failed. Please run the following commands manually: \n\n".implode(' && ', $commands)); } else { $this->components->info('Node dependencies installed successfully.'); } } } framework/src/Illuminate/Foundation/Console/StorageLinkCommand.php 0000644 00000004072 15060132306 0021374 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; } } framework/src/Illuminate/Foundation/Console/ClassMakeCommand.php 0000644 00000003146 15060132306 0021016 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:class')] class ClassMakeCommand extends GeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:class'; /** * The console command description. * * @var string */ protected $description = 'Create a new class'; /** * The type of class being generated. * * @var string */ protected $type = 'Class'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return $this->option('invokable') ? $this->resolveStubPath('/stubs/class.invokable.stub') : $this->resolveStubPath('/stubs/class.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 console command arguments. * * @return array */ protected function getOptions() { return [ ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable class'], ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the class already exists'], ]; } } framework/src/Illuminate/Foundation/Console/JobMakeCommand.php 0000644 00000003702 15060132306 0020461 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'], ]; } } framework/src/Illuminate/Foundation/Console/RouteListCommand.php 0000644 00000035557 15060132306 0021120 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) { if (Str::contains($sort, ',')) { $sort = explode(',', $sort); } return collect($routes) ->sortBy($sort) ->toArray(); } /** * 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'], ]; } } framework/src/Illuminate/Foundation/Console/ChannelListCommand.php 0000644 00000010455 15060132306 0021360 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 $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; } } framework/src/Illuminate/Foundation/AliasLoader.php 0000755 00000012046 15060132306 0016434 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() { // } } framework/src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php 0000644 00000002235 15060132306 0022075 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'); $callback = function () { if (RefreshDatabaseState::$lazilyRefreshed) { return; } RefreshDatabaseState::$lazilyRefreshed = true; if (property_exists($this, 'mockConsoleOutput')) { $shouldMockOutput = $this->mockConsoleOutput; $this->mockConsoleOutput = false; } $this->baseRefreshDatabase(); if (property_exists($this, 'mockConsoleOutput')) { $this->mockConsoleOutput = $shouldMockOutput; } }; $database->beforeStartingTransaction($callback); $database->beforeExecuting($callback); $this->beforeApplicationDestroyed(function () { RefreshDatabaseState::$lazilyRefreshed = false; }); } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDeprecationHandling.php 0000644 00000002250 15060132306 0026047 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, fn () => $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; } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php 0000644 00000002176 15060132306 0023577 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; } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php 0000644 00000012511 15060132306 0024070 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 extends Vite { public function __invoke($entrypoints, $buildDirectory = null) { return new HtmlString(''); } public function __call($method, $parameters) { return ''; } public function __toString() { return ''; } public function useIntegrityKey($key) { return $this; } public function useBuildDirectory($path) { return $this; } public function useHotFile($path) { return $this; } public function withEntryPoints($entryPoints) { return $this; } public function useScriptTagAttributes($attributes) { return $this; } public function useStyleTagAttributes($attributes) { return $this; } public function usePreloadTagAttributes($attributes) { return $this; } public function preloadedAssets() { return []; } public function reactRefresh() { return ''; } public function content($asset, $buildDirectory = null) { return ''; } public function asset($asset, $buildDirectory = null) { 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; } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTestCaseLifecycle.php 0000644 00000017570 15060132306 0025513 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Carbon\CarbonImmutable; use Illuminate\Console\Application as Artisan; use Illuminate\Cookie\Middleware\EncryptCookies; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Bootstrap\HandleExceptions; use Illuminate\Foundation\Bootstrap\RegisterProviders; use Illuminate\Foundation\Console\AboutCommand; use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull; use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance; use Illuminate\Foundation\Http\Middleware\TrimStrings; use Illuminate\Foundation\Http\Middleware\ValidateCsrfToken; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTruncation; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Http\Middleware\TrustHosts; use Illuminate\Http\Middleware\TrustProxies; use Illuminate\Queue\Queue; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\ParallelTesting; use Illuminate\Support\Once; use Illuminate\Support\Sleep; use Illuminate\Support\Str; use Illuminate\View\Component; use Mockery; use Mockery\Exception\InvalidCountException; use PHPUnit\Metadata\Annotation\Parser\Registry as PHPUnitRegistry; use Throwable; trait InteractsWithTestCaseLifecycle { /** * 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; /** * Setup the test environment. * * @internal * * @return void */ protected function setUpTheTestEnvironment(): void { 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; } /** * Clean up the testing environment before the next test. * * @internal * * @return void */ protected function tearDownTheTestEnvironment(): 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 = []; if (property_exists($this, 'originalExceptionHandler')) { $this->originalExceptionHandler = null; } if (property_exists($this, 'originalDeprecationHandler')) { $this->originalDeprecationHandler = null; } AboutCommand::flushState(); Artisan::forgetBootstrappers(); Component::flushCache(); Component::forgetComponentsResolver(); Component::forgetFactory(); ConvertEmptyStringsToNull::flushState(); EncryptCookies::flushState(); HandleExceptions::flushState(); Once::flush(); PreventRequestsDuringMaintenance::flushState(); Queue::createPayloadUsing(null); RegisterProviders::flushState(); Sleep::fake(false); TrimStrings::flushState(); TrustProxies::flushState(); TrustHosts::flushState(); ValidateCsrfToken::flushState(); if ($this->callbackException) { throw $this->callbackException; } } /** * 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[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; } /** * Clean up the testing environment before the next test case. * * @internal * * @return void */ public static function tearDownAfterClassUsingTestCase() { (function () { $this->classDocBlocks = []; $this->methodDocBlocks = []; })->call(PHPUnitRegistry::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; } } } } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php 0000644 00000004532 15060132306 0023247 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; } } framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php 0000644 00000046230 15060132306 0023076 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 from a given URL 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 referer header and previous URL session value from a given route in order to simulate a previous request. * * @param string $name * @param mixed $parameters * @return $this */ public function fromRoute(string $name, $parameters = []) { return $this->from($this->app['url']->route($name, $parameters)); } /** * 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 = $this->createTestRequest($symfonyRequest) ); $kernel->terminate($request, $response); if ($this->followRedirects) { $response = $this->followRedirects($response); } return static::$latestResponse = $this->createTestResponse($response, $request); } /** * 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 request instance used for testing from the given Symfony request. * * @param \Symfony\Component\HttpFoundation\Request $symfonyRequest * @return \Illuminate\Http\Request */ protected function createTestRequest($symfonyRequest) { return Request::createFromBase($symfonyRequest); } /** * Create the test response instance from the given response. * * @param \Illuminate\Http\Response $response * @param \Illuminate\Http\Request $request * @return \Illuminate\Testing\TestResponse */ protected function createTestResponse($response, $request) { return tap(TestResponse::fromBaseResponse($response, $request), function ($response) { $response->withExceptions( $this->app->bound(LoggedExceptionCollection::class) ? $this->app->make(LoggedExceptionCollection::class) : new LoggedExceptionCollection ); }); } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php 0000644 00000022744 15060132306 0023663 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; } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php 0000644 00000002772 15060132306 0023054 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(); } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithRedis.php 0000644 00000006016 15060132306 0023217 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 array<string, \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(); } } framework/src/Illuminate/Foundation/Testing/Concerns/WithoutExceptionHandlingHandler.php 0000644 00000000157 15060132306 0025725 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; interface WithoutExceptionHandlingHandler { // } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php 0000644 00000014450 15060132306 0025555 0 ustar 00 <?php namespace Illuminate\Foundation\Testing\Concerns; use Closure; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Support\Testing\Fakes\ExceptionHandlerFake; 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) { $currentExceptionHandler = app(ExceptionHandler::class); $currentExceptionHandler instanceof ExceptionHandlerFake ? $currentExceptionHandler->setHandler($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) { $currentExceptionHandler = app(ExceptionHandler::class); $this->originalExceptionHandler = $currentExceptionHandler instanceof ExceptionHandlerFake ? $currentExceptionHandler->handler() : $currentExceptionHandler; } $exceptionHandler = new class($this->originalExceptionHandler, $except) implements ExceptionHandler, WithoutExceptionHandlingHandler { 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); } }; $currentExceptionHandler = app(ExceptionHandler::class); $currentExceptionHandler instanceof ExceptionHandlerFake ? $currentExceptionHandler->setHandler($exceptionHandler) : $this->app->instance(ExceptionHandler::class, $exceptionHandler); 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; } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithAuthentication.php 0000644 00000007656 15060132306 0025143 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); } } framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php 0000644 00000004126 15060132306 0023553 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; /** * Indicates if the command is expected to output anything. * * @var bool|null */ public $expectsOutput; /** * 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; } } framework/src/Illuminate/Foundation/Testing/DatabaseTruncation.php 0000644 00000012011 15060132306 0021431 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->getSchemaBuilder()->getTableListing()) ->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 { $migrations = $this->app['config']->get('database.migrations'); $migrationsTable = is_array($migrations) ? ($migrations['table'] ?? null) : $migrations; if (property_exists($this, 'exceptTables')) { if (array_is_list($this->exceptTables ?? [])) { return array_merge( $this->exceptTables ?? [], [$migrationsTable], ); } return array_merge( $this->exceptTables[$connectionName] ?? [], [$migrationsTable], ); } return [$migrationsTable]; } /** * 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 { // } } framework/src/Illuminate/Foundation/Testing/Wormhole.php 0000644 00000012245 15060132306 0017463 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(); }); } } } framework/src/Illuminate/Foundation/Testing/Traits/CanConfigureMigrationCommands.php 0000644 00000003037 15060132306 0025033 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; } } framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php 0000644 00000002554 15060132306 0021432 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->beforeRefreshingDatabase(); $this->refreshTestDatabase(); $this->afterRefreshingDatabase(); $this->beforeApplicationDestroyed(function () { $this->artisan('migrate:rollback'); RefreshDatabaseState::$migrated = false; }); } /** * Refresh a conventional test database. * * @return void */ protected function refreshTestDatabase() { $this->artisan('migrate:fresh', $this->migrateFreshUsing()); $this->app[Kernel::class]->setArtisan(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() { // ... } } framework/src/Illuminate/Foundation/Testing/DatabaseTransactions.php 0000644 00000003074 15060132306 0021764 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]; } } framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php 0000644 00000007501 15060132306 0020711 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(); if ($this->usingInMemoryDatabase()) { $this->restoreInMemoryDatabase(); } $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:'; } /** * Restore the in-memory database between tests. * * @return void */ protected function restoreInMemoryDatabase() { $database = $this->app->make('db'); foreach ($this->connectionsToTransact() as $name) { if (isset(RefreshDatabaseState::$inMemoryConnections[$name])) { $database->connection($name)->setPdo(RefreshDatabaseState::$inMemoryConnections[$name]); } } } /** * 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); if ($this->usingInMemoryDatabase()) { RefreshDatabaseState::$inMemoryConnections[$name] ??= $connection->getPdo(); } $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() { // ... } } framework/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php 0000644 00000001004 15060132306 0021702 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; class RefreshDatabaseState { /** * The current SQLite in-memory database connections. * * @var array<string, \PDO> */ public static $inMemoryConnections = []; /** * 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; } framework/src/Illuminate/Foundation/Testing/DatabaseTransactionsManager.php 0000644 00000002561 15060132306 0023257 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->pendingTransactions->last()->addCallback($callback); } /** * Get the transactions that are applicable to callbacks. * * @return \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord> */ public function callbackApplicableTransactions() { return $this->pendingTransactions->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; } } framework/src/Illuminate/Foundation/Testing/WithConsoleEvents.php 0000644 00000000530 15060132306 0021304 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(); } } framework/src/Illuminate/Foundation/Testing/WithoutMiddleware.php 0000644 00000000764 15060132306 0021333 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.'); } } } framework/src/Illuminate/Foundation/Testing/TestCase.php 0000644 00000004315 15060132306 0017401 0 ustar 00 <?php namespace Illuminate\Foundation\Testing; use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Application; 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\InteractsWithTestCaseLifecycle, Concerns\InteractsWithViews; /** * Creates the application. * * @return \Illuminate\Foundation\Application */ public function createApplication() { $app = require Application::inferBasePath().'/bootstrap/app.php'; $app->make(Kernel::class)->bootstrap(); return $app; } /** * Setup the test environment. * * @return void */ protected function setUp(): void { static::$latestResponse = null; $this->setUpTheTestEnvironment(); } /** * Refresh the application instance. * * @return void */ protected function refreshApplication() { $this->app = $this->createApplication(); } /** * {@inheritdoc} */ protected function transformException(Throwable $error): Throwable { $response = static::$latestResponse ?? null; if (! is_null($response)) { $response->transformNotSuccessfulException($error); } return $error; } /** * Clean up the testing environment before the next test. * * @return void * * @throws \Mockery\Exception\InvalidCountException */ protected function tearDown(): void { $this->tearDownTheTestEnvironment(); } /** * Clean up the testing environment before the next test case. * * @return void */ public static function tearDownAfterClass(): void { static::$latestResponse = null; static::tearDownAfterClassUsingTestCase(); } } framework/src/Illuminate/Foundation/Testing/WithFaker.php 0000644 00000002325 15060132306 0017551 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); } } framework/src/Illuminate/Foundation/Mix.php 0000644 00000004166 15060132306 0015012 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 \Illuminate\Foundation\MixManifestNotFoundException */ 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 MixManifestNotFoundException("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]); } } framework/src/Illuminate/Foundation/Exceptions/RegisterErrorViewPaths.php 0000644 00000000647 15060132306 0023027 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()); } } framework/src/Illuminate/Foundation/Exceptions/views/404.blade.php 0000644 00000000175 15060132306 0021104 0 ustar 00 @extends('errors::minimal') @section('title', __('Not Found')) @section('code', '404') @section('message', __('Not Found')) framework/src/Illuminate/Foundation/Exceptions/views/layout.blade.php 0000644 00000002633 15060132306 0022113 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> framework/src/Illuminate/Foundation/Exceptions/views/500.blade.php 0000644 00000000203 15060132306 0021071 0 ustar 00 @extends('errors::minimal') @section('title', __('Server Error')) @section('code', '500') @section('message', __('Server Error')) framework/src/Illuminate/Foundation/Exceptions/views/429.blade.php 0000644 00000000215 15060132306 0021106 0 ustar 00 @extends('errors::minimal') @section('title', __('Too Many Requests')) @section('code', '429') @section('message', __('Too Many Requests')) framework/src/Illuminate/Foundation/Exceptions/views/401.blade.php 0000644 00000000203 15060132306 0021071 0 ustar 00 @extends('errors::minimal') @section('title', __('Unauthorized')) @section('code', '401') @section('message', __('Unauthorized')) framework/src/Illuminate/Foundation/Exceptions/views/402.blade.php 0000644 00000000213 15060132306 0021073 0 ustar 00 @extends('errors::minimal') @section('title', __('Payment Required')) @section('code', '402') @section('message', __('Payment Required')) framework/src/Illuminate/Foundation/Exceptions/views/minimal.blade.php 0000644 00000014746 15060132306 0022234 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> framework/src/Illuminate/Foundation/Exceptions/views/419.blade.php 0000644 00000000203 15060132306 0021102 0 ustar 00 @extends('errors::minimal') @section('title', __('Page Expired')) @section('code', '419') @section('message', __('Page Expired')) framework/src/Illuminate/Foundation/Exceptions/views/403.blade.php 0000644 00000000231 15060132306 0021074 0 ustar 00 @extends('errors::minimal') @section('title', __('Forbidden')) @section('code', '403') @section('message', __($exception->getMessage() ?: 'Forbidden')) framework/src/Illuminate/Foundation/Exceptions/views/503.blade.php 0000644 00000000221 15060132306 0021074 0 ustar 00 @extends('errors::minimal') @section('title', __('Service Unavailable')) @section('code', '503') @section('message', __('Service Unavailable')) framework/src/Illuminate/Foundation/Exceptions/Renderer/Mappers/BladeMapper.php 0000644 00000024431 15060132306 0023724 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions\Renderer\Mappers; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\View\Compilers\BladeCompiler; use Illuminate\View\ViewException; use ReflectionClass; use ReflectionProperty; use Symfony\Component\ErrorHandler\Exception\FlattenException; use Throwable; /* * This file contains parts of https://github.com/spatie/laravel-ignition. * * (c) Spatie <info@spatie.be> * * For the full copyright and license information, please review its LICENSE: * * The MIT License (MIT) * * Copyright (c) Spatie <info@spatie.be> * * 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. */ class BladeMapper { /** * The view factory instance. * * @var \Illuminate\Contracts\View\Factory */ protected $factory; /** * The Blade compiler instance. * * @var \Illuminate\View\Compilers\BladeCompiler */ protected $bladeCompiler; /** * Create a new Blade mapper instance. * * @param \Illuminate\Contracts\View\Factory $factory * @param \Illuminate\View\Compilers\BladeCompiler $bladeCompiler * @return void */ public function __construct(Factory $factory, BladeCompiler $bladeCompiler) { $this->factory = $factory; $this->bladeCompiler = $bladeCompiler; } /** * Map cached view paths to their original paths. * * @param \Symfony\Component\ErrorHandler\Exception\FlattenException $exception * @return \Symfony\Component\ErrorHandler\Exception\FlattenException */ public function map(FlattenException $exception) { while ($exception->getClass() === ViewException::class) { if (($previous = $exception->getPrevious()) === null) { break; } $exception = $previous; } $trace = Collection::make($exception->getTrace()) ->map(function ($frame) { if ($originalPath = $this->findCompiledView((string) Arr::get($frame, 'file', ''))) { $frame['file'] = $originalPath; $frame['line'] = $this->detectLineNumber($frame['file'], $frame['line']); } return $frame; })->toArray(); return tap($exception, fn () => (fn () => $this->trace = $trace)->call($exception)); } /** * Find the compiled view file for the given compiled path. * * @param string $compiledPath * @return string|null */ protected function findCompiledView(string $compiledPath) { return once(fn () => $this->getKnownPaths())[$compiledPath] ?? null; } /** * Get the list of known paths from the compiler engine. * * @return array<string, string> */ protected function getKnownPaths() { $compilerEngineReflection = new ReflectionClass( $bladeCompilerEngine = $this->factory->getEngineResolver()->resolve('blade'), ); if (! $compilerEngineReflection->hasProperty('lastCompiled') && $compilerEngineReflection->hasProperty('engine')) { $compilerEngine = $compilerEngineReflection->getProperty('engine'); $compilerEngine->setAccessible(true); $compilerEngine = $compilerEngine->getValue($bladeCompilerEngine); $lastCompiled = new ReflectionProperty($compilerEngine, 'lastCompiled'); $lastCompiled->setAccessible(true); $lastCompiled = $lastCompiled->getValue($compilerEngine); } else { $lastCompiled = $compilerEngineReflection->getProperty('lastCompiled'); $lastCompiled->setAccessible(true); $lastCompiled = $lastCompiled->getValue($bladeCompilerEngine); } $knownPaths = []; foreach ($lastCompiled as $lastCompiledPath) { $compiledPath = $bladeCompilerEngine->getCompiler()->getCompiledPath($lastCompiledPath); $knownPaths[realpath($compiledPath ?? $lastCompiledPath)] = realpath($lastCompiledPath); } return $knownPaths; } /** * Filter out the view data that should not be shown in the exception report. * * @param array<string, mixed> $data * @return array<string, mixed> */ protected function filterViewData(array $data) { return array_filter($data, function ($value, $key) { if ($key === 'app') { return ! $value instanceof Application; } return $key !== '__env'; }, ARRAY_FILTER_USE_BOTH); } /** * Detect the line number in the original blade file. * * @param string $filename * @param int $compiledLineNumber * @return int */ protected function detectLineNumber(string $filename, int $compiledLineNumber) { $map = $this->compileSourcemap((string) file_get_contents($filename)); return $this->findClosestLineNumberMapping($map, $compiledLineNumber); } /** * Compile the source map for the given blade file. * * @param string $value * @return string */ protected function compileSourcemap(string $value) { try { $value = $this->addEchoLineNumbers($value); $value = $this->addStatementLineNumbers($value); $value = $this->addBladeComponentLineNumbers($value); $value = $this->bladeCompiler->compileString($value); return $this->trimEmptyLines($value); } catch (Throwable $e) { report($e); return $value; } } /** * Add line numbers to echo statements. * * @param string $value * @return string */ protected function addEchoLineNumbers(string $value) { $echoPairs = [['{{', '}}'], ['{{{', '}}}'], ['{!!', '!!}']]; foreach ($echoPairs as $pair) { // Matches {{ $value }}, {!! $value !!} and {{{ $value }}} depending on $pair $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $pair[0], $pair[1]); if (preg_match_all($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { foreach (array_reverse($matches[0]) as $match) { $position = mb_strlen(substr($value, 0, $match[1])); $value = $this->insertLineNumberAtPosition($position, $value); } } } return $value; } /** * Add line numbers to blade statements. * * @param string $value * @return string */ protected function addStatementLineNumbers(string $value) { $shouldInsertLineNumbers = preg_match_all( '/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $value, $matches, PREG_OFFSET_CAPTURE ); if ($shouldInsertLineNumbers) { foreach (array_reverse($matches[0]) as $match) { $position = mb_strlen(substr($value, 0, $match[1])); $value = $this->insertLineNumberAtPosition($position, $value); } } return $value; } /** * Add line numbers to blade components. * * @param string $value * @return string */ protected function addBladeComponentLineNumbers(string $value) { $shouldInsertLineNumbers = preg_match_all( '/<\s*x[-:]([\w\-:.]*)/mx', $value, $matches, PREG_OFFSET_CAPTURE ); if ($shouldInsertLineNumbers) { foreach (array_reverse($matches[0]) as $match) { $position = mb_strlen(substr($value, 0, $match[1])); $value = $this->insertLineNumberAtPosition($position, $value); } } return $value; } /** * Insert a line number at the given position. * * @param int $position * @param string $value * @return string */ protected function insertLineNumberAtPosition(int $position, string $value) { $before = mb_substr($value, 0, $position); $lineNumber = count(explode("\n", $before)); return mb_substr($value, 0, $position)."|---LINE:{$lineNumber}---|".mb_substr($value, $position); } /** * Trim empty lines from the given value. * * @param string $value * @return string */ protected function trimEmptyLines(string $value) { $value = preg_replace('/^\|---LINE:([0-9]+)---\|$/m', '', $value); return ltrim((string) $value, PHP_EOL); } /** * Find the closest line number mapping in the given source map. * * @param string $map * @param int $compiledLineNumber * @return int */ protected function findClosestLineNumberMapping(string $map, int $compiledLineNumber) { $map = explode("\n", $map); $maxDistance = 20; $pattern = '/\|---LINE:(?P<line>[0-9]+)---\|/m'; $lineNumberToCheck = $compiledLineNumber - 1; while (true) { if ($lineNumberToCheck < $compiledLineNumber - $maxDistance) { return min($compiledLineNumber, count($map)); } if (preg_match($pattern, $map[$lineNumberToCheck] ?? '', $matches)) { return (int) $matches['line']; } $lineNumberToCheck--; } } } framework/src/Illuminate/Foundation/Exceptions/Renderer/Renderer.php 0000644 00000007427 15060132306 0021715 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions\Renderer; use Illuminate\Contracts\View\Factory; use Illuminate\Foundation\Exceptions\Renderer\Mappers\BladeMapper; use Illuminate\Http\Request; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; use Throwable; class Renderer { /** * The path to the renderer's distribution files. * * @var string */ protected const DIST = __DIR__.'/../../resources/exceptions/renderer/dist/'; /** * The view factory instance. * * @var \Illuminate\Contracts\View\Factory */ protected $viewFactory; /** * The exception listener instance. * * @var \Illuminate\Foundation\Exceptions\Renderer\Listener */ protected $listener; /** * The HTML error renderer instance. * * @var \Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer */ protected $htmlErrorRenderer; /** * The Blade mapper instance. * * @var \Illuminate\Foundation\Exceptions\Renderer\Mappers\BladeMapper */ protected $bladeMapper; /** * The application's base path. * * @var string */ protected $basePath; /** * Creates a new exception renderer instance. * * @param \Illuminate\Contracts\View\Factory $viewFactory * @param \Illuminate\Foundation\Exceptions\Renderer\Listener $listener * @param \Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer $htmlErrorRenderer * @param \Illuminate\Foundation\Exceptions\Renderer\Mappers\BladeMapper $bladeMapper * @param string $basePath * @return void */ public function __construct( Factory $viewFactory, Listener $listener, HtmlErrorRenderer $htmlErrorRenderer, BladeMapper $bladeMapper, string $basePath ) { $this->viewFactory = $viewFactory; $this->listener = $listener; $this->htmlErrorRenderer = $htmlErrorRenderer; $this->bladeMapper = $bladeMapper; $this->basePath = $basePath; } /** * Render the given exception as an HTML string. * * @param \Illuminate\Http\Request $request * @param \Throwable $throwable * @return string */ public function render(Request $request, Throwable $throwable) { $flattenException = $this->bladeMapper->map( $this->htmlErrorRenderer->render($throwable), ); return $this->viewFactory->make('laravel-exceptions-renderer::show', [ 'exception' => new Exception($flattenException, $request, $this->listener, $this->basePath), ])->render(); } /** * Get the renderer's CSS content. * * @return string */ public static function css() { return collect([ ['styles.css', []], ['light-mode.css', ['data-theme' => 'light']], ['dark-mode.css', ['data-theme' => 'dark']], ])->map(function ($fileAndAttributes) { [$filename, $attributes] = $fileAndAttributes; return '<style '.collect($attributes)->map(function ($value, $attribute) { return $attribute.'="'.$value.'"'; })->implode(' ').'>' .file_get_contents(static::DIST.$filename) .'</style>'; })->implode(''); } /** * Get the renderer's JavaScript content. * * @return string */ public static function js() { $viteJsAutoRefresh = ''; $vite = app(\Illuminate\Foundation\Vite::class); if (is_file($vite->hotFile())) { $viteJsAutoRefresh = $vite->__invoke([]); } return '<script type="text/javascript">' .file_get_contents(static::DIST.'scripts.js') .'</script>'.$viteJsAutoRefresh; } } framework/src/Illuminate/Foundation/Exceptions/Renderer/Listener.php 0000644 00000003767 15060132306 0021737 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions\Renderer; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Laravel\Octane\Events\RequestReceived; use Laravel\Octane\Events\RequestTerminated; use Laravel\Octane\Events\TaskReceived; use Laravel\Octane\Events\TickReceived; class Listener { /** * The queries that have been executed. * * @var array<int, array{connectionName: string, time: float, sql: string, bindings: array}> */ protected $queries = []; /** * Register the appropriate listeners on the given event dispatcher. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function registerListeners(Dispatcher $events) { $events->listen(QueryExecuted::class, [$this, 'onQueryExecuted']); $events->listen([JobProcessing::class, JobProcessed::class], function () { $this->queries = []; }); if (isset($_SERVER['LARAVEL_OCTANE'])) { $events->listen([RequestReceived::class, TaskReceived::class, TickReceived::class, RequestTerminated::class], function () { $this->queries = []; }); } } /** * Returns the queries that have been executed. * * @return array<int, array{sql: string, time: float}> */ public function queries() { return $this->queries; } /** * Listens for the query executed event. * * @param \Illuminate\Database\Events\QueryExecuted $event * @return void */ public function onQueryExecuted(QueryExecuted $event) { if (count($this->queries) === 100) { return; } $this->queries[] = [ 'connectionName' => $event->connectionName, 'time' => $event->time, 'sql' => $event->sql, 'bindings' => $event->bindings, ]; } } framework/src/Illuminate/Foundation/Exceptions/Renderer/Frame.php 0000644 00000006702 15060132306 0021174 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions\Renderer; use Illuminate\Foundation\Concerns\ResolvesDumpSource; use Symfony\Component\ErrorHandler\Exception\FlattenException; class Frame { use ResolvesDumpSource; /** * The "flattened" exception instance. * * @var \Symfony\Component\ErrorHandler\Exception\FlattenException */ protected $exception; /** * The application's class map. * * @var array<string, string> */ protected $classMap; /** * The frame's raw data from the "flattened" exception. * * @var array{file: string, line: int, class?: string, type?: string, function?: string} */ protected $frame; /** * The application's base path. * * @var string */ protected $basePath; /** * Create a new frame instance. * * @param \Symfony\Component\ErrorHandler\Exception\FlattenException $exception * @param array<string, string> $classMap * @param array{file: string, line: int, class?: string, type?: string, function?: string} $frame * @param string $basePath * @return void */ public function __construct(FlattenException $exception, array $classMap, array $frame, string $basePath) { $this->exception = $exception; $this->classMap = $classMap; $this->frame = $frame; $this->basePath = $basePath; } /** * Get the frame's source / origin. * * @return string */ public function source() { return match (true) { is_string($this->class()) => $this->class(), default => $this->file(), }; } /** * Get the frame's editor link. * * @return string */ public function editorHref() { return $this->resolveSourceHref($this->frame['file'], $this->line()); } /** * Get the frame's class, if any. * * @return string|null */ public function class() { $class = array_search((string) realpath($this->frame['file']), $this->classMap, true); return $class === false ? null : $class; } /** * Get the frame's file. * * @return string */ public function file() { return str_replace($this->basePath.'/', '', $this->frame['file']); } /** * Get the frame's line number. * * @return int */ public function line() { $maxLines = count(file($this->frame['file']) ?: []); return $this->frame['line'] > $maxLines ? 1 : $this->frame['line']; } /** * Get the frame's function or method. * * @return string */ public function callable() { return match (true) { ! empty($this->frame['function']) => $this->frame['function'], default => 'throw', }; } /** * Get the frame's code snippet. * * @return string */ public function snippet() { $contents = file($this->frame['file']) ?: []; $start = max($this->line() - 6, 0); $length = 8 * 2 + 1; return implode('', array_slice($contents, $start, $length)); } /** * Determine if the frame is from the vendor directory. * * @return bool */ public function isFromVendor() { return ! str_starts_with($this->frame['file'], $this->basePath) || str_starts_with($this->frame['file'], $this->basePath.'/vendor'); } } framework/src/Illuminate/Foundation/Exceptions/Renderer/Exception.php 0000644 00000013355 15060132306 0022102 0 ustar 00 <?php namespace Illuminate\Foundation\Exceptions\Renderer; use Closure; use Composer\Autoload\ClassLoader; use Illuminate\Foundation\Bootstrap\HandleExceptions; use Illuminate\Http\Request; use Symfony\Component\ErrorHandler\Exception\FlattenException; class Exception { /** * The "flattened" exception instance. * * @var \Symfony\Component\ErrorHandler\Exception\FlattenException */ protected $exception; /** * The current request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * The exception listener instance. * * @var \Illuminate\Foundation\Exceptions\Renderer\Listener */ protected $listener; /** * The application's base path. * * @var string */ protected $basePath; /** * Creates a new exception renderer instance. * * @param \Symfony\Component\ErrorHandler\Exception\FlattenException $exception * @param \Illuminate\Http\Request $request * @param \Illuminate\Foundation\Exceptions\Renderer\Listener $listener * @param string $basePath * @return void */ public function __construct(FlattenException $exception, Request $request, Listener $listener, string $basePath) { $this->exception = $exception; $this->request = $request; $this->listener = $listener; $this->basePath = $basePath; } /** * Get the exception title. * * @return string */ public function title() { return $this->exception->getStatusText(); } /** * Get the exception message. * * @return string */ public function message() { return $this->exception->getMessage(); } /** * Get the exception class name. * * @return string */ public function class() { return $this->exception->getClass(); } /** * Get the first "non-vendor" frame index. * * @return int */ public function defaultFrame() { $key = array_search(false, array_map(function (Frame $frame) { return $frame->isFromVendor(); }, $this->frames()->all())); return $key === false ? 0 : $key; } /** * Get the exception's frames. * * @return \Illuminate\Support\Collection<int, Frame> */ public function frames() { $classMap = once(fn () => array_map(function ($path) { return (string) realpath($path); }, array_values(ClassLoader::getRegisteredLoaders())[0]->getClassMap())); $trace = array_values(array_filter( $this->exception->getTrace(), fn ($trace) => isset($trace['file']), )); if (($trace[1]['class'] ?? '') === HandleExceptions::class) { array_shift($trace); array_shift($trace); } return collect(array_map( fn (array $trace) => new Frame($this->exception, $classMap, $trace, $this->basePath), $trace, )); } /** * Get the exception's request instance. * * @return \Illuminate\Http\Request */ public function request() { return $this->request; } /** * Get the request's headers. * * @return array<string, string> */ public function requestHeaders() { return array_map(function (array $header) { return implode(', ', $header); }, $this->request()->headers->all()); } /** * Get the request's body parameters. * * @return string|null */ public function requestBody() { if (empty($payload = $this->request()->all())) { return null; } $json = (string) json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); return str_replace('\\', '', $json); } /** * Get the application's route context. * * @return array<string, string> */ public function applicationRouteContext() { $route = $this->request()->route(); return $route ? array_filter([ 'controller' => $route->getActionName(), 'route name' => $route->getName() ?: null, 'middleware' => implode(', ', array_map(function ($middleware) { return $middleware instanceof Closure ? 'Closure' : $middleware; }, $route->gatherMiddleware())), ]) : []; } /** * Get the application's route parameters context. * * @return array<string, mixed>|null */ public function applicationRouteParametersContext() { $parameters = $this->request()->route()?->parameters(); return $parameters ? json_encode(array_map( fn ($value) => $value instanceof Model ? $value->withoutRelations() : $value, $parameters ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : null; } /** * Get the application's SQL queries. * * @return array<int, array{connectionName: string, time: float, sql: string}> */ public function applicationQueries() { return array_map(function (array $query) { $sql = $query['sql']; foreach ($query['bindings'] as $binding) { $sql = match (gettype($binding)) { 'integer', 'double' => preg_replace('/\?/', $binding, $sql, 1), 'NULL' => preg_replace('/\?/', 'NULL', $sql, 1), default => preg_replace('/\?/', "'$binding'", $sql, 1), }; } return [ 'connectionName' => $query['connectionName'], 'time' => $query['time'], 'sql' => $sql, ]; }, $this->listener->queries()); } } framework/src/Illuminate/Foundation/Exceptions/Handler.php 0000644 00000074537 15060132306 0017764 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\Foundation\Exceptions\Renderer\Renderer; 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\RequestExceptionInterface; 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\BadRequestHttpException; 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 to throttle reportable exceptions. * * @var array */ protected $throttleCallbacks = []; /** * The callbacks that should be used to build exception context data. * * @var array */ protected $contextCallbacks = []; /** * The callbacks that should be used during rendering. * * @var \Closure[] */ protected $renderCallbacks = []; /** * The callback that determines if the exception handler response should be JSON. * * @var callable|null */ protected $shouldRenderJsonWhenCallback; /** * The callback that prepares responses to be returned to the browser. * * @var callable|null */ protected $finalizeResponseCallback; /** * 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, RequestExceptionInterface::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. * * Alias of "ignore". * * @param array|string $exceptions * @return $this */ public function dontReport(array|string $exceptions) { return $this->ignore($exceptions); } /** * Indicate that the given exception type should not be reported. * * @param array|string $exceptions * @return $this */ public function ignore(array|string $exceptions) { $exceptions = Arr::wrap($exceptions); $this->dontReport = array_values(array_unique(array_merge($this->dontReport, $exceptions))); return $this; } /** * Indicate that the given attributes should never be flashed to the session on validation errors. * * @param array|string $attributes * @return $this */ public function dontFlash(array|string $attributes) { $this->dontFlash = array_values(array_unique( array_merge($this->dontFlash, Arr::wrap($attributes)) )); 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, $throttle->decaySeconds ); }), 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) { foreach ($this->throttleCallbacks as $throttleCallback) { foreach ($this->firstClosureParameterTypes($throttleCallback) as $type) { if (is_a($e, $type)) { $response = $throttleCallback($e); if (! is_null($response)) { return $response; } } } } return Limit::none(); } /** * Specify the callback that should be used to throttle reportable exceptions. * * @param callable $throttleUsing * @return $this */ public function throttleUsing(callable $throttleUsing) { if (! $throttleUsing instanceof Closure) { $throttleUsing = Closure::fromCallable($throttleUsing); } $this->throttleCallbacks[] = $throttleUsing; return $this; } /** * Remove the given exception class from the list of exceptions that should be ignored. * * @param array|string $exceptions * @return $this */ public function stopIgnoring(array|string $exceptions) { $exceptions = Arr::wrap($exceptions); $this->dontReport = collect($this->dontReport) ->reject(fn ($ignored) => in_array($ignored, $exceptions))->values()->all(); $this->internalDontReport = collect($this->internalDontReport) ->reject(fn ($ignored) => in_array($ignored, $exceptions))->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) { $context = []; if (method_exists($e, 'context')) { $context = $e->context(); } foreach ($this->contextCallbacks as $callback) { $context = array_merge($context, $callback($e, $context)); } return $context; } /** * Get the default context variables for logging. * * @return array */ protected function context() { try { return array_filter([ 'userId' => Auth::id(), ]); } catch (Throwable) { return []; } } /** * Register a closure that should be used to build exception context data. * * @param \Closure $contextCallback * @return $this */ public function buildContextUsing(Closure $contextCallback) { $this->contextCallbacks[] = $contextCallback; return $this; } /** * 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 $this->finalizeRenderedResponse( $request, Router::toResponse($request, $response), $e ); } if ($e instanceof Responsable) { return $this->finalizeRenderedResponse($request, $e->toResponse($request), $e); } $e = $this->prepareException($e); if ($response = $this->renderViaCallbacks($request, $e)) { return $this->finalizeRenderedResponse($request, $response, $e); } return $this->finalizeRenderedResponse($request, 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), }, $e); } /** * Prepare the final, rendered response to be returned to the browser. * * @param \Illuminate\Http\Request $request * @param \Symfony\Component\HttpFoundation\Response $response * @param \Throwable $e * @return \Symfony\Component\HttpFoundation\Response */ protected function finalizeRenderedResponse($request, $response, Throwable $e) { return $this->finalizeResponseCallback ? call_user_func($this->finalizeResponseCallback, $response, $e, $request) : $response; } /** * Prepare the final, rendered response for an exception using the given callback. * * @param callable $callback * @return $this */ public function respondUsing($callback) { $this->finalizeResponseCallback = $callback; return $this; } /** * 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 RequestExceptionInterface => new BadRequestHttpException('Bad request.', $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($request) ?? 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 $this->shouldRenderJsonWhenCallback ? call_user_func($this->shouldRenderJsonWhenCallback, $request, $e) : $request->expectsJson(); } /** * Register the callable that determines if the exception handler response should be JSON. * * @param callable(\Illuminate\Http\Request $request, \Throwable): bool $callback * @return $this */ public function shouldRenderJsonWhen($callback) { $this->shouldRenderJsonWhenCallback = $callback; return $this; } /** * 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 { if (config('app.debug')) { if (app()->has(ExceptionRenderer::class)) { return $this->renderExceptionWithCustomRenderer($e); } elseif ($this->container->bound(Renderer::class)) { return $this->container->make(Renderer::class)->render(request(), $e); } } return $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($alternatives); $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); } } framework/src/Illuminate/Foundation/Exceptions/ReportableHandler.php 0000644 00000003037 15060132306 0021767 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; } } framework/src/Illuminate/Foundation/Exceptions/Whoops/WhoopsHandler.php 0000644 00000004063 15060132306 0022426 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; } } framework/src/Illuminate/Foundation/Exceptions/Whoops/WhoopsExceptionRenderer.php 0000644 00000001523 15060132306 0024474 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(); } } framework/src/Illuminate/Foundation/ViteManifestNotFoundException.php 0000644 00000000172 15060132306 0022200 0 ustar 00 <?php namespace Illuminate\Foundation; use Exception; class ViteManifestNotFoundException extends Exception { // } framework/src/Illuminate/Log/Events/MessageLogged.php 0000644 00000001252 15060132306 0016633 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; } } framework/src/Illuminate/Log/LICENSE.md 0000644 00000002063 15060132306 0013555 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. framework/src/Illuminate/Log/Context/ContextServiceProvider.php 0000644 00000001750 15060132306 0020770 0 ustar 00 <?php namespace Illuminate\Log\Context; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Queue; use Illuminate\Support\Facades\Context; use Illuminate\Support\ServiceProvider; class ContextServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->scoped(Repository::class); } /** * Boot the application services. * * @return void */ public function boot() { Queue::createPayloadUsing(function ($connection, $queue, $payload) { $context = Context::dehydrate(); return $context === null ? $payload : [ ...$payload, 'illuminate:log:context' => $context, ]; }); $this->app['events']->listen(function (JobProcessing $event) { Context::hydrate($event->job->payload()['illuminate:log:context'] ?? null); }); } } framework/src/Illuminate/Log/Context/Events/ContextHydrated.php 0000644 00000000624 15060132306 0020664 0 ustar 00 <?php namespace Illuminate\Log\Context\Events; class ContextHydrated { /** * The context instance. * * @var \Illuminate\Log\Context\Repository */ public $context; /** * Create a new event instance. * * @param \Illuminate\Log\Context\Repository $context */ public function __construct($context) { $this->context = $context; } } framework/src/Illuminate/Log/Context/Events/ContextDehydrating.php 0000644 00000000627 15060132306 0021365 0 ustar 00 <?php namespace Illuminate\Log\Context\Events; class ContextDehydrating { /** * The context instance. * * @var \Illuminate\Log\Context\Repository */ public $context; /** * Create a new event instance. * * @param \Illuminate\Log\Context\Repository $context */ public function __construct($context) { $this->context = $context; } } framework/src/Illuminate/Log/Context/Repository.php 0000644 00000025320 15060132306 0016466 0 ustar 00 <?php namespace Illuminate\Log\Context; use __PHP_Incomplete_Class; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Log\Context\Events\ContextDehydrating as Dehydrating; use Illuminate\Log\Context\Events\ContextHydrated as Hydrated; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use RuntimeException; use Throwable; class Repository { use Conditionable, Macroable, SerializesModels; /** * The event dispatcher instance. * * @var \Illuminate\Events\Dispatcher */ protected $events; /** * The contextual data. * * @var array<string, mixed> */ protected $data = []; /** * The hidden contextual data. * * @var array<string, mixed> */ protected $hidden = []; /** * The callback that should handle unserialize exceptions. * * @var callable|null */ protected static $handleUnserializeExceptionsUsing; /** * Create a new Context instance. */ public function __construct(Dispatcher $events) { $this->events = $events; } /** * Determine if the given key exists. * * @param string $key * @return bool */ public function has($key) { return array_key_exists($key, $this->data); } /** * Determine if the given key exists within the hidden context data. * * @param string $key * @return bool */ public function hasHidden($key) { return array_key_exists($key, $this->hidden); } /** * Retrieve all the context data. * * @return array<string, mixed> */ public function all() { return $this->data; } /** * Retrieve all the hidden context data. * * @return array<string, mixed> */ public function allHidden() { return $this->hidden; } /** * Retrieve the given key's value. * * @param string $key * @param mixed $default * @return mixed */ public function get($key, $default = null) { return $this->data[$key] ?? value($default); } /** * Retrieve the given key's hidden value. * * @param string $key * @param mixed $default * @return mixed */ public function getHidden($key, $default = null) { return $this->hidden[$key] ?? value($default); } /** * Retrieve the given key's value and then forget it. * * @param string $key * @param mixed $default * @return mixed */ public function pull($key, $default = null) { return tap($this->get($key, $default), function () use ($key) { $this->forget($key); }); } /** * Retrieve the given key's hidden value and then forget it. * * @param string $key * @param mixed $default * @return mixed */ public function pullHidden($key, $default = null) { return tap($this->getHidden($key, $default), function () use ($key) { $this->forgetHidden($key); }); } /** * Retrieve only the values of the given keys. * * @param array<int, string> $keys * @return array<string, mixed> */ public function only($keys) { return array_intersect_key($this->data, array_flip($keys)); } /** * Retrieve only the hidden values of the given keys. * * @param array<int, string> $keys * @return array<string, mixed> */ public function onlyHidden($keys) { return array_intersect_key($this->hidden, array_flip($keys)); } /** * Add a context value. * * @param string|array<string, mixed> $key * @param mixed $value * @return $this */ public function add($key, $value = null) { $this->data = array_merge( $this->data, is_array($key) ? $key : [$key => $value] ); return $this; } /** * Add a hidden context value. * * @param string|array<string, mixed> $key * @param mixed $value * @return $this */ public function addHidden($key, $value = null) { $this->hidden = array_merge( $this->hidden, is_array($key) ? $key : [$key => $value] ); return $this; } /** * Forget the given context key. * * @param string|array<int, string> $key * @return $this */ public function forget($key) { foreach ((array) $key as $k) { unset($this->data[$k]); } return $this; } /** * Forget the given hidden context key. * * @param string|array<int, string> $key * @return $this */ public function forgetHidden($key) { foreach ((array) $key as $k) { unset($this->hidden[$k]); } return $this; } /** * Add a context value if it does not exist yet. * * @param string $key * @param mixed $value * @return $this */ public function addIf($key, $value) { if (! $this->has($key)) { $this->add($key, $value); } return $this; } /** * Add a hidden context value if it does not exist yet. * * @param string $key * @param mixed $value * @return $this */ public function addHiddenIf($key, $value) { if (! $this->hasHidden($key)) { $this->addHidden($key, $value); } return $this; } /** * Push the given values onto the key's stack. * * @param string $key * @param mixed ...$values * @return $this * * @throws \RuntimeException */ public function push($key, ...$values) { if (! $this->isStackable($key)) { throw new RuntimeException("Unable to push value onto context stack for key [{$key}]."); } $this->data[$key] = [ ...$this->data[$key] ?? [], ...$values, ]; return $this; } /** * Push the given hidden values onto the key's stack. * * @param string $key * @param mixed ...$values * @return $this * * @throws \RuntimeException */ public function pushHidden($key, ...$values) { if (! $this->isHiddenStackable($key)) { throw new RuntimeException("Unable to push value onto hidden context stack for key [{$key}]."); } $this->hidden[$key] = [ ...$this->hidden[$key] ?? [], ...$values, ]; return $this; } /** * Determine if a given key can be used as a stack. * * @param string $key * @return bool */ protected function isStackable($key) { return ! $this->has($key) || (is_array($this->data[$key]) && array_is_list($this->data[$key])); } /** * Determine if a given key can be used as a hidden stack. * * @param string $key * @return bool */ protected function isHiddenStackable($key) { return ! $this->hasHidden($key) || (is_array($this->hidden[$key]) && array_is_list($this->hidden[$key])); } /** * Determine if the repository is empty. * * @return bool */ public function isEmpty() { return $this->all() === [] && $this->allHidden() === []; } /** * Execute the given callback when context is about to be dehydrated. * * @param callable $callback * @return $this */ public function dehydrating($callback) { $this->events->listen(fn (Dehydrating $event) => $callback($event->context)); return $this; } /** * Execute the given callback when context has been hydrated. * * @param callable $callback * @return $this */ public function hydrated($callback) { $this->events->listen(fn (Hydrated $event) => $callback($event->context)); return $this; } /** * Handle unserialize exceptions using the given callback. * * @param callable|null $callback * @return static */ public function handleUnserializeExceptionsUsing($callback) { static::$handleUnserializeExceptionsUsing = $callback; return $this; } /** * Flush all context data. * * @return $this */ public function flush() { $this->data = []; $this->hidden = []; return $this; } /** * Dehydrate the context data. * * @internal * * @return ?array */ public function dehydrate() { $instance = (new static($this->events)) ->add($this->all()) ->addHidden($this->allHidden()); $instance->events->dispatch(new Dehydrating($instance)); $serialize = fn ($value) => serialize($instance->getSerializedPropertyValue($value, withRelations: false)); return $instance->isEmpty() ? null : [ 'data' => array_map($serialize, $instance->all()), 'hidden' => array_map($serialize, $instance->allHidden()), ]; } /** * Hydrate the context instance. * * @internal * * @param ?array $context * @return $this * * @throws \RuntimeException */ public function hydrate($context) { $unserialize = function ($value, $key, $hidden) { try { return tap($this->getRestoredPropertyValue(unserialize($value)), function ($value) { if ($value instanceof __PHP_Incomplete_Class) { throw new RuntimeException('Value is incomplete class: '.json_encode($value)); } }); } catch (Throwable $e) { if (static::$handleUnserializeExceptionsUsing !== null) { return (static::$handleUnserializeExceptionsUsing)($e, $key, $value, $hidden); } if ($e instanceof ModelNotFoundException) { if (function_exists('report')) { report($e); } return null; } throw $e; } }; [$data, $hidden] = [ collect($context['data'] ?? [])->map(fn ($value, $key) => $unserialize($value, $key, false))->all(), collect($context['hidden'] ?? [])->map(fn ($value, $key) => $unserialize($value, $key, true))->all(), ]; $this->events->dispatch(new Hydrated( $this->flush()->add($data)->addHidden($hidden) )); return $this; } } framework/src/Illuminate/Log/Logger.php 0000755 00000021317 15060132306 0014107 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); } } framework/src/Illuminate/Log/LogServiceProvider.php 0000644 00000000505 15060132306 0016436 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)); } } framework/src/Illuminate/Log/composer.json 0000755 00000001514 15060132306 0014676 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.2", "illuminate/contracts": "^11.0", "illuminate/support": "^11.0", "monolog/monolog": "^3.0" }, "autoload": { "psr-4": { "Illuminate\\Log\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Log/LogManager.php 0000644 00000051547 15060132306 0014711 0 ustar 00 <?php namespace Illuminate\Log; use Closure; use Illuminate\Log\Context\Repository as ContextRepository; 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] = tap($this->tap($name, new Logger($logger, $this->app['events'])) ->withContext($this->sharedContext)) ->pushProcessor(function ($record) { if (! $this->app->bound(ContextRepository::class)) { return $record; } return $record->with(extra: [ ...$record->extra, ...$this->app[ContextRepository::class]->all(), ]); }); }); } 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 log context on all currently resolved channels. * * @return $this */ public function withoutContext() { foreach ($this->channels as $channel) { if (method_exists($channel, 'withoutContext')) { $channel->withoutContext(); } } return $this; } /** * 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|\Stringable $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|\Stringable $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|\Stringable $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|\Stringable $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|\Stringable $message * @param array $context * @return void */ public function warning($message, array $context = []): void { $this->driver()->warning($message, $context); } /** * Normal but significant events. * * @param string|\Stringable $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|\Stringable $message * @param array $context * @return void */ public function info($message, array $context = []): void { $this->driver()->info($message, $context); } /** * Detailed debug information. * * @param string|\Stringable $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|\Stringable $message * @param array $context * @return void */ public function log($level, $message, array $context = []): void { $this->driver()->log($level, $message, $context); } /** * 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->driver()->$method(...$parameters); } } framework/src/Illuminate/Log/ParsesLogConfiguration.php 0000644 00000003426 15060132306 0017315 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(); } } framework/src/Illuminate/Contracts/Translation/Loader.php 0000755 00000001460 15060132306 0017610 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(); } framework/src/Illuminate/Contracts/Translation/HasLocalePreference.php 0000644 00000000336 15060132306 0022232 0 ustar 00 <?php namespace Illuminate\Contracts\Translation; interface HasLocalePreference { /** * Get the preferred locale of the entity. * * @return string|null */ public function preferredLocale(); } framework/src/Illuminate/Contracts/Translation/Translator.php 0000644 00000001633 15060132306 0020532 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|float|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); } framework/src/Illuminate/Contracts/Container/ContextualBindingBuilder.php 0000644 00000001572 15060132306 0022757 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); } framework/src/Illuminate/Contracts/Container/BindingResolutionException.php 0000644 00000000326 15060132306 0023340 0 ustar 00 <?php namespace Illuminate\Contracts\Container; use Exception; use Psr\Container\ContainerExceptionInterface; class BindingResolutionException extends Exception implements ContainerExceptionInterface { // } framework/src/Illuminate/Contracts/Container/CircularDependencyException.php 0000644 00000000327 15060132306 0023446 0 ustar 00 <?php namespace Illuminate\Contracts\Container; use Exception; use Psr\Container\ContainerExceptionInterface; class CircularDependencyException extends Exception implements ContainerExceptionInterface { // } framework/src/Illuminate/Contracts/Container/Container.php 0000644 00000012667 15060132306 0017760 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); } framework/src/Illuminate/Contracts/Support/Jsonable.php 0000755 00000000361 15060132306 0017314 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); } framework/src/Illuminate/Contracts/Support/MessageProvider.php 0000755 00000000354 15060132306 0020660 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface MessageProvider { /** * Get the messages for the instance. * * @return \Illuminate\Contracts\Support\MessageBag */ public function getMessageBag(); } framework/src/Illuminate/Contracts/Support/DeferrableProvider.php 0000644 00000000317 15060132306 0021323 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface DeferrableProvider { /** * Get the services provided by the provider. * * @return array */ public function provides(); } framework/src/Illuminate/Contracts/Support/Responsable.php 0000644 00000000462 15060132306 0020033 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); } framework/src/Illuminate/Contracts/Support/DeferringDisplayableValue.php 0000644 00000000435 15060132306 0022632 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(); } framework/src/Illuminate/Contracts/Support/Arrayable.php 0000755 00000000401 15060132306 0017454 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(); } framework/src/Illuminate/Contracts/Support/CanBeEscapedWhenCastToString.php 0000644 00000000511 15060132306 0023135 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); } framework/src/Illuminate/Contracts/Support/Renderable.php 0000755 00000000305 15060132306 0017620 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface Renderable { /** * Get the evaluated contents of the object. * * @return string */ public function render(); } framework/src/Illuminate/Contracts/Support/ValidatedData.php 0000644 00000000256 15060132306 0020246 0 ustar 00 <?php namespace Illuminate\Contracts\Support; use ArrayAccess; use IteratorAggregate; interface ValidatedData extends Arrayable, ArrayAccess, IteratorAggregate { // } framework/src/Illuminate/Contracts/Support/Htmlable.php 0000644 00000000272 15060132306 0017305 0 ustar 00 <?php namespace Illuminate\Contracts\Support; interface Htmlable { /** * Get content as a string of HTML. * * @return string */ public function toHtml(); } framework/src/Illuminate/Contracts/Support/MessageBag.php 0000644 00000004272 15060132306 0017557 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(); } framework/src/Illuminate/Contracts/Config/Repository.php 0000644 00000002212 15060132306 0017461 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); } framework/src/Illuminate/Contracts/Broadcasting/ShouldBroadcastNow.php 0000644 00000000165 15060132306 0022247 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface ShouldBroadcastNow extends ShouldBroadcast { // } framework/src/Illuminate/Contracts/Broadcasting/Broadcaster.php 0000644 00000001473 15060132306 0020736 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 = []); } framework/src/Illuminate/Contracts/Broadcasting/HasBroadcastChannel.php 0000644 00000000663 15060132306 0022334 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(); } framework/src/Illuminate/Contracts/Broadcasting/ShouldBeUnique.php 0000644 00000000131 15060132306 0021367 0 ustar 00 <?php namespace Illuminate\Contracts\Broadcasting; interface ShouldBeUnique { // } framework/src/Illuminate/Contracts/Broadcasting/ShouldBroadcast.php 0000644 00000000447 15060132306 0021566 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(); } framework/src/Illuminate/Contracts/Broadcasting/Factory.php 0000644 00000000441 15060132306 0020106 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); } framework/src/Illuminate/Contracts/Bus/QueueingDispatcher.php 0000644 00000001263 15060132306 0020424 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); } framework/src/Illuminate/Contracts/Bus/Dispatcher.php 0000644 00000002715 15060132306 0016724 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); } framework/src/Illuminate/Contracts/Pagination/Paginator.php 0000644 00000004636 15060132306 0020126 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 = []); } framework/src/Illuminate/Contracts/Pagination/LengthAwarePaginator.php 0000644 00000001051 15060132306 0022234 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(); } framework/src/Illuminate/Contracts/Pagination/CursorPaginator.php 0000644 00000005024 15060132306 0021314 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 = []); } framework/src/Illuminate/Contracts/Events/ShouldDispatchAfterCommit.php 0000644 00000000136 15060132306 0022415 0 ustar 00 <?php namespace Illuminate\Contracts\Events; interface ShouldDispatchAfterCommit { // } framework/src/Illuminate/Contracts/Events/Dispatcher.php 0000644 00000003440 15060132306 0017433 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(); } framework/src/Illuminate/Contracts/Events/ShouldHandleEventsAfterCommit.php 0000644 00000000142 15060132306 0023233 0 ustar 00 <?php namespace Illuminate\Contracts\Events; interface ShouldHandleEventsAfterCommit { // } framework/src/Illuminate/Contracts/LICENSE.md 0000644 00000002063 15060132306 0014774 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. framework/src/Illuminate/Contracts/Http/Kernel.php 0000644 00000001560 15060132306 0016241 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(); } framework/src/Illuminate/Contracts/Database/ModelIdentifier.php 0000644 00000002656 15060132306 0020660 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; } } framework/src/Illuminate/Contracts/Database/Eloquent/SupportsPartialRelations.php 0000644 00000001415 15060132306 0024436 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(); } framework/src/Illuminate/Contracts/Database/Eloquent/Builder.php 0000644 00000000456 15060132306 0020773 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 { } framework/src/Illuminate/Contracts/Database/Eloquent/Castable.php 0000644 00000000604 15060132306 0021116 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); } framework/src/Illuminate/Contracts/Database/Eloquent/DeviatesCastableAttributes.php 0000644 00000001306 15060132306 0024652 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); } framework/src/Illuminate/Contracts/Database/Eloquent/CastsInboundAttributes.php 0000644 00000000736 15060132306 0024051 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); } framework/src/Illuminate/Contracts/Database/Eloquent/CastsAttributes.php 0000644 00000001602 15060132306 0022523 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); } framework/src/Illuminate/Contracts/Database/Eloquent/SerializesCastableAttributes.php 0000644 00000000761 15060132306 0025224 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); } framework/src/Illuminate/Contracts/Database/Query/ConditionExpression.php 0000644 00000000154 15060132306 0022717 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Query; interface ConditionExpression extends Expression { } framework/src/Illuminate/Contracts/Database/Query/Expression.php 0000644 00000000467 15060132306 0021057 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); } framework/src/Illuminate/Contracts/Database/Query/Builder.php 0000644 00000000321 15060132306 0020273 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 { } framework/src/Illuminate/Contracts/Database/Events/MigrationEvent.php 0000644 00000000134 15060132306 0022001 0 ustar 00 <?php namespace Illuminate\Contracts\Database\Events; interface MigrationEvent { // } framework/src/Illuminate/Contracts/Cache/Lock.php 0000644 00000001463 15060132306 0015777 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(); } framework/src/Illuminate/Contracts/Cache/Repository.php 0000644 00000005525 15060132306 0017271 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(); } framework/src/Illuminate/Contracts/Cache/LockTimeoutException.php 0000644 00000000166 15060132306 0021224 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; use Exception; class LockTimeoutException extends Exception { // } framework/src/Illuminate/Contracts/Cache/Store.php 0000644 00000003547 15060132306 0016210 0 ustar 00 <?php namespace Illuminate\Contracts\Cache; interface Store { /** * Retrieve an item from the cache by key. * * @param string $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(); } framework/src/Illuminate/Contracts/Cache/Factory.php 0000644 00000000407 15060132306 0016513 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); } framework/src/Illuminate/Contracts/Cache/LockProvider.php 0000644 00000001065 15060132306 0017510 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); } framework/src/Illuminate/Contracts/Notifications/Dispatcher.php 0000644 00000001242 15060132306 0020776 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 * @param array|null $channels * @return void */ public function sendNow($notifiables, $notification, ?array $channels = null); } framework/src/Illuminate/Contracts/Notifications/Factory.php 0000644 00000001402 15060132306 0020315 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); } framework/src/Illuminate/Contracts/Redis/Connector.php 0000644 00000001174 15060132306 0017103 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); } framework/src/Illuminate/Contracts/Redis/LimiterTimeoutException.php 0000644 00000000171 15060132306 0022000 0 ustar 00 <?php namespace Illuminate\Contracts\Redis; use Exception; class LimiterTimeoutException extends Exception { // } framework/src/Illuminate/Contracts/Redis/Connection.php 0000644 00000001416 15060132306 0017247 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 = []); } framework/src/Illuminate/Contracts/Redis/Factory.php 0000644 00000000412 15060132306 0016552 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); } framework/src/Illuminate/Contracts/View/Engine.php 0000755 00000000411 15060132306 0016216 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 = []); } framework/src/Illuminate/Contracts/View/View.php 0000644 00000001035 15060132306 0015723 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(); } framework/src/Illuminate/Contracts/View/Factory.php 0000644 00000003615 15060132306 0016426 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); } framework/src/Illuminate/Contracts/View/ViewCompilationException.php 0000644 00000000171 15060132306 0022001 0 ustar 00 <?php namespace Illuminate\Contracts\View; use Exception; class ViewCompilationException extends Exception { // } framework/src/Illuminate/Contracts/Auth/Authenticatable.php 0000644 00000002111 15060132306 0020071 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 name of the password attribute for the user. * * @return string */ public function getAuthPasswordName(); /** * 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(); } framework/src/Illuminate/Contracts/Auth/UserProvider.php 0000644 00000003261 15060132306 0017434 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); /** * Rehash the user's password if required and supported. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @param bool $force * @return void */ public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false); } framework/src/Illuminate/Contracts/Auth/Middleware/AuthenticatesRequests.php 0000644 00000000143 15060132306 0023411 0 ustar 00 <?php namespace Illuminate\Contracts\Auth\Middleware; interface AuthenticatesRequests { // } framework/src/Illuminate/Contracts/Auth/Access/Authorizable.php 0000644 00000000466 15060132306 0020641 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 = []); } framework/src/Illuminate/Contracts/Auth/Access/Gate.php 0000644 00000007260 15060132306 0017067 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 all of the given abilities should be granted for the current user. * * @param iterable|string $ability * @param array|mixed $arguments * @return bool */ public function allows($ability, $arguments = []); /** * Determine if any of the given abilities should be denied for the current user. * * @param iterable|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(); } framework/src/Illuminate/Contracts/Auth/Guard.php 0000644 00000002114 15060132306 0016041 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 $this */ public function setUser(Authenticatable $user); } framework/src/Illuminate/Contracts/Auth/MustVerifyEmail.php 0000644 00000001232 15060132306 0020064 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(); } framework/src/Illuminate/Contracts/Auth/PasswordBrokerFactory.php 0000644 00000000434 15060132306 0021301 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); } framework/src/Illuminate/Contracts/Auth/StatefulGuard.php 0000644 00000003002 15060132306 0017546 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|false */ 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|false */ 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(); } framework/src/Illuminate/Contracts/Auth/PasswordBroker.php 0000644 00000002411 15060132306 0017746 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); } framework/src/Illuminate/Contracts/Auth/CanResetPassword.php 0000644 00000000636 15060132306 0020235 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); } framework/src/Illuminate/Contracts/Auth/SupportsBasicAuth.php 0000644 00000001173 15060132306 0020426 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 = []); } framework/src/Illuminate/Contracts/Auth/Factory.php 0000644 00000000711 15060132306 0016407 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); } framework/src/Illuminate/Contracts/Cookie/QueueingFactory.php 0000644 00000001153 15060132306 0020423 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(); } framework/src/Illuminate/Contracts/Cookie/Factory.php 0000644 00000002633 15060132306 0016724 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); } framework/src/Illuminate/Contracts/Debug/ExceptionHandler.php 0000644 00000002113 15060132306 0020357 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); } framework/src/Illuminate/Contracts/Filesystem/Filesystem.php 0000644 00000011744 15060132306 0020357 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'; /** * Get the full path to the file that exists at the given relative path. * * @param string $path * @return string */ public function path($path); /** * 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 = []); /** * 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 = []); /** * 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 = []); /** * 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); } framework/src/Illuminate/Contracts/Filesystem/FileNotFoundException.php 0000644 00000000174 15060132306 0022441 0 ustar 00 <?php namespace Illuminate\Contracts\Filesystem; use Exception; class FileNotFoundException extends Exception { // } framework/src/Illuminate/Contracts/Filesystem/Cloud.php 0000644 00000000367 15060132306 0017300 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); } framework/src/Illuminate/Contracts/Filesystem/LockTimeoutException.php 0000644 00000000173 15060132306 0022343 0 ustar 00 <?php namespace Illuminate\Contracts\Filesystem; use Exception; class LockTimeoutException extends Exception { // } framework/src/Illuminate/Contracts/Filesystem/Factory.php 0000644 00000000415 15060132306 0017633 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); } framework/src/Illuminate/Contracts/Process/ProcessResult.php 0000644 00000003166 15060132306 0020341 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(); /** * Determine if the output contains the given string. * * @param string $output * @return bool */ public function seeInOutput(string $output); /** * Get the error output of the process. * * @return string */ public function errorOutput(); /** * Determine if the error output contains the given string. * * @param string $output * @return bool */ public function seeInErrorOutput(string $output); /** * 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); } framework/src/Illuminate/Contracts/Process/InvokedProcess.php 0000644 00000002305 15060132306 0020454 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); } framework/src/Illuminate/Contracts/Validation/ValidatorAwareRule.php 0000644 00000000474 15060132306 0021734 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); } framework/src/Illuminate/Contracts/Validation/Rule.php 0000644 00000000666 15060132306 0017111 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(); } framework/src/Illuminate/Contracts/Validation/InvokableRule.php 0000644 00000000673 15060132306 0020742 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); } framework/src/Illuminate/Contracts/Validation/Validator.php 0000644 00000002555 15060132306 0020126 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(); } framework/src/Illuminate/Contracts/Validation/DataAwareRule.php 0000644 00000000347 15060132306 0020657 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); } framework/src/Illuminate/Contracts/Validation/ValidatesWhenResolved.php 0000644 00000000324 15060132306 0022433 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; interface ValidatesWhenResolved { /** * Validate the given class instance. * * @return void */ public function validateResolved(); } framework/src/Illuminate/Contracts/Validation/ValidationRule.php 0000644 00000000630 15060132306 0021113 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; } framework/src/Illuminate/Contracts/Validation/Factory.php 0000644 00000002240 15060132306 0017577 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); } framework/src/Illuminate/Contracts/Validation/UncompromisedVerifier.php 0000644 00000000413 15060132306 0022510 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); } framework/src/Illuminate/Contracts/Validation/ImplicitRule.php 0000644 00000000214 15060132306 0020571 0 ustar 00 <?php namespace Illuminate\Contracts\Validation; /** * @deprecated see ValidationRule */ interface ImplicitRule extends Rule { // } framework/src/Illuminate/Contracts/Routing/BindingRegistrar.php 0000644 00000000677 15060132306 0020776 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); } framework/src/Illuminate/Contracts/Routing/UrlRoutable.php 0000644 00000001561 15060132306 0017772 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); } framework/src/Illuminate/Contracts/Routing/ResponseFactory.php 0000644 00000011202 15060132306 0020651 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); } framework/src/Illuminate/Contracts/Routing/UrlGenerator.php 0000644 00000005373 15060132306 0020150 0 ustar 00 <?php namespace Illuminate\Contracts\Routing; /** * @method string query(string $path, array $query = [], mixed $extra = [], bool|null $secure = null) */ 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); /** * 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); /** * 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); /** * 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); } framework/src/Illuminate/Contracts/Routing/Registrar.php 0000644 00000005203 15060132306 0017471 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); } framework/src/Illuminate/Contracts/Queue/Job.php 0000644 00000005775 15060132306 0015714 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(); } framework/src/Illuminate/Contracts/Queue/ShouldBeEncrypted.php 0000644 00000000125 15060132306 0020545 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldBeEncrypted { // } framework/src/Illuminate/Contracts/Queue/QueueableEntity.php 0000644 00000000741 15060132306 0020273 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(); } framework/src/Illuminate/Contracts/Queue/EntityResolver.php 0000644 00000000403 15060132306 0020157 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); } framework/src/Illuminate/Contracts/Queue/ShouldBeUniqueUntilProcessing.php 0000644 00000000170 15060132306 0023127 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldBeUniqueUntilProcessing extends ShouldBeUnique { // } framework/src/Illuminate/Contracts/Queue/Monitor.php 0000644 00000001240 15060132306 0016610 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); } framework/src/Illuminate/Contracts/Queue/EntityNotFoundException.php 0000644 00000000704 15060132306 0021775 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}]."); } } framework/src/Illuminate/Contracts/Queue/ShouldQueueAfterCommit.php 0000644 00000000156 15060132306 0021564 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldQueueAfterCommit extends ShouldQueue { // } framework/src/Illuminate/Contracts/Queue/QueueableCollection.php 0000644 00000001254 15060132306 0021112 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(); } framework/src/Illuminate/Contracts/Queue/ShouldBeUnique.php 0000644 00000000122 15060132306 0020053 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldBeUnique { // } framework/src/Illuminate/Contracts/Queue/ClearableQueue.php 0000644 00000000344 15060132306 0020044 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); } framework/src/Illuminate/Contracts/Queue/ShouldQueue.php 0000644 00000000117 15060132306 0017426 0 ustar 00 <?php namespace Illuminate\Contracts\Queue; interface ShouldQueue { // } framework/src/Illuminate/Contracts/Queue/Queue.php 0000644 00000004434 15060132306 0016255 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); } framework/src/Illuminate/Contracts/Queue/Factory.php 0000644 00000000410 15060132306 0016566 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); } framework/src/Illuminate/Contracts/composer.json 0000644 00000001503 15060132306 0016110 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.2", "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": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Contracts/Encryption/EncryptException.php 0000644 00000000205 15060132306 0021532 0 ustar 00 <?php namespace Illuminate\Contracts\Encryption; use RuntimeException; class EncryptException extends RuntimeException { // } framework/src/Illuminate/Contracts/Encryption/Encrypter.php 0000644 00000002015 15060132306 0020203 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(); /** * Get the current encryption key and all previous encryption keys. * * @return array */ public function getAllKeys(); /** * Get the previous encryption keys. * * @return array */ public function getPreviousKeys(); } framework/src/Illuminate/Contracts/Encryption/DecryptException.php 0000644 00000000205 15060132306 0021520 0 ustar 00 <?php namespace Illuminate\Contracts\Encryption; use RuntimeException; class DecryptException extends RuntimeException { // } framework/src/Illuminate/Contracts/Encryption/StringEncrypter.php 0000644 00000001072 15060132306 0021374 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); } framework/src/Illuminate/Contracts/Pipeline/Hub.php 0000644 00000000446 15060132306 0016367 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); } framework/src/Illuminate/Contracts/Pipeline/Pipeline.php 0000644 00000001367 15060132306 0017421 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); } framework/src/Illuminate/Contracts/Mail/Mailable.php 0000644 00000003376 15060132306 0016501 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); } framework/src/Illuminate/Contracts/Mail/Attachable.php 0000644 00000000343 15060132306 0017012 0 ustar 00 <?php namespace Illuminate\Contracts\Mail; interface Attachable { /** * Get an attachment instance for this entity. * * @return \Illuminate\Mail\Attachment */ public function toMailAttachment(); } framework/src/Illuminate/Contracts/Mail/MailQueue.php 0000644 00000001210 15060132306 0016643 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); } framework/src/Illuminate/Contracts/Mail/Mailer.php 0000644 00000002521 15060132306 0016173 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); /** * Send a new message synchronously using a view. * * @param \Illuminate\Contracts\Mail\Mailable|string|array $mailable * @param array $data * @param \Closure|string|null $callback * @return \Illuminate\Mail\SentMessage|null */ public function sendNow($mailable, array $data = [], $callback = null); } framework/src/Illuminate/Contracts/Mail/Factory.php 0000644 00000000375 15060132306 0016376 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); } framework/src/Illuminate/Contracts/Console/Isolatable.php 0000644 00000000120 15060132306 0017552 0 ustar 00 <?php namespace Illuminate\Contracts\Console; interface Isolatable { // } framework/src/Illuminate/Contracts/Console/Kernel.php 0000644 00000003016 15060132306 0016722 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); } framework/src/Illuminate/Contracts/Console/PromptsForMissingInput.php 0000644 00000000134 15060132306 0022165 0 ustar 00 <?php namespace Illuminate\Contracts\Console; interface PromptsForMissingInput { // } framework/src/Illuminate/Contracts/Console/Application.php 0000644 00000001005 15060132306 0017741 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(); } framework/src/Illuminate/Contracts/Foundation/ExceptionRenderer.php 0000644 00000000372 15060132306 0021635 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); } framework/src/Illuminate/Contracts/Foundation/MaintenanceMode.php 0000644 00000001313 15060132306 0021233 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; } framework/src/Illuminate/Contracts/Foundation/CachesConfiguration.php 0000644 00000001010 15060132306 0022114 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(); } framework/src/Illuminate/Contracts/Foundation/Application.php 0000644 00000012746 15060132306 0020463 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(); } framework/src/Illuminate/Contracts/Foundation/CachesRoutes.php 0000644 00000000536 15060132306 0020602 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(); } framework/src/Illuminate/Contracts/Hashing/Hasher.php 0000644 00000001662 15060132306 0016700 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 = []); } framework/src/Illuminate/Contracts/Session/Middleware/AuthenticatesSessions.php 0000644 00000000146 15060132306 0024131 0 ustar 00 <?php namespace Illuminate\Contracts\Session\Middleware; interface AuthenticatesSessions { // } framework/src/Illuminate/Contracts/Session/Session.php 0000644 00000007467 15060132306 0017164 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); } framework/src/Illuminate/Testing/TestComponent.php 0000644 00000007655 15060132306 0016374 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Testing\Assert as PHPUnit; use Illuminate\Testing\Constraints\SeeInOrder; use Stringable; class TestComponent implements Stringable { /** * 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); } } framework/src/Illuminate/Testing/ParallelTesting.php 0000644 00000015400 15060132306 0016647 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(); } } framework/src/Illuminate/Testing/TestResponse.php 0000644 00000140026 15060132306 0016216 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\Conditionable; use Illuminate\Support\Traits\Dumpable; 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\StreamedJsonResponse; use Symfony\Component\HttpFoundation\StreamedResponse; /** * @mixin \Illuminate\Http\Response */ class TestResponse implements ArrayAccess { use Concerns\AssertsStatusCodes, Conditionable, Dumpable, Tappable, Macroable { __call as macroCall; } /** * The original request. * * @var \Illuminate\Http\Request|null */ public $baseRequest; /** * 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 * @param \Illuminate\Http\Request|null $request * @return void */ public function __construct($response, $request = null) { $this->baseResponse = $response; $this->baseRequest = $request; $this->exceptions = new Collection; } /** * Create a new TestResponse from another response. * * @param \Illuminate\Http\Response $response * @param \Illuminate\Http\Request|null $request * @return static */ public static function fromBaseResponse($response, $request = null) { return new static($response, $request); } /** * 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($status, $actual, $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(), date_default_timezone_get()); 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(), date_default_timezone_get()); 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(), $cookie->isPartitioned() ); } } } /** * 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 array matches the streamed JSON response content. * * @param array $value * @return $this */ public function assertStreamedJsonContent($value) { return $this->assertStreamedContent(json_encode($value, JSON_THROW_ON_ERROR)); } /** * 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( $resolvedKey = (is_int($key)) ? $value : $key, $sessionErrors, "Failed to find a validation error in session for key: '{$resolvedKey}'".PHP_EOL.PHP_EOL.$errorMessage ); foreach (Arr::wrap($value) as $message) { if (! is_int($key)) { $hasError = false; foreach (Arr::wrap($sessionErrors[$key]) as $sessionErrorMessage) { if (Str::contains($sessionErrorMessage, $message)) { $hasError = true; break; } } if (! $hasError) { PHPUnit::fail( "Failed to find a validation error for key and message: '$key' => '$message'".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 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 && ! $this->baseResponse instanceof StreamedJsonResponse) { 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 = is_string($exceptionToAppend) ? $exceptionToAppend : $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 | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : 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); } } framework/src/Illuminate/Testing/Concerns/RunsInParallel.php 0000644 00000011755 15060132306 0020233 0 ustar 00 <?php namespace Illuminate\Testing\Concerns; use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Application; use Illuminate\Support\Facades\ParallelTesting; use Illuminate\Testing\ParallelConsoleOutput; use PHPUnit\TextUI\Configuration\PhpHandler; 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 { $configuration = $this->options instanceof \ParaTest\Options ? $this->options->configuration : $this->options->configuration(); (new PhpHandler())->handle($configuration->php()); $this->forEachProcess(function () { ParallelTesting::callSetUpProcessCallbacks(); }); try { $potentialExitCode = $this->runner->run(); } finally { $this->forEachProcess(function () { ParallelTesting::callTearDownProcessCallbacks(); }); } return $potentialExitCode ?? $this->getExitCode(); } /** * 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 = (Application::inferBasePath().'/bootstrap/app.php'))) { $app = require $path; $app->make(Kernel::class)->bootstrap(); return $app; } throw new RuntimeException('Parallel Runner unable to resolve application.'); }; return $applicationResolver(); } } framework/src/Illuminate/Testing/Concerns/AssertsStatusCodes.php 0000644 00000012275 15060132306 0021144 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); } } framework/src/Illuminate/Testing/Concerns/TestDatabases.php 0000644 00000012237 15060132306 0020063 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}"; } } framework/src/Illuminate/Testing/Fluent/Concerns/Matching.php 0000644 00000013527 15060132306 0020326 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); } framework/src/Illuminate/Testing/Fluent/Concerns/Debugging.php 0000644 00000001073 15060132306 0020460 0 ustar 00 <?php namespace Illuminate\Testing\Fluent\Concerns; use Illuminate\Support\Traits\Dumpable; trait Debugging { use Dumpable; /** * Dumps the given props. * * @param string|null $prop * @return $this */ public function dump(?string $prop = null): self { dump($this->prop($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); } framework/src/Illuminate/Testing/Fluent/Concerns/Has.php 0000644 00000012012 15060132306 0017273 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); } framework/src/Illuminate/Testing/Fluent/Concerns/Interaction.php 0000644 00000002702 15060132306 0021044 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); } framework/src/Illuminate/Testing/Fluent/AssertableJson.php 0000644 00000010261 15060132306 0017731 0 ustar 00 <?php namespace Illuminate\Testing\Fluent; use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Conditionable; 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, Conditionable, 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; } } framework/src/Illuminate/Testing/LICENSE.md 0000644 00000002063 15060132306 0014451 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. framework/src/Illuminate/Testing/TestView.php 0000644 00000013037 15060132306 0015333 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; use Stringable; class TestView implements Stringable { 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 view's rendered content is empty. * * @return $this */ public function assertViewEmpty() { PHPUnit::assertEmpty($this->rendered); 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; } } framework/src/Illuminate/Testing/Constraints/CountInDatabase.php 0000644 00000003561 15060132306 0021075 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; } } framework/src/Illuminate/Testing/Constraints/NotSoftDeletedInDatabase.php 0000644 00000005261 15060132306 0022667 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); } } framework/src/Illuminate/Testing/Constraints/HasInDatabase.php 0000644 00000006240 15060132306 0020515 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); } } framework/src/Illuminate/Testing/Constraints/SeeInOrder.php 0000644 00000003401 15060132306 0020061 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; } } framework/src/Illuminate/Testing/Constraints/ArraySubset.php 0000644 00000007153 15060132306 0020336 0 ustar 00 <?php namespace Illuminate\Testing\Constraints; use ArrayObject; use PHPUnit\Framework\Constraint\Constraint; use SebastianBergmann\Comparator\ComparisonFailure; use SebastianBergmann\Exporter\Exporter; use Traversable; class ArraySubset extends Constraint { /** * @var iterable */ protected $subset; /** * @var bool */ protected $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 '.(new 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 */ protected 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); } return (array) $other; } } framework/src/Illuminate/Testing/Constraints/SoftDeletedInDatabase.php 0000644 00000005267 15060132306 0022214 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); } } framework/src/Illuminate/Testing/ParallelConsoleOutput.php 0000644 00000002573 15060132306 0020064 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): void { $messages = collect($messages)->filter(function ($message) { return ! Str::contains($message, $this->ignore); }); $this->output->write($messages->toArray(), $newline, $options); } } framework/src/Illuminate/Testing/AssertableJsonString.php 0000644 00000025513 15060132306 0017671 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]); } } framework/src/Illuminate/Testing/LoggedExceptionCollection.php 0000644 00000000210 15060132306 0020642 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Support\Collection; class LoggedExceptionCollection extends Collection { // } framework/src/Illuminate/Testing/composer.json 0000644 00000002572 15060132306 0015574 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.2", "ext-mbstring": "*", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/macroable": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Testing\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "brianium/paratest": "Required to run tests in parallel (^7.0|^8.0).", "illuminate/console": "Required to assert console commands (^11.0).", "illuminate/database": "Required to assert databases (^11.0).", "illuminate/http": "Required to assert responses (^11.0).", "mockery/mockery": "Required to use mocking (^1.6).", "phpunit/phpunit": "Required to use assertions and run tests (^10.5|^11.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Testing/Assert.php 0000644 00000002222 15060132306 0015014 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); } } framework/src/Illuminate/Testing/ParallelRunner.php 0000644 00000001341 15060132306 0016502 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(); } } } framework/src/Illuminate/Testing/PendingCommand.php 0000644 00000033450 15060132306 0016445 0 ustar 00 <?php namespace Illuminate\Testing; use Illuminate\Console\OutputStyle; use Illuminate\Console\PromptValidationException; use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; 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 { use Conditionable; use Macroable; /** * 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|null $output * @return $this */ public function expectsOutput($output = null) { if ($output === null) { $this->test->expectsOutput = true; return $this; } $this->test->expectedOutput[] = $output; return $this; } /** * Specify output that should never be printed when the command runs. * * @param string|null $output * @return $this */ public function doesntExpectOutput($output = null) { if ($output === null) { $this->test->expectsOutput = false; return $this; } $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; } catch (PromptValidationException) { $exitCode = Command::FAILURE; } 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(); if ($this->test->expectsOutput === false) { $mock->shouldReceive('doWrite')->never(); return $mock; } if ($this->test->expectsOutput === true && count($this->test->expectedOutput) === 0 && count($this->test->expectedOutputSubstrings) === 0) { $mock->shouldReceive('doWrite')->atLeast()->once(); } 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(); } } framework/src/Illuminate/Testing/ParallelTestingServiceProvider.php 0000644 00000001521 15060132306 0021702 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); }); } } } framework/src/Illuminate/Testing/Exceptions/InvalidArgumentException.php 0000644 00000001574 15060132306 0022655 0 ustar 00 <?php namespace Illuminate\Testing\Exceptions; use PHPUnit\Framework\Exception; class InvalidArgumentException extends Exception { /** * Creates a new exception for an invalid argument. * * @param int $argument * @param string $type * @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 ) ); } } framework/src/Illuminate/Hashing/LICENSE.md 0000644 00000002063 15060132306 0014415 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. framework/src/Illuminate/Hashing/Argon2IdHasher.php 0000644 00000002306 15060132306 0016262 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; } } framework/src/Illuminate/Hashing/BcryptHasher.php 0000755 00000007106 15060132306 0016126 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; } } framework/src/Illuminate/Hashing/ArgonHasher.php 0000644 00000013134 15060132306 0015724 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; } } framework/src/Illuminate/Hashing/composer.json 0000755 00000001465 15060132306 0015543 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.2", "illuminate/contracts": "^11.0", "illuminate/support": "^11.0" }, "autoload": { "psr-4": { "Illuminate\\Hashing\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Hashing/AbstractHasher.php 0000644 00000001344 15060132306 0016421 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); } } framework/src/Illuminate/Hashing/HashServiceProvider.php 0000755 00000001353 15060132306 0017445 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']; } } framework/src/Illuminate/Hashing/HashManager.php 0000644 00000005045 15060132306 0015703 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 $this->driver()->info($value)['algo'] !== null; } /** * Get the default driver name. * * @return string */ public function getDefaultDriver() { return $this->config->get('hashing.driver', 'bcrypt'); } } framework/src/Illuminate/Session/Middleware/AuthenticateSession.php 0000644 00000007640 15060132306 0021631 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; /** * The callback that should be used to generate the authentication redirect path. * * @var callable */ protected static $redirectToCallback; /** * 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() || ! $request->user()->getAuthPassword()) { return $next($request); } if ($this->guard()->viaRemember()) { $passwordHash = explode('|', $request->cookies->get($this->guard()->getRecallerName()))[2] ?? null; if (! $passwordHash || ! hash_equals($request->user()->getAuthPassword(), $passwordHash)) { $this->logout($request); } } if (! $request->session()->has('password_hash_'.$this->auth->getDefaultDriver())) { $this->storePasswordHashInSession($request); } if (! hash_equals($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) { if (static::$redirectToCallback) { return call_user_func(static::$redirectToCallback, $request); } } /** * Specify the callback that should be used to generate the redirect path. * * @param callable $redirectToCallback * @return void */ public static function redirectUsing(callable $redirectToCallback) { static::$redirectToCallback = $redirectToCallback; } } framework/src/Illuminate/Session/Middleware/StartSession.php 0000644 00000021752 15060132306 0020310 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, $config['partitioned'] ?? false )); } } /** * 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); } } framework/src/Illuminate/Session/NullSessionHandler.php 0000644 00000001732 15060132306 0017342 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; } } framework/src/Illuminate/Session/SymfonySessionDecorator.php 0000644 00000006724 15060132306 0020447 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\Contracts\Session\Session */ public readonly Session $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} */ public function setId(string $id): void { $this->store->setId($id); } /** * {@inheritdoc} */ public function getName(): string { return $this->store->getName(); } /** * {@inheritdoc} */ public function setName(string $name): void { $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} */ public function save(): void { $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} */ public function set(string $name, mixed $value): void { $this->store->put($name, $value); } /** * {@inheritdoc} */ public function all(): array { return $this->store->all(); } /** * {@inheritdoc} */ public function replace(array $attributes): void { $this->store->replace($attributes); } /** * {@inheritdoc} */ public function remove(string $name): mixed { return $this->store->remove($name); } /** * {@inheritdoc} */ public function clear(): void { $this->store->flush(); } /** * {@inheritdoc} */ public function isStarted(): bool { return $this->store->isStarted(); } /** * {@inheritdoc} */ public function registerBag(SessionBagInterface $bag): void { 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.'); } } framework/src/Illuminate/Session/CookieSessionHandler.php 0000755 00000005611 15060132306 0017644 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; } } framework/src/Illuminate/Session/LICENSE.md 0000644 00000002063 15060132306 0014457 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. framework/src/Illuminate/Session/CacheBasedSessionHandler.php 0000755 00000003632 15060132306 0020376 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; } } framework/src/Illuminate/Session/ArraySessionHandler.php 0000644 00000005107 15060132306 0017506 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; } } framework/src/Illuminate/Session/DatabaseSessionHandler.php 0000644 00000015734 15060132306 0020143 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; } } framework/src/Illuminate/Session/TokenMismatchException.php 0000755 00000000160 15060132306 0020210 0 ustar 00 <?php namespace Illuminate\Session; use Exception; class TokenMismatchException extends Exception { // } framework/src/Illuminate/Session/SessionManager.php 0000755 00000016146 15060132306 0016514 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); } } framework/src/Illuminate/Session/composer.json 0000755 00000002170 15060132306 0015577 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.2", "ext-ctype": "*", "ext-session": "*", "illuminate/collections": "^11.0", "illuminate/contracts": "^11.0", "illuminate/filesystem": "^11.0", "illuminate/support": "^11.0", "symfony/finder": "^7.0", "symfony/http-foundation": "^7.0" }, "autoload": { "psr-4": { "Illuminate\\Session\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "suggest": { "illuminate/console": "Required to use the session:table command (^11.0)." }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/src/Illuminate/Session/ExistenceAwareInterface.php 0000644 00000000376 15060132306 0020321 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); } framework/src/Illuminate/Session/EncryptedStore.php 0000644 00000003354 15060132306 0016542 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; } } framework/src/Illuminate/Session/FileSessionHandler.php 0000644 00000005060 15060132306 0017305 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; } } framework/src/Illuminate/Session/Console/stubs/database.stub 0000755 00000001423 15060132306 0020262 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'); } }; framework/src/Illuminate/Session/Console/SessionTableCommand.php 0000644 00000003240 15060132306 0021056 0 ustar 00 <?php namespace Illuminate\Session\Console; use Illuminate\Console\MigrationGeneratorCommand; use Symfony\Component\Console\Attribute\AsCommand; use function Illuminate\Filesystem\join_paths; #[AsCommand(name: 'make:session-table', aliases: ['session:table'])] class SessionTableCommand extends MigrationGeneratorCommand { /** * The console command name. * * @var string */ protected $name = 'make:session-table'; /** * The console command name aliases. * * @var array */ protected $aliases = ['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'; } /** * Determine whether a migration for the table already exists. * * @param string $table * @return bool */ protected function migrationExists($table) { foreach ([ join_paths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php'), join_paths($this->laravel->databasePath('migrations'), '0001_01_01_000000_create_users_table.php'), ] as $path) { if (count($this->files->glob($path)) !== 0) { return true; } } return false; } } framework/src/Illuminate/Session/SessionServiceProvider.php 0000755 00000002721 15060132306 0020247 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(); }); } } framework/src/Illuminate/Session/Store.php 0000755 00000041232 15060132306 0014664 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_replace($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); } /** * Get all the session data except for a specified array of items. * * @param array $keys * @return array */ public function except(array $keys) { return Arr::except($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); } /** * Determine 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)); }); } /** * Determine if any of the given keys are present and not null. * * @param string|array $key * @return bool */ public function hasAny($key) { return collect(is_array($key) ? $key : func_get_args())->filter(function ($key) { return ! is_null($this->get($key)); })->count() >= 1; } /** * 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); } } } framework/src/Illuminate/Macroable/LICENSE.md 0000644 00000002063 15060132306 0014721 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. framework/src/Illuminate/Macroable/Traits/Macroable.php 0000644 00000005455 15060132306 0017171 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 * * @param-closure-this static $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); } } framework/src/Illuminate/Macroable/composer.json 0000644 00000001351 15060132306 0016036 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.2" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" } }, "extra": { "branch-alias": { "dev-master": "11.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" } framework/LICENSE.md 0000644 00000002063 15060132306 0010142 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. framework/CHANGELOG.md 0000644 00000124521 15060132306 0010353 0 ustar 00 # Release Notes for 11.x ## [Unreleased](https://github.com/laravel/framework/compare/v11.9.2...11.x) ## [v11.9.2](https://github.com/laravel/framework/compare/v11.9.1...v11.9.2) - 2024-05-30 * [11.x] Fix new exception renderer compatibility with closure middleware by [@ifox](https://github.com/ifox) in https://github.com/laravel/framework/pull/51614 * [11.x] Fix double-quoted string literals on SQLite by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/framework/pull/51615 * [11.x] Allow setting Resend api key in mailer specific config by [@riasvdv](https://github.com/riasvdv) in https://github.com/laravel/framework/pull/51618 * [11.x] Fix only number as session key will result in numbered session keys by [@Katalam](https://github.com/Katalam) in https://github.com/laravel/framework/pull/51611 ## [v11.9.1](https://github.com/laravel/framework/compare/v11.9.0...v11.9.1) - 2024-05-28 * [11.x] Fixes missing route context by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/51602 ## [v11.9.0](https://github.com/laravel/framework/compare/v11.8.0...v11.9.0) - 2024-05-28 * [11.x] Optimize boostrap time by using hashtable to store providers by [@sarven](https://github.com/sarven) in https://github.com/laravel/framework/pull/51343 * [11.x] Prevent destructive commands from running by [@jasonmccreary](https://github.com/jasonmccreary) in https://github.com/laravel/framework/pull/51376 * [11.x] renamed left `has` to `contains` by [@MrPunyapal](https://github.com/MrPunyapal) in https://github.com/laravel/framework/pull/51532 * [10.x] Fix typo by [@Issei0804-ie](https://github.com/Issei0804-ie) in https://github.com/laravel/framework/pull/51535 * [11.x] Fixes doc block in Timebox.php by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/51537 * [11.x] Rename test function to match prohibit action by [@faissaloux](https://github.com/faissaloux) in https://github.com/laravel/framework/pull/51534 * [11.x] Fix LazilyRefreshDatabase when using Laravel BrowserKit Testing by [@MaxGiting](https://github.com/MaxGiting) in https://github.com/laravel/framework/pull/51538 * [10.x] Fix SQL Server detection in database store by [@staudenmeir](https://github.com/staudenmeir) in https://github.com/laravel/framework/pull/51547 * [11.x] Display test creation messages by [@nshiro](https://github.com/nshiro) in https://github.com/laravel/framework/pull/51546 * [11.x] Detect Cockroach DB connection loss by [@saschaglo](https://github.com/saschaglo) in https://github.com/laravel/framework/pull/51559 * [11.x] Fix type tests by [@stayallive](https://github.com/stayallive) in https://github.com/laravel/framework/pull/51558 * [11.x] Add `withoutDelay()` to the `Queueable` trait by [@KennedyTedesco](https://github.com/KennedyTedesco) in https://github.com/laravel/framework/pull/51555 * [11.x] Add an option to remove the original environment file after encrypting by [@riasvdv](https://github.com/riasvdv) in https://github.com/laravel/framework/pull/51556 * [10.x] - Fix batch list loading in Horizon when serialization error by [@jeffortegad](https://github.com/jeffortegad) in https://github.com/laravel/framework/pull/51551 * [10.x] Fixes explicit route binding with `BackedEnum` by [@CAAHS](https://github.com/CAAHS) in https://github.com/laravel/framework/pull/51586 * [11.x] Add `Macroable` to `PendingCommand` by [@PerryvanderMeer](https://github.com/PerryvanderMeer) in https://github.com/laravel/framework/pull/51572 * [11.x] Improves errors by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/51261 * [11.x] Add RELEASE.md to .gitattributes by [@Jubeki](https://github.com/Jubeki) in https://github.com/laravel/framework/pull/51598 * [11.x] Fixes exception rendering by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/51587 ## [v11.8.0](https://github.com/laravel/framework/compare/v11.7.0...v11.8.0) - 2024-05-21 * [11.x] Update PendingRequest.php by [@foremtehan](https://github.com/foremtehan) in https://github.com/laravel/framework/pull/51338 * Add unshift method to Collection by [@timkelty](https://github.com/timkelty) in https://github.com/laravel/framework/pull/51344 * [11.x] Synchronizing cache configuration file with updated laravel v11.0.7 by [@dvlpr91](https://github.com/dvlpr91) in https://github.com/laravel/framework/pull/51336 * [11.x] Utilize `null-safe` operator instead of conditional check by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/51328 * [11.x] Add the events to be displayed on the model:show command by [@WendellAdriel](https://github.com/WendellAdriel) in https://github.com/laravel/framework/pull/51324 * [11.x] fix: remove use of Redis::COMPRESSION_ZSTD_MIN by [@calebdw](https://github.com/calebdw) in https://github.com/laravel/framework/pull/51346 * [10.x] Backport: Fix SesV2Transport to use correct `EmailTags` argument by [@Tietew](https://github.com/Tietew) in https://github.com/laravel/framework/pull/51352 * [11.x] feat: use phpredis 6 in ci by [@calebdw](https://github.com/calebdw) in https://github.com/laravel/framework/pull/51347 * [11.x] create new "has" validation rule by [@browner12](https://github.com/browner12) in https://github.com/laravel/framework/pull/51348 * [11.x] Add support for previous apps keys in signed URL verification by [@Krisell](https://github.com/Krisell) in https://github.com/laravel/framework/pull/51222 * [11.x] Allow setting exit code in migrate:status --pending by [@brecht-vermeersch](https://github.com/brecht-vermeersch) in https://github.com/laravel/framework/pull/51341 * [11.x] Fix array rule typehint by [@erik-perri](https://github.com/erik-perri) in https://github.com/laravel/framework/pull/51372 * [11.x] Test Improvements by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/51365 * [10.x] Fix PHPDoc typo by [@staudenmeir](https://github.com/staudenmeir) in https://github.com/laravel/framework/pull/51390 * [11.x] Fix return type hint of resolveRouteBindingQuery by [@philbates35](https://github.com/philbates35) in https://github.com/laravel/framework/pull/51392 * [11.x] Allow adding array or string for web and api routes in bootstrap/app.php by [@mrthito](https://github.com/mrthito) in https://github.com/laravel/framework/pull/51356 * [ 11.x ] Adds ability to manually fail a command from outside the handle() method by [@ProjektGopher](https://github.com/ProjektGopher) in https://github.com/laravel/framework/pull/51435 * [10.x] Fix `apa` on non ASCII characters by [@faissaloux](https://github.com/faissaloux) in https://github.com/laravel/framework/pull/51428 * [11.x] Compare lowercased column names in getColumnType by [@chady](https://github.com/chady) in https://github.com/laravel/framework/pull/51431 * [11.x] Use contracts instead of concrete type for `resolveRouteBindingQuery()` by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/51425 * [11.x] Set the value of `$this` in macro closures by [@simonwelsh](https://github.com/simonwelsh) in https://github.com/laravel/framework/pull/51401 * [11.x] Add missing roundrobin transport driver config by [@u01jmg3](https://github.com/u01jmg3) in https://github.com/laravel/framework/pull/51400 * [11.x] Remove unused namespace by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/51436 * [11.x] Fixes doc block in `Connector.php` by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/51440 * [10.x] Fixes view engine resolvers leaking memory by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/51450 * [11.x] Add some tests to `SupportStrTest` by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/51437 * [11.x] Add isCurrentlyOwnedBy function to lock by [@gazben](https://github.com/gazben) in https://github.com/laravel/framework/pull/51393 * [11.x] Collection average/avg optimization by [@bert-w](https://github.com/bert-w) in https://github.com/laravel/framework/pull/51512 * [11.x] Introduce `MixManifestNotFoundException` for handling missing Mix manifests by [@xurshudyan](https://github.com/xurshudyan) in https://github.com/laravel/framework/pull/51502 * [11.x] MailMakeCommand: Add new `--view` option by [@ryangjchandler](https://github.com/ryangjchandler) in https://github.com/laravel/framework/pull/51411 * [11.x] Replace all backed enums with values when building URLs by [@stefanvdlugt](https://github.com/stefanvdlugt) in https://github.com/laravel/framework/pull/51524 * [10.x] Do not use `app()` Foundation helper on `ViewServiceProvider` by [@rodrigopedra](https://github.com/rodrigopedra) in https://github.com/laravel/framework/pull/51522 * Fixes explicit route binding with `BackedEnum` by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/51525 * [11.x] Add query method to UrlGenerator contract docblock by [@hjanos-bc](https://github.com/hjanos-bc) in https://github.com/laravel/framework/pull/51515 ## [v11.7.0](https://github.com/laravel/framework/compare/v11.6.0...v11.7.0) - 2024-05-07 * [11.x] Fix SesV2Transport to use correct `EmailTags` argument by @Tietew in https://github.com/laravel/framework/pull/51265 * [11.x] Add Databases nightly workflow by @Jubeki in https://github.com/laravel/framework/pull/51218 * [11.x] update "min" and "max" rule comments by @browner12 in https://github.com/laravel/framework/pull/51274 * [11.x] Fix namespace and improvement PSR in `ClassMakeCommandTest.php` by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51280 * [11.x] improvement test coverage for view components. by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51271 * [11.x] Introduce method `Rule::array()` by @Jacobs63 in https://github.com/laravel/framework/pull/51250 * [11.x] Fix docblock for collection pluck methods by @SanderMuller in https://github.com/laravel/framework/pull/51295 * [11.x] Add tests for handling non-baked enum and empty string requests by @hrant1020 in https://github.com/laravel/framework/pull/51289 * blank and filled now support stringable by @lava83 in https://github.com/laravel/framework/pull/51300 * [11.x] Fix ratio validation for high ratio images by @ahmedbally in https://github.com/laravel/framework/pull/51296 * [11.x] Add int|float support to e method by @trippo in https://github.com/laravel/framework/pull/51314 * [11.x] Add release notes by @driesvints in https://github.com/laravel/framework/pull/51310 * [11.x] `Stringable` is also an interface of symfony by @lava83 in https://github.com/laravel/framework/pull/51309 * [11.x] Add some tests and improvement test coverage for `Str::camel` by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51308 * [11.x] Using the `??` Operator (Null Coalescing Operator) by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51305 * [11.x] Add ability to override the default loading cached Routes for application by @ahmedabdel3al in https://github.com/laravel/framework/pull/51292 * [11.x] Add ->whereJsonOverlaps() for mysql by @parkourben99 in https://github.com/laravel/framework/pull/51288 * [11.x] Add `InteractsWithInput` methods to `ValidatedInput` by @aydinfatih in https://github.com/laravel/framework/pull/51316 * [11.x] Adding PasswordResetLinkSent event by @Muffinman in https://github.com/laravel/framework/pull/51253 ## [v11.6.0](https://github.com/laravel/framework/compare/v11.5.0...v11.6.0) - 2024-04-30 * [11.x] github: mariadb database healthcheck+naming by @grooverdan in https://github.com/laravel/framework/pull/51192 * Add support for PHPUnit 11.1 by @crynobone in https://github.com/laravel/framework/pull/51197 * Move whitespace in front of verbatim block in Blade templates by @Sjord in https://github.com/laravel/framework/pull/51195 * [11.x] Trim trailing `?` from generated URL without query params by @onlime in https://github.com/laravel/framework/pull/51191 * Add some tests on route:list sort command by @fgaroby in https://github.com/laravel/framework/pull/51202 * [10.x] Improve releases flow by @driesvints in https://github.com/laravel/framework/pull/51213 * Fix return types of `firstWhere` and `first` of `BelongsToMany` and `HasManyThrough` by @SanderMuller in https://github.com/laravel/framework/pull/51219 * [10.x] Fix typo in signed URL tampering tests by @Krisell in https://github.com/laravel/framework/pull/51238 * [10.x] Add "Server has gone away" to DetectsLostConnection by @Jubeki in https://github.com/laravel/framework/pull/51241 * [11.x] Add some tests in `SupportStrTest` class by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51235 * [10.x] Fix support for the LARAVEL_STORAGE_PATH env var (#51238) by @dunglas in https://github.com/laravel/framework/pull/51243 * [11.x] Add replaceable tags to translations by @LegendEffects in https://github.com/laravel/framework/pull/51190 * [10.x] fix: Factory::createMany creating n^2 records by @calebdw in https://github.com/laravel/framework/pull/51225 ## [v11.5.0](https://github.com/laravel/framework/compare/v11.4.0...v11.5.0) - 2024-04-23 * [11.x] Add namespace for `make:trait` and `make:interface` command by [@milwad-dev](https://github.com/milwad-dev) in https://github.com/laravel/framework/pull/51083 * [11.x] Ability to generate URL's with query params by [@stevebauman](https://github.com/stevebauman) in https://github.com/laravel/framework/pull/51075 * [11.x] Adds anonymous broadcasting by [@joedixon](https://github.com/joedixon) in https://github.com/laravel/framework/pull/51082 * [10.x] Binding order is incorrect when using cursor paginate with multiple unions with a where by [@thijsvdanker](https://github.com/thijsvdanker) in https://github.com/laravel/framework/pull/50884 * [10.x] Fix cursor paginate with union and column alias by [@thijsvdanker](https://github.com/thijsvdanker) in https://github.com/laravel/framework/pull/50882 * [11.x] Fix typo in tests by [@milwad-dev](https://github.com/milwad-dev) in https://github.com/laravel/framework/pull/51093 * Fix argument type in `Cache\Store` by [@GromNaN](https://github.com/GromNaN) in https://github.com/laravel/framework/pull/51100 * Correct comment's grammatical and semantic errors by [@javadihugo](https://github.com/javadihugo) in https://github.com/laravel/framework/pull/51101 * [11.x] Replace matches typehint fix by [@henzeb](https://github.com/henzeb) in https://github.com/laravel/framework/pull/51095 * [11.x] Exclude `laravel_through_key` when replicating model, fixes #51097 by [@levu42](https://github.com/levu42) in https://github.com/laravel/framework/pull/51098 * [11.x] Add enum types to static Rule methods by [@erik-perri](https://github.com/erik-perri) in https://github.com/laravel/framework/pull/51090 * [11.x] Add decrement method to the rate limiter class by [@AlexJump24](https://github.com/AlexJump24) in https://github.com/laravel/framework/pull/51102 * [11.x] Remove dead code by [@michaelnabil230](https://github.com/michaelnabil230) in https://github.com/laravel/framework/pull/51106 * [11.x] Fix support for other hashing implementations when using `hashed` cast by [@j3j5](https://github.com/j3j5) in https://github.com/laravel/framework/pull/51112 * Revert "[11.x] Adds support for `int` backed enums to implicit `Enum` route binding" by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/51119 * [11.x] Add support for enums in `whereIn` route constraints by [@osbre](https://github.com/osbre) in https://github.com/laravel/framework/pull/51121 * Clarify that \Illuminate\Http\Request::replace replace all input values by [@treyssatvincent](https://github.com/treyssatvincent) in https://github.com/laravel/framework/pull/51123 * [11.x] Fix db:show's --counts option by [@xuchunyang](https://github.com/xuchunyang) in https://github.com/laravel/framework/pull/51140 * Update RuntimeException message when no data has been found by [@mikemeijer](https://github.com/mikemeijer) in https://github.com/laravel/framework/pull/51133 * [11] Update DetectsLostConnections.php by [@it-can](https://github.com/it-can) in https://github.com/laravel/framework/pull/51127 * [11.x] Reset connection after migrate for FreshCommand by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/51167 * [10.x] Address Null Parameter Deprecations in UrlGenerator by [@aldobarr](https://github.com/aldobarr) in https://github.com/laravel/framework/pull/51148 * [11.x] Provide context for NestedRules by [@imahmood](https://github.com/imahmood) in https://github.com/laravel/framework/pull/51160 * [11.x] Fix renaming columns with `NULL` as default on legacy MariaDB/MySQL by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/framework/pull/51177 * [11.x] Supercharge Blade by [@assertchris](https://github.com/assertchris) in https://github.com/laravel/framework/pull/51143 * [11.x] Allow implicit binding to have optional backed enums by [@Neol3108](https://github.com/Neol3108) in https://github.com/laravel/framework/pull/51178 * [11.x] Blade Component Loop Speed Improvement by [@lonnylot](https://github.com/lonnylot) in https://github.com/laravel/framework/pull/51158 * [11.x] Fix normalizedNameCache by [@Jubeki](https://github.com/Jubeki) in https://github.com/laravel/framework/pull/51185 * [11.x] GenericUser use `getAuthPasswordName` instead of hardcoded column name by [@Daniel-H123](https://github.com/Daniel-H123) in https://github.com/laravel/framework/pull/51186 ## [v11.4.0](https://github.com/laravel/framework/compare/v11.3.1...v11.4.0) - 2024-04-16 * [11.x] Apc Cache - Remove long-time gone apc_* functions by [@serpentblade](https://github.com/serpentblade) in https://github.com/laravel/framework/pull/51010 * [11.x] Allowing Usage of Livewire Wire Boolean Style Directives by [@devajmeireles](https://github.com/devajmeireles) in https://github.com/laravel/framework/pull/51007 * [11.x] Introduces `Exceptions` facade by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50704 * [11.x] `afterQuery` hook by [@gdebrauwer](https://github.com/gdebrauwer) in https://github.com/laravel/framework/pull/50587 * Fix computed columns mapping to wrong tables by [@maddhatter](https://github.com/maddhatter) in https://github.com/laravel/framework/pull/51009 * [11.x] improvement test for string title by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/51015 * [11.x] Fix failing `afterQuery` method tests when using sql server by [@gdebrauwer](https://github.com/gdebrauwer) in https://github.com/laravel/framework/pull/51016 * [11.x] Fix: Apply database connection before checking if the repository exist by [@sjspereira](https://github.com/sjspereira) in https://github.com/laravel/framework/pull/51021 * [10.x] Fix error when using `orderByRaw()` in query before using `cursorPaginate()` by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/51023 * [11.x] Add RequiredIfDeclined validation rule by [@timmydhooghe](https://github.com/timmydhooghe) in https://github.com/laravel/framework/pull/51030 * [11.x] Adds support for enums on `mapInto` collection method by [@lukeraymonddowning](https://github.com/lukeraymonddowning) in https://github.com/laravel/framework/pull/51027 * [11.x] Fix prompt fallback return value when using numeric keys by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/50995 * [11.x] Adds support for `int` backed enums to implicit `Enum` route binding by [@monurakkaya](https://github.com/monurakkaya) in https://github.com/laravel/framework/pull/51029 * [11.x] Configuration to disable events on Cache Repository by [@serpentblade](https://github.com/serpentblade) in https://github.com/laravel/framework/pull/51032 * Revert "[11.x] Name of job set by displayName() must be honoured by S… by [@RobertBoes](https://github.com/RobertBoes) in https://github.com/laravel/framework/pull/51034 * chore: fix some typos in comments by [@laterlaugh](https://github.com/laterlaugh) in https://github.com/laravel/framework/pull/51037 * Name of job set by displayName() must be honoured by Schedule by [@SCIF](https://github.com/SCIF) in https://github.com/laravel/framework/pull/51038 * Fix more typos by [@szepeviktor](https://github.com/szepeviktor) in https://github.com/laravel/framework/pull/51039 * [11.x] Fix some doc blocks by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/51043 * [11.x] Add [@throws](https://github.com/throws) ConnectionException tag on Http methods for IDE support by [@masoudtajer](https://github.com/masoudtajer) in https://github.com/laravel/framework/pull/51066 * [11.x] Add Prompts `textarea` fallback for tests and add assertion tests by [@lioneaglesolutions](https://github.com/lioneaglesolutions) in https://github.com/laravel/framework/pull/51055 * Validate MAC per key by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/51063 * [11.x] Add `throttle` method to `LazyCollection` by [@JosephSilber](https://github.com/JosephSilber) in https://github.com/laravel/framework/pull/51060 * [11.x] Pass decay seconds or minutes like hour and day by [@jimmypuckett](https://github.com/jimmypuckett) in https://github.com/laravel/framework/pull/51054 * [11.x] Consider after_commit config in SyncQueue by [@hansnn](https://github.com/hansnn) in https://github.com/laravel/framework/pull/51071 * [10.x] Database layer fixes by [@saadsidqui](https://github.com/saadsidqui) in https://github.com/laravel/framework/pull/49787 * [11.x] Fix context helper always requiring `$key` value by [@nikspyratos](https://github.com/nikspyratos) in https://github.com/laravel/framework/pull/51080 * [11.x] Fix `expectsChoice` assertion with optional `multiselect` prompts. by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/51078 ## [v11.3.1](https://github.com/laravel/framework/compare/v11.3.0...v11.3.1) - 2024-04-10 * [11.x] Name of job set by displayName() must be honoured by Schedule by [@SCIF](https://github.com/SCIF) in https://github.com/laravel/framework/pull/50973 * Add Conditionable trait to Testing\PendingCommand.php by [@tobz-nz](https://github.com/tobz-nz) in https://github.com/laravel/framework/pull/50988 * Allow sorting of route:list by multiple column/factors using a comma by [@fredbradley](https://github.com/fredbradley) in https://github.com/laravel/framework/pull/50998 * [10.x] Added eachById and chunkByIdDesc to BelongsToMany by [@lonnylot](https://github.com/lonnylot) in https://github.com/laravel/framework/pull/50991 ## [v11.3.0](https://github.com/laravel/framework/compare/v11.2.0...v11.3.0) - 2024-04-09 * [10.x] Prevent Redis connection error report flood on queue worker by [@kasus](https://github.com/kasus) in https://github.com/laravel/framework/pull/50812 * [11.x] Optimize SetCacheHeaders to ensure error responses aren't cached by [@MinaWilliam](https://github.com/MinaWilliam) in https://github.com/laravel/framework/pull/50903 * [11.x] Add session `hasAny` method by [@mahmoudmohamedramadan](https://github.com/mahmoudmohamedramadan) in https://github.com/laravel/framework/pull/50897 * [11.x] Add option to report throttled exception in ThrottlesExceptions middleware by [@JaZo](https://github.com/JaZo) in https://github.com/laravel/framework/pull/50896 * [11.x] Add DeleteWhenMissingModels attribute by [@Neol3108](https://github.com/Neol3108) in https://github.com/laravel/framework/pull/50890 * [11.x] Allow customizing TrimStrings::$except by [@grohiro](https://github.com/grohiro) in https://github.com/laravel/framework/pull/50901 * [11.x] Add pull methods to Context by [@renegeuze](https://github.com/renegeuze) in https://github.com/laravel/framework/pull/50904 * [11.x] Remove redundant code from MariaDbGrammar by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/framework/pull/50907 * [11.x] Explicit nullable parameter declarations to fix PHP 8.4 deprecation by [@Jubeki](https://github.com/Jubeki) in https://github.com/laravel/framework/pull/50922 * [11.x] Add setters to cache stores by [@stancl](https://github.com/stancl) in https://github.com/laravel/framework/pull/50912 * [10.x] Laravel 10x optional withSize for hasTable by [@apspan](https://github.com/apspan) in https://github.com/laravel/framework/pull/50888 * [11.x] Fix prompting for missing array arguments on artisan command by [@macocci7](https://github.com/macocci7) in https://github.com/laravel/framework/pull/50850 * [11.x] Add strict-mode safe hasAttribute method to Eloquent by [@mateusjatenee](https://github.com/mateusjatenee) in https://github.com/laravel/framework/pull/50909 * [11.x] add function to get faked events by [@browner12](https://github.com/browner12) in https://github.com/laravel/framework/pull/50905 * [11.x] `retry` func - catch "Throwable" instead of Exception by [@sethsandaru](https://github.com/sethsandaru) in https://github.com/laravel/framework/pull/50944 * chore: remove repetitive words by [@findseat](https://github.com/findseat) in https://github.com/laravel/framework/pull/50943 * [10.x] Add `serializeAndRestore()` to `NotificationFake` by [@dbpolito](https://github.com/dbpolito) in https://github.com/laravel/framework/pull/50935 * [11.x] Prevent crash when handling ConnectionException in HttpClient retry logic by [@shinsenter](https://github.com/shinsenter) in https://github.com/laravel/framework/pull/50955 * [11.x] Remove unknown parameters by [@naopusyu](https://github.com/naopusyu) in https://github.com/laravel/framework/pull/50965 * [11.x] Fixed typo in PHPDoc `[@param](https://github.com/param)` by [@naopusyu](https://github.com/naopusyu) in https://github.com/laravel/framework/pull/50967 * [11.x] Fix dockblock by [@michaelnabil230](https://github.com/michaelnabil230) in https://github.com/laravel/framework/pull/50979 * [11.x] Allow time to be faked in database lock by [@JurianArie](https://github.com/JurianArie) in https://github.com/laravel/framework/pull/50981 * [11.x] Introduce method `Http::createPendingRequest()` by [@Jacobs63](https://github.com/Jacobs63) in https://github.com/laravel/framework/pull/50980 * [11.x] Add [@throws](https://github.com/throws) to some doc blocks by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50969 * [11.x] Fix PHP_MAXPATHLEN check for existing check of files for views by [@joshuaruesweg](https://github.com/joshuaruesweg) in https://github.com/laravel/framework/pull/50962 * [11.x] Allow to remove scopes from BelongsToMany relation by [@plumthedev](https://github.com/plumthedev) in https://github.com/laravel/framework/pull/50953 * [11.x] Throw exception if named rate limiter and model property do not exist by [@mateusjatenee](https://github.com/mateusjatenee) in https://github.com/laravel/framework/pull/50908 ## [v11.2.0](https://github.com/laravel/framework/compare/v11.1.1...v11.2.0) - 2024-04-02 * [11.x] Fix: update `[@param](https://github.com/param)` in some doc block by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50827 * [11.x] Fix: update [@return](https://github.com/return) in some doc blocks by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50826 * [11.x] Fix retrieving generated columns on legacy PostgreSQL by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/framework/pull/50834 * [11.x] Trim invisible characters by [@dasundev](https://github.com/dasundev) in https://github.com/laravel/framework/pull/50832 * [11.x] Add default value for `get` and `getHidden` on `Context` by [@michaelnabil230](https://github.com/michaelnabil230) in https://github.com/laravel/framework/pull/50824 * [11.x] Improves `serve` Artisan command by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50821 * [11.x] Rehash user passwords when logging in once by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/50843 * [11.x] Do not wipe database if it does not exists by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50838 * [11.x] Better database creation failure handling by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50836 * [11.x] Use Default Schema Name on SQL Server by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/framework/pull/50855 * Correct typing for startedAs and virtualAs database column definitions by [@ollieread](https://github.com/ollieread) in https://github.com/laravel/framework/pull/50851 * Allow passing query Expression as column in Many-to-Many relationship by [@plumthedev](https://github.com/plumthedev) in https://github.com/laravel/framework/pull/50849 * [11.x] Fix `Middleware::trustHosts(subdomains: true)` by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/50877 * [11.x] Modify doc blocks for getGateArguments by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50874 * [11.x] Add `[@throws](https://github.com/throws)` to doc block for resolve method by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50873 * [11.x] Str trim methods by [@patrickomeara](https://github.com/patrickomeara) in https://github.com/laravel/framework/pull/50822 * [11.x] Add fluent helper by [@PhiloNL](https://github.com/PhiloNL) in https://github.com/laravel/framework/pull/50848 * [11.x] Add a new helper for context by [@michaelnabil230](https://github.com/michaelnabil230) in https://github.com/laravel/framework/pull/50878 * [11.x] `assertChain` and `assertNoChain` on job instance by [@gdebrauwer](https://github.com/gdebrauwer) in https://github.com/laravel/framework/pull/50858 * [11.x] Remove redundant `getDefaultNamespace` method in some classes (class, interface and trait commands) by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50880 * [11.x] Remove redundant implementation of ConnectorInterface in MariaDbConnector by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50881 * [11.X] Fix: error when using `orderByRaw` in query before using `cursorPaginate` by [@ngunyimacharia](https://github.com/ngunyimacharia) in https://github.com/laravel/framework/pull/50887 ## [v11.1.1](https://github.com/laravel/framework/compare/v11.1.0...v11.1.1) - 2024-03-28 * [11.x] Fix: update `[@param](https://github.com/param)` in doc blocks by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50791 * [11.x] Fix query builder `whereBetween` with CarbonPeriod and Carbon 3 by [@bakerkretzmar](https://github.com/bakerkretzmar) in https://github.com/laravel/framework/pull/50792 * [11.x] Allows asserting no output in Artisan commands by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50702 * fix typo by [@elguitarraverde](https://github.com/elguitarraverde) in https://github.com/laravel/framework/pull/50808 * [11.x] Make DB::usingConnection() respect read/write type by [@SajtiDH](https://github.com/SajtiDH) in https://github.com/laravel/framework/pull/50806 * [11.x] Fix deprecation warning caused by Carbon 3.2 by [@JackWH](https://github.com/JackWH) in https://github.com/laravel/framework/pull/50813 ## [v11.1.0](https://github.com/laravel/framework/compare/v11.0.8...v11.1.0) - 2024-03-26 * [11.x] MySQL transaction isolation level fix by [@mwikberg-virta](https://github.com/mwikberg-virta) in https://github.com/laravel/framework/pull/50689 * [11.x] Add ListManagementOptions in SES mail transport by [@arifszn](https://github.com/arifszn) in https://github.com/laravel/framework/pull/50660 * [11.x] Accept non-backed enum in database queries by [@gbalduzzi](https://github.com/gbalduzzi) in https://github.com/laravel/framework/pull/50674 * [11.x] Add `Conditionable` trait to `Context` by [@michaelnabil230](https://github.com/michaelnabil230) in https://github.com/laravel/framework/pull/50707 * [11.x] Adds `[@throws](https://github.com/throws)` section to the Context's doc blocks by [@rnambaale](https://github.com/rnambaale) in https://github.com/laravel/framework/pull/50715 * [11.x] Test modifying nullable columns by [@hafezdivandari](https://github.com/hafezdivandari) in https://github.com/laravel/framework/pull/50708 * [11.x] Introduce HASH_VERIFY env var by [@valorin](https://github.com/valorin) in https://github.com/laravel/framework/pull/50718 * [11.x] Apply default timezone when casting unix timestamps by [@daniser](https://github.com/daniser) in https://github.com/laravel/framework/pull/50751 * [11.x] Fixes `ApplicationBuilder::withCommandRouting()` usage by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/50742 * [11.x] Register console commands, paths and routes after the app is booted by [@plumthedev](https://github.com/plumthedev) in https://github.com/laravel/framework/pull/50738 * [11.x] Enhance malformed request handling by [@jnoordsij](https://github.com/jnoordsij) in https://github.com/laravel/framework/pull/50735 * [11.x] Adds `withSchedule` to `bootstrap/app.php` file by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50755 * [11.x] Fix dock block for create method in `InvalidArgumentException.php` by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50762 * [11.x] signature typo by [@abrahamgreyson](https://github.com/abrahamgreyson) in https://github.com/laravel/framework/pull/50766 * [11.x] Simplify `ApplicationBuilder::withSchedule()` by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/50765 ## [v11.0.8](https://github.com/laravel/framework/compare/v11.0.7...v11.0.8) - 2024-03-21 * [11.x] Change typehint for enum rule from string to class-string by [@liamduckett](https://github.com/liamduckett) in https://github.com/laravel/framework/pull/50603 * [11.x] Fixed enum and enum.backed stub paths after publish by [@haroon-mahmood-4276](https://github.com/haroon-mahmood-4276) in https://github.com/laravel/framework/pull/50629 * [11.x] Fix(ScheduleListCommand): fix doc block for listEvent method by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50638 * [11.x] Re: Fix issue with missing 'js/' directory in broadcasting installation command by [@alnahian2003](https://github.com/alnahian2003) in https://github.com/laravel/framework/pull/50657 * [11.x] Remove `$except` property from `ExcludesPaths` trait by [@gdebrauwer](https://github.com/gdebrauwer) in https://github.com/laravel/framework/pull/50644 * [11.x] Fix command alias registration and usage. by [@timacdonald](https://github.com/timacdonald) in https://github.com/laravel/framework/pull/50617 * [11.x] Fixed make:session-table Artisan command cannot be executed if a migration exists by [@naopusyu](https://github.com/naopusyu) in https://github.com/laravel/framework/pull/50615 * [11.x] Fix(src\illuminate\Queue): update doc block, Simplification of the code in RedisManager by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50635 * [11.x] Add `--without-reverb` and `--without-node` arguments to `install:broadcasting` command by [@duncanmcclean](https://github.com/duncanmcclean) in https://github.com/laravel/framework/pull/50662 * [11.x] Fixed `trait` stub paths after publish by [@haroon-mahmood-4276](https://github.com/haroon-mahmood-4276) in https://github.com/laravel/framework/pull/50678 * [11.x] Fixed `class` and `class.invokable` stub paths after publish by [@haroon-mahmood-4276](https://github.com/haroon-mahmood-4276) in https://github.com/laravel/framework/pull/50676 * [10.x] Fix `Collection::concat()` return type by [@axlon](https://github.com/axlon) in https://github.com/laravel/framework/pull/50669 * [11.x] Fix adding multiple bootstrap providers with opcache by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/framework/pull/50665 * [11.x] Allow `BackedEnum` and `UnitEnum` in `Rule::in` and `Rule::notIn` by [@PerryvanderMeer](https://github.com/PerryvanderMeer) in https://github.com/laravel/framework/pull/50680 * [10.x] Fix command alias registration and usage by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/50695 ## [v11.0.7](https://github.com/laravel/framework/compare/v11.0.6...v11.0.7) - 2024-03-15 * [11.x] Re-add translations for ValidationException by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50546 * [11.x] Removes unused Dumpable trait by [@OussamaMater](https://github.com/OussamaMater) in https://github.com/laravel/framework/pull/50559 * [11.x] Fix withRouting docblock type by [@santigarcor](https://github.com/santigarcor) in https://github.com/laravel/framework/pull/50563 * [11.x] Fix docblock in FakeInvokedProcess.php by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50568 * [11.x] fix: Add missing InvalidArgumentException import to Database/Schema/SqlServerBuilder by [@ayutaya](https://github.com/ayutaya) in https://github.com/laravel/framework/pull/50573 * [11.x] Improved translation for displaying the count of errors in the validation message by [@andrey-helldar](https://github.com/andrey-helldar) in https://github.com/laravel/framework/pull/50560 * [11.x] Fix retry_after to be an integer by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50580 * [11.x] Use available `getPath()` instead of using `app_path()` to detect if base controller exists by [@crynobone](https://github.com/crynobone) in https://github.com/laravel/framework/pull/50583 * [11.x] Fix doc block: `[@return](https://github.com/return) static` has been modified to `[@return](https://github.com/return) void` by [@saMahmoudzadeh](https://github.com/saMahmoudzadeh) in https://github.com/laravel/framework/pull/50592 * accept attributes for channels by [@taylorotwell](https://github.com/taylorotwell) in https://github.com/laravel/framework/commit/398f49485e305756409b52af64837c784fd30de9 ## [v11.0.6](https://github.com/laravel/framework/compare/v11.0.5...v11.0.6) - 2024-03-14 * [11.x] Fix version constraints for illuminate/process by [@riesjart](https://github.com/riesjart) in https://github.com/laravel/framework/pull/50524 * [11.x] Update Broadcasting Install Command With Bun Support by [@HDVinnie](https://github.com/HDVinnie) in https://github.com/laravel/framework/pull/50525 * [11.x] Allows to comment `web` and `health` routes by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50533 * [11.x] Add generics for Arr::first() by [@phh](https://github.com/phh) in https://github.com/laravel/framework/pull/50514 * Change default collation for MySQL by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50555 * [11.x] Fixes install:broadcasting command by [@joedixon](https://github.com/joedixon) in https://github.com/laravel/framework/pull/50550 * [11.x] Fix crash when configuration directory is non-existing by [@buismaarten](https://github.com/buismaarten) in https://github.com/laravel/framework/pull/50537 ## [v11.0.5](https://github.com/laravel/framework/compare/v11.0.4...v11.0.5) - 2024-03-13 * [11.x] Improves broadcasting install by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50519 * [11.x] Improved exception message on 'ensure' method by [@fgaroby](https://github.com/fgaroby) in https://github.com/laravel/framework/pull/50517 * [11.x] Add hasValidRelativeSignatureWhileIgnoring macro by [@br13an](https://github.com/br13an) in https://github.com/laravel/framework/pull/50511 * [11.x] Prevents database redis options of being merged by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50523 ## [v11.0.4](https://github.com/laravel/framework/compare/v11.0.3...v11.0.4) - 2024-03-13 * [11.x] Add class_exists check for `Spark`'s `subscribed` default alias Middleware by [@akr4m](https://github.com/akr4m) in https://github.com/laravel/framework/pull/50489 * [11.x] Fix: Removed TTY mode to resolve Windows compatibility issue by [@yourchocomate](https://github.com/yourchocomate) in https://github.com/laravel/framework/pull/50495 * [11.x] Check for password before storing hash in session by [@valorin](https://github.com/valorin) in https://github.com/laravel/framework/pull/50507 * [11.x] Fix an issue with missing controller class by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50505 * [11.x] Add default empty config when creating repository within CacheManager by [@noefleury](https://github.com/noefleury) in https://github.com/laravel/framework/pull/50510 ## [v11.0.3](https://github.com/laravel/framework/compare/v11.0.2...v11.0.3) - 2024-03-12 * [11.x] Arr helper map spread by [@bilfeldt](https://github.com/bilfeldt) in https://github.com/laravel/framework/pull/50474 * [11.x] add `list` rule by [@medilies](https://github.com/medilies) in https://github.com/laravel/framework/pull/50454 * [11.x] Fixes installation of passport by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50488 ## [v11.0.2](https://github.com/laravel/framework/compare/v11.0.1...v11.0.2) - 2024-03-12 * [11.x] Adds `--graceful` to `php artisan migrate` by [@nunomaduro](https://github.com/nunomaduro) in https://github.com/laravel/framework/pull/50486 ## [v11.0.1](https://github.com/laravel/framework/compare/v11.0.0..v11.0.1) - 2024-03-12 * [10.x] Update mockery conflict to just disallow the broken version by [@GrahamCampbell](https://github.com/GrahamCampbell) in https://github.com/laravel/framework/pull/50472 * [10.x] Conflict with specific release by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50473 * [10.x] Fix for attributes being escaped on Dynamic Blade Components by [@pascalbaljet](https://github.com/pascalbaljet) in https://github.com/laravel/framework/pull/50471 * [10.x] Revert PR 50403 by [@driesvints](https://github.com/driesvints) in https://github.com/laravel/framework/pull/50482 ## v11.0.0 - 2024-03-12 Check the upgrade guide in the [Official Laravel Upgrade Documentation](https://laravel.com/docs/11.x/upgrade). Also you can see some release notes in the [Official Laravel Release Documentation](https://laravel.com/docs/11.x/releases). framework/composer.json 0000644 00000020741 15060132306 0011263 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.2", "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|^0.12", "doctrine/inflector": "^2.0.5", "dragonmantank/cron-expression": "^3.3.2", "egulias/email-validator": "^3.2.1|^4.0", "fruitcake/php-cors": "^1.3", "guzzlehttp/guzzle": "^7.8", "guzzlehttp/uri-template": "^1.0", "laravel/prompts": "^0.1.18", "laravel/serializable-closure": "^1.3", "league/commonmark": "^2.2.1", "league/flysystem": "^3.8.0", "monolog/monolog": "^3.0", "nesbot/carbon": "^2.72.2|^3.0", "nunomaduro/termwind": "^2.0", "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": "^7.0", "symfony/error-handler": "^7.0", "symfony/finder": "^7.0", "symfony/http-foundation": "^7.0", "symfony/http-kernel": "^7.0", "symfony/mailer": "^7.0", "symfony/mime": "^7.0", "symfony/polyfill-php83": "^1.28", "symfony/process": "^7.0", "symfony/routing": "^7.0", "symfony/uid": "^7.0", "symfony/var-dumper": "^7.0", "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", "spatie/once": "*" }, "require-dev": { "ext-gmp": "*", "ably/ably-php": "^1.0", "aws/aws-sdk-php": "^3.235.5", "fakerphp/faker": "^1.23", "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.6", "nyholm/psr7": "^1.2", "orchestra/testbench-core": "^9.0.15", "pda/pheanstalk": "^5.0", "phpstan/phpstan": "^1.4.7", "phpunit/phpunit": "^10.5|^11.0", "predis/predis": "^2.0.2", "resend/resend-php": "^0.10.0", "symfony/cache": "^7.0", "symfony/http-client": "^7.0", "symfony/psr-http-message-bridge": "^7.0" }, "conflict": { "mockery/mockery": "1.6.8", "tightenco/collect": "<5.5.33" }, "provide": { "psr/container-implementation": "1.1|2.0", "psr/simple-cache-implementation": "1.0|2.0|3.0" }, "autoload": { "files": [ "src/Illuminate/Collections/helpers.php", "src/Illuminate/Events/functions.php", "src/Illuminate/Filesystem/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": "11.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|^6.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 (^7.0|^8.0).", "fakerphp/faker": "Required to use the eloquent factory builder (^1.9.1).", "filp/whoops": "Required for friendly error pages in development (^2.14.3).", "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.6).", "nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).", "pda/pheanstalk": "Required to use the beanstalk queue driver (^5.0).", "phpunit/phpunit": "Required to use assertions and run tests (^10.5|^11.0).", "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).", "resend/resend-php": "Required to enable support for the Resend mail transport (^0.10.0).", "symfony/cache": "Required to PSR-6 cache bridge (^7.0).", "symfony/filesystem": "Required to enable support for relative symbolic links (^7.0).", "symfony/http-client": "Required to enable support for the Symfony API mail transports (^7.0).", "symfony/mailgun-mailer": "Required to enable support for the Mailgun mail transport (^7.0).", "symfony/postmark-mailer": "Required to enable support for the Postmark mail transport (^7.0).", "symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^7.0)." }, "config": { "sort-packages": true, "allow-plugins": { "composer/package-versions-deprecated": true } }, "minimum-stability": "stable", "prefer-stable": true } framework/config/logging.php 0000644 00000010326 15060132306 0012143 0 ustar 00 <?php use Monolog\Handler\NullHandler; use Monolog\Handler\StreamHandler; use Monolog\Handler\SyslogUdpHandler; use Monolog\Processor\PsrLogMessageProcessor; return [ /* |-------------------------------------------------------------------------- | Default Log Channel |-------------------------------------------------------------------------- | | This option defines the default log channel that is utilized to write | messages to your logs. The value provided here should match one of | the channels present in the list of "channels" configured below. | */ 'default' => env('LOG_CHANNEL', 'stack'), /* |-------------------------------------------------------------------------- | Deprecations Log Channel |-------------------------------------------------------------------------- | | This option controls the log channel that should be used to log warnings | regarding deprecated PHP and library features. This allows you to get | your application ready for upcoming major versions of dependencies. | */ 'deprecations' => [ 'channel' => env('LOG_DEPRECATIONS_CHANNEL', 'null'), 'trace' => env('LOG_DEPRECATIONS_TRACE', false), ], /* |-------------------------------------------------------------------------- | Log Channels |-------------------------------------------------------------------------- | | Here you may configure the log channels for your application. Laravel | utilizes the Monolog PHP logging library, which includes a variety | of powerful log handlers and formatters that you're free to use. | | Available Drivers: "single", "daily", "slack", "syslog", | "errorlog", "monolog", "custom", "stack" | */ 'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => explode(',', env('LOG_STACK', 'single')), 'ignore_exceptions' => false, ], 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => env('LOG_LEVEL', 'debug'), 'replace_placeholders' => true, ], 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => env('LOG_LEVEL', 'debug'), 'days' => env('LOG_DAILY_DAYS', 14), 'replace_placeholders' => true, ], 'slack' => [ 'driver' => 'slack', 'url' => env('LOG_SLACK_WEBHOOK_URL'), 'username' => env('LOG_SLACK_USERNAME', 'Laravel Log'), 'emoji' => env('LOG_SLACK_EMOJI', ':boom:'), 'level' => env('LOG_LEVEL', 'critical'), 'replace_placeholders' => true, ], 'papertrail' => [ 'driver' => 'monolog', 'level' => env('LOG_LEVEL', 'debug'), 'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class), 'handler_with' => [ 'host' => env('PAPERTRAIL_URL'), 'port' => env('PAPERTRAIL_PORT'), 'connectionString' => 'tls://'.env('PAPERTRAIL_URL').':'.env('PAPERTRAIL_PORT'), ], 'processors' => [PsrLogMessageProcessor::class], ], 'stderr' => [ 'driver' => 'monolog', 'level' => env('LOG_LEVEL', 'debug'), 'handler' => StreamHandler::class, 'formatter' => env('LOG_STDERR_FORMATTER'), 'with' => [ 'stream' => 'php://stderr', ], 'processors' => [PsrLogMessageProcessor::class], ], 'syslog' => [ 'driver' => 'syslog', 'level' => env('LOG_LEVEL', 'debug'), 'facility' => env('LOG_SYSLOG_FACILITY', LOG_USER), 'replace_placeholders' => true, ], 'errorlog' => [ 'driver' => 'errorlog', 'level' => env('LOG_LEVEL', 'debug'), 'replace_placeholders' => true, ], 'null' => [ 'driver' => 'monolog', 'handler' => NullHandler::class, ], 'emergency' => [ 'path' => storage_path('logs/laravel.log'), ], ], ]; framework/config/cache.php 0000644 00000006534 15060132306 0011566 0 ustar 00 <?php use Illuminate\Support\Str; return [ /* |-------------------------------------------------------------------------- | Default Cache Store |-------------------------------------------------------------------------- | | This option controls the default cache store that will be used by the | framework. This connection is utilized if another isn't explicitly | specified when running a cache operation inside the application. | */ 'default' => env('CACHE_STORE', 'database'), /* |-------------------------------------------------------------------------- | Cache Stores |-------------------------------------------------------------------------- | | Here you may define all of the cache "stores" for your application as | well as their drivers. You may even define multiple stores for the | same cache driver to group types of items stored in your caches. | | Supported drivers: "array", "database", "file", "memcached", | "redis", "dynamodb", "octane", "null" | */ 'stores' => [ 'array' => [ 'driver' => 'array', 'serialize' => false, ], 'database' => [ 'driver' => 'database', 'table' => env('DB_CACHE_TABLE', 'cache'), 'connection' => env('DB_CACHE_CONNECTION'), 'lock_connection' => env('DB_CACHE_LOCK_CONNECTION'), ], 'file' => [ 'driver' => 'file', 'path' => storage_path('framework/cache/data'), 'lock_path' => storage_path('framework/cache/data'), ], 'memcached' => [ 'driver' => 'memcached', 'persistent_id' => env('MEMCACHED_PERSISTENT_ID'), 'sasl' => [ env('MEMCACHED_USERNAME'), env('MEMCACHED_PASSWORD'), ], 'options' => [ // Memcached::OPT_CONNECT_TIMEOUT => 2000, ], 'servers' => [ [ 'host' => env('MEMCACHED_HOST', '127.0.0.1'), 'port' => env('MEMCACHED_PORT', 11211), 'weight' => 100, ], ], ], 'redis' => [ 'driver' => 'redis', 'connection' => env('REDIS_CACHE_CONNECTION', 'cache'), 'lock_connection' => env('REDIS_CACHE_LOCK_CONNECTION', 'default'), ], 'dynamodb' => [ 'driver' => 'dynamodb', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'table' => env('DYNAMODB_CACHE_TABLE', 'cache'), 'endpoint' => env('DYNAMODB_ENDPOINT'), ], 'octane' => [ 'driver' => 'octane', ], ], /* |-------------------------------------------------------------------------- | Cache Key Prefix |-------------------------------------------------------------------------- | | When utilizing the APC, database, memcached, Redis, and DynamoDB cache | stores, there might be other applications using the same cache. For | that reason, you may prefix every cache key to avoid collisions. | */ 'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache_'), ]; framework/config/database.php 0000644 00000013713 15060132306 0012264 0 ustar 00 <?php use Illuminate\Support\Str; return [ /* |-------------------------------------------------------------------------- | Default Database Connection Name |-------------------------------------------------------------------------- | | Here you may specify which of the database connections below you wish | to use as your default connection for database operations. This is | the connection which will be utilized unless another connection | is explicitly specified when you execute a query / statement. | */ 'default' => env('DB_CONNECTION', 'sqlite'), /* |-------------------------------------------------------------------------- | Database Connections |-------------------------------------------------------------------------- | | Below are all of the database connections defined for your application. | An example configuration is provided for each database system which | is supported by Laravel. You're free to add / remove connections. | */ 'connections' => [ 'sqlite' => [ 'driver' => 'sqlite', 'url' => env('DB_URL'), 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), ], 'mysql' => [ 'driver' => 'mysql', 'url' => env('DB_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'laravel'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => env('DB_CHARSET', 'utf8mb4'), 'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'), 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ], 'mariadb' => [ 'driver' => 'mariadb', 'url' => env('DB_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'laravel'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => env('DB_CHARSET', 'utf8mb4'), 'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'), 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ], 'pgsql' => [ 'driver' => 'pgsql', 'url' => env('DB_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '5432'), 'database' => env('DB_DATABASE', 'laravel'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'charset' => env('DB_CHARSET', 'utf8'), 'prefix' => '', 'prefix_indexes' => true, 'search_path' => 'public', 'sslmode' => 'prefer', ], 'sqlsrv' => [ 'driver' => 'sqlsrv', 'url' => env('DB_URL'), 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '1433'), 'database' => env('DB_DATABASE', 'laravel'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'charset' => env('DB_CHARSET', 'utf8'), 'prefix' => '', 'prefix_indexes' => true, // 'encrypt' => env('DB_ENCRYPT', 'yes'), // 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'), ], ], /* |-------------------------------------------------------------------------- | Migration Repository Table |-------------------------------------------------------------------------- | | This table keeps track of all the migrations that have already run for | your application. Using this information, we can determine which of | the migrations on disk haven't actually been run on the database. | */ 'migrations' => [ 'table' => 'migrations', 'update_date_on_publish' => true, ], /* |-------------------------------------------------------------------------- | Redis Databases |-------------------------------------------------------------------------- | | Redis is an open source, fast, and advanced key-value store that also | provides a richer body of commands than a typical key-value system | such as Memcached. You may define your connection settings here. | */ 'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'), ], 'default' => [ 'url' => env('REDIS_URL'), 'host' => env('REDIS_HOST', '127.0.0.1'), 'username' => env('REDIS_USERNAME'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_DB', '0'), ], 'cache' => [ 'url' => env('REDIS_URL'), 'host' => env('REDIS_HOST', '127.0.0.1'), 'username' => env('REDIS_USERNAME'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_CACHE_DB', '1'), ], ], ]; framework/config/hashing.php 0000644 00000004237 15060132306 0012142 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Default Hash Driver |-------------------------------------------------------------------------- | | This option controls the default hash driver that will be used to hash | passwords for your application. By default, the bcrypt algorithm is | used; however, you remain free to modify this option if you wish. | | Supported: "bcrypt", "argon", "argon2id" | */ 'driver' => env('HASH_DRIVER', 'bcrypt'), /* |-------------------------------------------------------------------------- | Bcrypt Options |-------------------------------------------------------------------------- | | Here you may specify the configuration options that should be used when | passwords are hashed using the Bcrypt algorithm. This will allow you | to control the amount of time it takes to hash the given password. | */ 'bcrypt' => [ 'rounds' => env('BCRYPT_ROUNDS', 12), 'verify' => env('HASH_VERIFY', true), ], /* |-------------------------------------------------------------------------- | Argon Options |-------------------------------------------------------------------------- | | Here you may specify the configuration options that should be used when | passwords are hashed using the Argon algorithm. These will allow you | to control the amount of time it takes to hash the given password. | */ 'argon' => [ 'memory' => env('ARGON_MEMORY', 65536), 'threads' => env('ARGON_THREADS', 1), 'time' => env('ARGON_TIME', 4), 'verify' => env('HASH_VERIFY', true), ], /* |-------------------------------------------------------------------------- | Rehash On Login |-------------------------------------------------------------------------- | | Setting this option to true will tell Laravel to automatically rehash | the user's password during login if the configured work factor for | the algorithm has changed, allowing graceful upgrades of hashes. | */ 'rehash_on_login' => true, ]; framework/config/mail.php 0000644 00000010006 15060132306 0011432 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Default Mailer |-------------------------------------------------------------------------- | | This option controls the default mailer that is used to send all email | messages unless another mailer is explicitly specified when sending | the message. All additional mailers can be configured within the | "mailers" array. Examples of each type of mailer are provided. | */ 'default' => env('MAIL_MAILER', 'log'), /* |-------------------------------------------------------------------------- | Mailer Configurations |-------------------------------------------------------------------------- | | Here you may configure all of the mailers used by your application plus | their respective settings. Several examples have been configured for | you and you are free to add your own as your application requires. | | Laravel supports a variety of mail "transport" drivers that can be used | when delivering an email. You may specify which one you're using for | your mailers below. You may also add additional mailers if needed. | | Supported: "smtp", "sendmail", "mailgun", "ses", "ses-v2", | "postmark", "resend", "log", "array", | "failover", "roundrobin" | */ 'mailers' => [ 'smtp' => [ 'transport' => 'smtp', 'url' => env('MAIL_URL'), 'host' => env('MAIL_HOST', '127.0.0.1'), 'port' => env('MAIL_PORT', 2525), 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, 'local_domain' => env('MAIL_EHLO_DOMAIN'), ], 'ses' => [ 'transport' => 'ses', ], 'postmark' => [ 'transport' => 'postmark', // 'message_stream_id' => env('POSTMARK_MESSAGE_STREAM_ID'), // 'client' => [ // 'timeout' => 5, // ], ], 'resend' => [ 'transport' => 'resend', ], 'sendmail' => [ 'transport' => 'sendmail', 'path' => env('MAIL_SENDMAIL_PATH', '/usr/sbin/sendmail -bs -i'), ], 'log' => [ 'transport' => 'log', 'channel' => env('MAIL_LOG_CHANNEL'), ], 'array' => [ 'transport' => 'array', ], 'failover' => [ 'transport' => 'failover', 'mailers' => [ 'smtp', 'log', ], ], 'roundrobin' => [ 'transport' => 'roundrobin', 'mailers' => [ 'ses', 'postmark', ], ], ], /* |-------------------------------------------------------------------------- | Global "From" Address |-------------------------------------------------------------------------- | | You may wish for all emails sent by your application to be sent from | the same address. Here you may specify a name and address that is | used globally for all emails that are sent by your application. | */ 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'), 'name' => env('MAIL_FROM_NAME', 'Example'), ], /* |-------------------------------------------------------------------------- | Markdown Mail Settings |-------------------------------------------------------------------------- | | If you are using Markdown based email rendering, you may configure your | theme and component paths here, allowing you to customize the design | of the emails. Or, you may simply stick with the Laravel defaults! | */ 'markdown' => [ 'theme' => env('MAIL_MARKDOWN_THEME', 'default'), 'paths' => [ resource_path('views/vendor/mail'), ], ], ]; framework/config/auth.php 0000644 00000007675 15060132306 0011473 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Authentication Defaults |-------------------------------------------------------------------------- | | This option defines the default authentication "guard" and password | reset "broker" for your application. You may change these values | as required, but they're a perfect start for most applications. | */ 'defaults' => [ 'guard' => env('AUTH_GUARD', 'web'), 'passwords' => env('AUTH_PASSWORD_BROKER', 'users'), ], /* |-------------------------------------------------------------------------- | Authentication Guards |-------------------------------------------------------------------------- | | Next, you may define every authentication guard for your application. | Of course, a great default configuration has been defined for you | which utilizes session storage plus the Eloquent user provider. | | All authentication guards have a user provider, which defines how the | users are actually retrieved out of your database or other storage | system used by the application. Typically, Eloquent is utilized. | | Supported: "session" | */ 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], ], /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication guards have a user provider, which defines how the | users are actually retrieved out of your database or other storage | system used by the application. Typically, Eloquent is utilized. | | If you have multiple user tables or models you may configure multiple | providers to represent the model / table. These providers may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => env('AUTH_MODEL', App\Models\User::class), ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], /* |-------------------------------------------------------------------------- | Resetting Passwords |-------------------------------------------------------------------------- | | These configuration options specify the behavior of Laravel's password | reset functionality, including the table utilized for token storage | and the user provider that is invoked to actually retrieve users. | | The expiry time is the number of minutes that each reset token will be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | | The throttle setting is the number of seconds a user must wait before | generating more password reset tokens. This prevents the user from | quickly generating a very large amount of password reset tokens. | */ 'passwords' => [ 'users' => [ 'provider' => 'users', 'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'), 'expire' => 60, 'throttle' => 60, ], ], /* |-------------------------------------------------------------------------- | Password Confirmation Timeout |-------------------------------------------------------------------------- | | Here you may define the amount of seconds before a password confirmation | window expires and users are asked to re-enter their password via the | confirmation screen. By default, the timeout lasts for three hours. | */ 'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800), ]; framework/config/queue.php 0000644 00000007360 15060132306 0011645 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Default Queue Connection Name |-------------------------------------------------------------------------- | | Laravel's queue supports a variety of backends via a single, unified | API, giving you convenient access to each backend using identical | syntax for each. The default queue connection is defined below. | */ 'default' => env('QUEUE_CONNECTION', 'database'), /* |-------------------------------------------------------------------------- | Queue Connections |-------------------------------------------------------------------------- | | Here you may configure the connection options for every queue backend | used by your application. An example configuration is provided for | each backend supported by Laravel. You're also free to add more. | | Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null" | */ 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'database' => [ 'driver' => 'database', 'connection' => env('DB_QUEUE_CONNECTION'), 'table' => env('DB_QUEUE_TABLE', 'jobs'), 'queue' => env('DB_QUEUE', 'default'), 'retry_after' => (int) env('DB_QUEUE_RETRY_AFTER', 90), 'after_commit' => false, ], 'beanstalkd' => [ 'driver' => 'beanstalkd', 'host' => env('BEANSTALKD_QUEUE_HOST', 'localhost'), 'queue' => env('BEANSTALKD_QUEUE', 'default'), 'retry_after' => (int) env('BEANSTALKD_QUEUE_RETRY_AFTER', 90), 'block_for' => 0, 'after_commit' => false, ], 'sqs' => [ 'driver' => 'sqs', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'), 'queue' => env('SQS_QUEUE', 'default'), 'suffix' => env('SQS_SUFFIX'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'after_commit' => false, ], 'redis' => [ 'driver' => 'redis', 'connection' => env('REDIS_QUEUE_CONNECTION', 'default'), 'queue' => env('REDIS_QUEUE', 'default'), 'retry_after' => (int) env('REDIS_QUEUE_RETRY_AFTER', 90), 'block_for' => null, 'after_commit' => false, ], ], /* |-------------------------------------------------------------------------- | Job Batching |-------------------------------------------------------------------------- | | The following options configure the database and table that store job | batching information. These options can be updated to any database | connection and table which has been defined by your application. | */ 'batching' => [ 'database' => env('DB_CONNECTION', 'sqlite'), 'table' => 'job_batches', ], /* |-------------------------------------------------------------------------- | Failed Queue Jobs |-------------------------------------------------------------------------- | | These options configure the behavior of failed queue job logging so you | can control how and where failed jobs are stored. Laravel ships with | support for storing failed jobs in a simple file or in a database. | | Supported drivers: "database-uuids", "dynamodb", "file", "null" | */ 'failed' => [ 'driver' => env('QUEUE_FAILED_DRIVER', 'database-uuids'), 'database' => env('DB_CONNECTION', 'sqlite'), 'table' => 'failed_jobs', ], ]; framework/config/broadcasting.php 0000644 00000005172 15060132306 0013160 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Default Broadcaster |-------------------------------------------------------------------------- | | This option controls the default broadcaster that will be used by the | framework when an event needs to be broadcast. You may set this to | any of the connections defined in the "connections" array below. | | Supported: "reverb", "pusher", "ably", "redis", "log", "null" | */ 'default' => env('BROADCAST_CONNECTION', 'null'), /* |-------------------------------------------------------------------------- | Broadcast Connections |-------------------------------------------------------------------------- | | Here you may define all of the broadcast connections that will be used | to broadcast events to other systems or over WebSockets. Samples of | each available type of connection are provided inside this array. | */ 'connections' => [ 'reverb' => [ 'driver' => 'reverb', 'key' => env('REVERB_APP_KEY'), 'secret' => env('REVERB_APP_SECRET'), 'app_id' => env('REVERB_APP_ID'), 'options' => [ 'host' => env('REVERB_HOST'), 'port' => env('REVERB_PORT', 443), 'scheme' => env('REVERB_SCHEME', 'https'), 'useTLS' => env('REVERB_SCHEME', 'https') === 'https', ], 'client_options' => [ // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html ], ], 'pusher' => [ 'driver' => 'pusher', 'key' => env('PUSHER_APP_KEY'), 'secret' => env('PUSHER_APP_SECRET'), 'app_id' => env('PUSHER_APP_ID'), 'options' => [ 'cluster' => env('PUSHER_APP_CLUSTER'), 'host' => env('PUSHER_HOST') ?: 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com', 'port' => env('PUSHER_PORT', 443), 'scheme' => env('PUSHER_SCHEME', 'https'), 'encrypted' => true, 'useTLS' => env('PUSHER_SCHEME', 'https') === 'https', ], 'client_options' => [ // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html ], ], 'ably' => [ 'driver' => 'ably', 'key' => env('ABLY_KEY'), ], 'log' => [ 'driver' => 'log', ], 'null' => [ 'driver' => 'null', ], ], ]; framework/config/services.php 0000644 00000002013 15060132306 0012332 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Third Party Services |-------------------------------------------------------------------------- | | This file is for storing the credentials for third party services such | as Mailgun, Postmark, AWS and more. This file provides the de facto | location for this type of information, allowing packages to have | a conventional file to locate the various service credentials. | */ 'postmark' => [ 'token' => env('POSTMARK_TOKEN'), ], 'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], 'resend' => [ 'key' => env('RESEND_KEY'), ], 'slack' => [ 'notifications' => [ 'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'), 'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'), ], ], ]; framework/config/cors.php 0000644 00000001516 15060132306 0011464 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Cross-Origin Resource Sharing (CORS) Configuration |-------------------------------------------------------------------------- | | Here you may configure your settings for cross-origin resource sharing | or "CORS". This determines what cross-origin operations may execute | in web browsers. You are free to adjust these settings as needed. | | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS | */ 'paths' => ['api/*', 'sanctum/csrf-cookie'], 'allowed_methods' => ['*'], 'allowed_origins' => ['*'], 'allowed_origins_patterns' => [], 'allowed_headers' => ['*'], 'exposed_headers' => [], 'max_age' => 0, 'supports_credentials' => false, ]; framework/config/view.php 0000644 00000002035 15060132306 0011465 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | View Storage Paths |-------------------------------------------------------------------------- | | Most templating systems load templates from disk. Here you may specify | an array of paths that should be checked for your views. Of course | the usual Laravel view path has already been registered for you. | */ 'paths' => [ resource_path('views'), ], /* |-------------------------------------------------------------------------- | Compiled View Path |-------------------------------------------------------------------------- | | This option determines where all the compiled Blade templates will be | stored for your application. Typically, this is within the storage | directory. However, as usual, you are free to change this value. | */ 'compiled' => env( 'VIEW_COMPILED_PATH', realpath(storage_path('framework/views')) ), ]; framework/config/session.php 0000644 00000017254 15060132306 0012207 0 ustar 00 <?php use Illuminate\Support\Str; return [ /* |-------------------------------------------------------------------------- | Default Session Driver |-------------------------------------------------------------------------- | | This option determines the default session driver that is utilized for | incoming requests. Laravel supports a variety of storage options to | persist session data. Database storage is a great default choice. | | Supported: "file", "cookie", "database", "apc", | "memcached", "redis", "dynamodb", "array" | */ 'driver' => env('SESSION_DRIVER', 'database'), /* |-------------------------------------------------------------------------- | Session Lifetime |-------------------------------------------------------------------------- | | Here you may specify the number of minutes that you wish the session | to be allowed to remain idle before it expires. If you want them | to expire immediately when the browser is closed then you may | indicate that via the expire_on_close configuration option. | */ 'lifetime' => env('SESSION_LIFETIME', 120), 'expire_on_close' => env('SESSION_EXPIRE_ON_CLOSE', false), /* |-------------------------------------------------------------------------- | Session Encryption |-------------------------------------------------------------------------- | | This option allows you to easily specify that all of your session data | should be encrypted before it's stored. All encryption is performed | automatically by Laravel and you may use the session like normal. | */ 'encrypt' => env('SESSION_ENCRYPT', false), /* |-------------------------------------------------------------------------- | Session File Location |-------------------------------------------------------------------------- | | When utilizing the "file" session driver, the session files are placed | on disk. The default storage location is defined here; however, you | are free to provide another location where they should be stored. | */ 'files' => storage_path('framework/sessions'), /* |-------------------------------------------------------------------------- | Session Database Connection |-------------------------------------------------------------------------- | | When using the "database" or "redis" session drivers, you may specify a | connection that should be used to manage these sessions. This should | correspond to a connection in your database configuration options. | */ 'connection' => env('SESSION_CONNECTION'), /* |-------------------------------------------------------------------------- | Session Database Table |-------------------------------------------------------------------------- | | When using the "database" session driver, you may specify the table to | be used to store sessions. Of course, a sensible default is defined | for you; however, you're welcome to change this to another table. | */ 'table' => env('SESSION_TABLE', 'sessions'), /* |-------------------------------------------------------------------------- | Session Cache Store |-------------------------------------------------------------------------- | | When using one of the framework's cache driven session backends, you may | define the cache store which should be used to store the session data | between requests. This must match one of your defined cache stores. | | Affects: "apc", "dynamodb", "memcached", "redis" | */ 'store' => env('SESSION_STORE'), /* |-------------------------------------------------------------------------- | Session Sweeping Lottery |-------------------------------------------------------------------------- | | Some session drivers must manually sweep their storage location to get | rid of old sessions from storage. Here are the chances that it will | happen on a given request. By default, the odds are 2 out of 100. | */ 'lottery' => [2, 100], /* |-------------------------------------------------------------------------- | Session Cookie Name |-------------------------------------------------------------------------- | | Here you may change the name of the session cookie that is created by | the framework. Typically, you should not need to change this value | since doing so does not grant a meaningful security improvement. | */ 'cookie' => env( 'SESSION_COOKIE', Str::slug(env('APP_NAME', 'laravel'), '_').'_session' ), /* |-------------------------------------------------------------------------- | Session Cookie Path |-------------------------------------------------------------------------- | | The session cookie path determines the path for which the cookie will | be regarded as available. Typically, this will be the root path of | your application, but you're free to change this when necessary. | */ 'path' => env('SESSION_PATH', '/'), /* |-------------------------------------------------------------------------- | Session Cookie Domain |-------------------------------------------------------------------------- | | This value determines the domain and subdomains the session cookie is | available to. By default, the cookie will be available to the root | domain and all subdomains. Typically, this shouldn't be changed. | */ 'domain' => env('SESSION_DOMAIN'), /* |-------------------------------------------------------------------------- | HTTPS Only Cookies |-------------------------------------------------------------------------- | | By setting this option to true, session cookies will only be sent back | to the server if the browser has a HTTPS connection. This will keep | the cookie from being sent to you when it can't be done securely. | */ 'secure' => env('SESSION_SECURE_COOKIE'), /* |-------------------------------------------------------------------------- | HTTP Access Only |-------------------------------------------------------------------------- | | Setting this value to true will prevent JavaScript from accessing the | value of the cookie and the cookie will only be accessible through | the HTTP protocol. It's unlikely you should disable this option. | */ 'http_only' => env('SESSION_HTTP_ONLY', true), /* |-------------------------------------------------------------------------- | Same-Site Cookies |-------------------------------------------------------------------------- | | This option determines how your cookies behave when cross-site requests | take place, and can be used to mitigate CSRF attacks. By default, we | will set this value to "lax" to permit secure cross-site requests. | | See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value | | Supported: "lax", "strict", "none", null | */ 'same_site' => env('SESSION_SAME_SITE', 'lax'), /* |-------------------------------------------------------------------------- | Partitioned Cookies |-------------------------------------------------------------------------- | | Setting this value to true will tie the cookie to the top-level site for | a cross-site context. Partitioned cookies are accepted by the browser | when flagged "secure" and the Same-Site attribute is set to "none". | */ 'partitioned' => env('SESSION_PARTITIONED_COOKIE', false), ]; framework/config/filesystems.php 0000644 00000004502 15060132306 0013063 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Default Filesystem Disk |-------------------------------------------------------------------------- | | Here you may specify the default filesystem disk that should be used | by the framework. The "local" disk, as well as a variety of cloud | based disks are available to your application for file storage. | */ 'default' => env('FILESYSTEM_DISK', 'local'), /* |-------------------------------------------------------------------------- | Filesystem Disks |-------------------------------------------------------------------------- | | Below you may configure as many filesystem disks as necessary, and you | may even configure multiple disks for the same driver. Examples for | most supported storage drivers are configured here for reference. | | Supported Drivers: "local", "ftp", "sftp", "s3" | */ 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), 'throw' => false, ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', 'throw' => false, ], 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), 'throw' => false, ], ], /* |-------------------------------------------------------------------------- | Symbolic Links |-------------------------------------------------------------------------- | | Here you may configure the symbolic links that will be created when the | `storage:link` Artisan command is executed. The array keys should be | the locations of the links and the values should be their targets. | */ 'links' => [ public_path('storage') => storage_path('app/public'), ], ]; framework/config/app.php 0000644 00000014766 15060132306 0011311 0 ustar 00 <?php use Illuminate\Support\Facades\Facade; use Illuminate\Support\ServiceProvider; return [ /* |-------------------------------------------------------------------------- | Application Name |-------------------------------------------------------------------------- | | This value is the name of your application, which will be used when the | framework needs to place the application's name in a notification or | other UI elements where an application name needs to be displayed. | */ 'name' => env('APP_NAME', 'Laravel'), /* |-------------------------------------------------------------------------- | Application Environment |-------------------------------------------------------------------------- | | This value determines the "environment" your application is currently | running in. This may determine how you prefer to configure various | services the application utilizes. Set this in your ".env" file. | */ 'env' => env('APP_ENV', 'production'), /* |-------------------------------------------------------------------------- | Application Debug Mode |-------------------------------------------------------------------------- | | When your application is in debug mode, detailed error messages with | stack traces will be shown on every error that occurs within your | application. If disabled, a simple generic error page is shown. | */ 'debug' => (bool) env('APP_DEBUG', false), /* |-------------------------------------------------------------------------- | Application URL |-------------------------------------------------------------------------- | | This URL is used by the console to properly generate URLs when using | the Artisan command line tool. You should set this to the root of | the application so that it's available within Artisan commands. | */ 'url' => env('APP_URL', 'http://localhost'), 'frontend_url' => env('FRONTEND_URL', 'http://localhost:3000'), 'asset_url' => env('ASSET_URL'), /* |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | | Here you may specify the default timezone for your application, which | will be used by the PHP date and date-time functions. The timezone | is set to "UTC" by default as it is suitable for most use cases. | */ 'timezone' => env('APP_TIMEZONE', 'UTC'), /* |-------------------------------------------------------------------------- | Application Locale Configuration |-------------------------------------------------------------------------- | | The application locale determines the default locale that will be used | by Laravel's translation / localization methods. This option can be | set to any locale for which you plan to have translation strings. | */ 'locale' => env('APP_LOCALE', 'en'), /* |-------------------------------------------------------------------------- | Application Fallback Locale |-------------------------------------------------------------------------- | | The fallback locale determines the locale to use when the default one | is not available. You may change the value to correspond to any of | the languages which are currently supported by your application. | */ 'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'), /* |-------------------------------------------------------------------------- | Faker Locale |-------------------------------------------------------------------------- | | This locale will be used by the Faker PHP library when generating fake | data for your database seeds. For example, this will be used to get | localized telephone numbers, street address information and more. | */ 'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'), /* |-------------------------------------------------------------------------- | Encryption Key |-------------------------------------------------------------------------- | | This key is utilized by Laravel's encryption services and should be set | to a random, 32 character string to ensure that all encrypted values | are secure. You should do this prior to deploying the application. | */ 'cipher' => 'AES-256-CBC', 'key' => env('APP_KEY'), 'previous_keys' => [ ...array_filter( explode(',', env('APP_PREVIOUS_KEYS', '')) ), ], /* |-------------------------------------------------------------------------- | Maintenance Mode Driver |-------------------------------------------------------------------------- | | These configuration options determine the driver used to determine and | manage Laravel's "maintenance mode" status. The "cache" driver will | allow maintenance mode to be controlled across multiple machines. | | Supported drivers: "file", "cache" | */ 'maintenance' => [ 'driver' => env('APP_MAINTENANCE_DRIVER', 'file'), 'store' => env('APP_MAINTENANCE_STORE', 'database'), ], /* |-------------------------------------------------------------------------- | Autoloaded Service Providers |-------------------------------------------------------------------------- | | The service providers listed here will be automatically loaded on any | requests to your application. You may add your own services to the | arrays below to provide additional features to this application. | */ 'providers' => ServiceProvider::defaultProviders()->merge([ // Package Service Providers... ])->merge([ // Application Service Providers... // App\Providers\AppServiceProvider::class, ])->merge([ // Added Service Providers (Do not remove this line)... ])->toArray(), /* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. You may add any additional class aliases which should | be loaded to the array. For speed, all aliases are lazy loaded. | */ 'aliases' => Facade::defaultAliases()->merge([ // 'Example' => App\Facades\Example::class, ])->toArray(), ]; framework/README.md 0000644 00000006654 15060132306 0010027 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 thousands of 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). ui/tests/AuthBackend/RegistersUsersTest.php 0000644 00000005537 15060132306 0015044 0 ustar 00 <?php namespace Laravel\Ui\Tests\AuthBackend; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Foundation\Auth\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Request; use Illuminate\Routing\Pipeline; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Illuminate\Testing\TestResponse; use Illuminate\Validation\ValidationException; use Orchestra\Testbench\Attributes\WithMigration; use Orchestra\Testbench\Factories\UserFactory; use Orchestra\Testbench\TestCase; use PHPUnit\Framework\Attributes\Test; #[WithMigration] class RegistersUsersTest extends TestCase { use RegistersUsers, RefreshDatabase; #[Test] public function it_can_register_a_user() { $request = Request::create('/register', 'POST', [ 'name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com', 'password' => 'secret-password', 'password_confirmation' => 'secret-password', ], [], [], [ 'HTTP_ACCEPT' => 'application/json', ]); $response = $this->handleRequestUsing($request, function ($request) { return $this->register($request); })->assertCreated(); $this->assertDatabaseHas('users', [ 'name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com', ]); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\Models\User */ protected function create(array $data) { $user = (new User())->forceFill([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); $user->save(); return $user; } /** * Handle Request using the following pipeline. * * @param \Illuminate\Http\Request $request * @param callable $callback * @return \Illuminate\Testing\TestResponse */ protected function handleRequestUsing(Request $request, callable $callback) { return new TestResponse( (new Pipeline($this->app)) ->send($request) ->through([ \Illuminate\Session\Middleware\StartSession::class, ]) ->then($callback) ); } } ui/tests/AuthBackend/ThrottleLoginsTest.php 0000644 00000003147 15060132306 0015027 0 ustar 00 <?php namespace Laravel\Ui\Tests\AuthBackend; use Illuminate\Foundation\Auth\ThrottlesLogins as ThrottlesLoginsTrait; use Orchestra\Testbench\TestCase; use Illuminate\Http\Request; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; class ThrottleLoginsTest extends TestCase { #[Test] #[DataProvider('emailProvider')] public function it_can_generate_throttle_key(string $email, string $expectedEmail): void { $throttle = $this->createMock(ThrottlesLogins::class); $throttle->method('username')->willReturn('email'); $reflection = new \ReflectionClass($throttle); $method = $reflection->getMethod('throttleKey'); $method->setAccessible(true); $request = $this->mock(Request::class); $request->expects('input')->with('email')->andReturn($email); $request->expects('ip')->andReturn('192.168.0.1'); $this->assertSame($expectedEmail . '|192.168.0.1', $method->invoke($throttle, $request)); } public static function emailProvider(): array { return [ 'lowercase special characters' => ['ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ', 'test@laravel.com'], 'uppercase special characters' => ['ⓉⒺⓈⓉ@ⓁⒶⓇⒶⓋⒺⓁ.ⒸⓄⓂ', 'test@laravel.com'], 'special character numbers' => ['test⑩⓸③@laravel.com', 'test1043@laravel.com'], 'default email' => ['test@laravel.com', 'test@laravel.com'], ]; } } class ThrottlesLogins { use ThrottlesLoginsTrait; public function username() { return 'email'; } } ui/tests/AuthBackend/AuthenticatesUsersTest.php 0000644 00000012506 15060132306 0015670 0 ustar 00 <?php namespace Laravel\Ui\Tests\AuthBackend; use Illuminate\Auth\Events\Attempting; use Illuminate\Auth\Events\Logout; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Request; use Illuminate\Routing\Pipeline; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Event; use Illuminate\Testing\TestResponse; use Illuminate\Validation\ValidationException; use Orchestra\Testbench\Attributes\WithMigration; use Orchestra\Testbench\Factories\UserFactory; use Orchestra\Testbench\TestCase; use PHPUnit\Framework\Attributes\Test; #[WithMigration] class AuthenticatesUsersTest extends TestCase { use AuthenticatesUsers, RefreshDatabase; protected function tearDown(): void { Auth::logout(); parent::tearDown(); } #[Test] public function it_can_authenticate_a_user() { Event::fake(); $user = UserFactory::new()->create(); $request = Request::create('/login', 'POST', [ 'email' => $user->email, 'password' => 'password', ], [], [], [ 'HTTP_ACCEPT' => 'application/json', ]); $response = $this->handleRequestUsing($request, function ($request) { return $this->login($request); })->assertStatus(204); Event::assertDispatched(function (Attempting $event) { return $event->remember === false; }); } #[Test] public function it_can_deauthenticate_a_user() { Event::fake(); $user = UserFactory::new()->create(); $this->actingAs($user); $request = Request::create('/logout', 'POST', [], [], [], [ 'HTTP_ACCEPT' => 'application/json', ]); $response = $this->handleRequestUsing( $request, fn ($request) => $this->logout($request) )->assertStatus(204); Event::assertDispatched(fn (Logout $event) => $user->is($event->user)); } #[Test] public function it_can_authenticate_a_user_with_remember_as_false() { Event::fake(); $user = UserFactory::new()->create(); $request = Request::create('/login', 'POST', [ 'email' => $user->email, 'password' => 'password', 'remember' => false, ], [], [], [ 'HTTP_ACCEPT' => 'application/json', ]); $response = $this->handleRequestUsing($request, function ($request) { return $this->login($request); })->assertStatus(204); Event::assertDispatched(function (Attempting $event) { return $event->remember === false; }); } #[Test] public function it_can_authenticate_a_user_with_remember_as_true() { Event::fake(); $user = UserFactory::new()->create(); $request = Request::create('/login', 'POST', [ 'email' => $user->email, 'password' => 'password', 'remember' => true, ], [], [], [ 'HTTP_ACCEPT' => 'application/json', ]); $response = $this->handleRequestUsing($request, function ($request) { return $this->login($request); })->assertStatus(204); Event::assertDispatched(function (Attempting $event) { return $event->remember === true; }); } #[Test] public function it_cant_authenticate_a_user_with_invalid_password() { $user = UserFactory::new()->create(); $request = Request::create('/login', 'POST', [ 'email' => $user->email, 'password' => 'invalid-password', ], [], [], [ 'HTTP_ACCEPT' => 'application/json', ]); $response = $this->handleRequestUsing($request, function ($request) { return $this->login($request); })->assertUnprocessable(); $this->assertInstanceOf(ValidationException::class, $response->exception); $this->assertSame([ 'email' => [ 'These credentials do not match our records.', ], ], $response->exception->errors()); } #[Test] public function it_cant_authenticate_unknown_credential() { $request = Request::create('/login', 'POST', [ 'email' => 'taylor@laravel.com', 'password' => 'password', ], [], [], [ 'HTTP_ACCEPT' => 'application/json', ]); $response = $this->handleRequestUsing($request, function ($request) { return $this->login($request); })->assertUnprocessable(); $this->assertInstanceOf(ValidationException::class, $response->exception); $this->assertSame([ 'email' => [ 'These credentials do not match our records.', ], ], $response->exception->errors()); } /** * Handle Request using the following pipeline. * * @param \Illuminate\Http\Request $request * @param callable $callback * @return \Illuminate\Testing\TestResponse */ protected function handleRequestUsing(Request $request, callable $callback) { return new TestResponse( (new Pipeline($this->app)) ->send($request) ->through([ \Illuminate\Session\Middleware\StartSession::class, ]) ->then($callback) ); } } ui/stubs/migrations/2014_10_12_100000_create_password_resets_table.php 0000644 00000001235 15060132306 0021174 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. * * @return void */ public function up() { Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token'); $table->timestamp('created_at')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('password_resets'); } }; ui/stubs/Auth/ForgotPasswordController.stub 0000644 00000001233 15060132306 0015143 0 ustar 00 <?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\SendsPasswordResetEmails; class ForgotPasswordController extends Controller { /* |-------------------------------------------------------------------------- | Password Reset Controller |-------------------------------------------------------------------------- | | This controller is responsible for handling password reset emails and | includes a trait which assists in sending these notifications from | your application to your users. Feel free to explore this trait. | */ use SendsPasswordResetEmails; } ui/stubs/Auth/ConfirmPasswordController.stub 0000644 00000001705 15060132306 0015304 0 ustar 00 <?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ConfirmsPasswords; class ConfirmPasswordController extends Controller { /* |-------------------------------------------------------------------------- | Confirm Password Controller |-------------------------------------------------------------------------- | | This controller is responsible for handling password confirmations and | uses a simple trait to include the behavior. You're free to explore | this trait and override any functions that require customization. | */ use ConfirmsPasswords; /** * Where to redirect users when the intended url fails. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } } ui/stubs/Auth/LoginController.stub 0000644 00000001742 15060132306 0013235 0 ustar 00 <?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); $this->middleware('auth')->only('logout'); } } ui/stubs/Auth/RegisterController.stub 0000644 00000003561 15060132306 0013752 0 ustar 00 <?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Models\User; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\Models\User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); } } ui/stubs/Auth/ResetPasswordController.stub 0000644 00000001421 15060132306 0014764 0 ustar 00 <?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ResetsPasswords; class ResetPasswordController extends Controller { /* |-------------------------------------------------------------------------- | Password Reset Controller |-------------------------------------------------------------------------- | | This controller is responsible for handling password reset requests | and uses a simple trait to include this behavior. You're free to | explore this trait and override any methods you wish to tweak. | */ use ResetsPasswords; /** * Where to redirect users after resetting their password. * * @var string */ protected $redirectTo = '/home'; } ui/stubs/Auth/VerificationController.stub 0000644 00000002057 15060132306 0014607 0 ustar 00 <?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\VerifiesEmails; class VerificationController extends Controller { /* |-------------------------------------------------------------------------- | Email Verification Controller |-------------------------------------------------------------------------- | | This controller is responsible for handling email verification for any | user that recently registered with the application. Emails may also | be re-sent if the user didn't receive the original email message. | */ use VerifiesEmails; /** * Where to redirect users after verification. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); $this->middleware('signed')->only('verify'); $this->middleware('throttle:6,1')->only('verify', 'resend'); } } ui/src/ControllersCommand.php 0000644 00000002462 15060132306 0012266 0 ustar 00 <?php namespace Laravel\Ui; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Finder\SplFileInfo; #[AsCommand(name: 'ui:controllers')] class ControllersCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'ui:controllers'; /** * The console command description. * * @var string */ protected $description = 'Scaffold the authentication controllers'; /** * Execute the console command. * * @return void */ public function handle() { if (! is_dir($directory = app_path('Http/Controllers/Auth'))) { mkdir($directory, 0755, true); } $filesystem = new Filesystem; collect($filesystem->allFiles(__DIR__.'/../stubs/Auth')) ->each(function (SplFileInfo $file) use ($filesystem) { $filesystem->copy( $file->getPathname(), app_path('Http/Controllers/Auth/'.Str::replaceLast('.stub', '.php', $file->getFilename())) ); }); $this->components->info('Authentication scaffolding generated successfully.'); } } ui/src/Presets/Bootstrap.php 0000644 00000003143 15060132306 0012060 0 ustar 00 <?php namespace Laravel\Ui\Presets; use Illuminate\Filesystem\Filesystem; class Bootstrap extends Preset { /** * Install the preset. * * @return void */ public static function install() { static::updatePackages(); static::updateViteConfiguration(); static::updateSass(); static::updateBootstrapping(); static::removeNodeModules(); } /** * Update the given package array. * * @param array $packages * @return array */ protected static function updatePackageArray(array $packages) { return [ 'bootstrap' => '^5.2.3', '@popperjs/core' => '^2.11.6', 'sass' => '^1.56.1', ] + $packages; } /** * Update the Vite configuration. * * @return void */ protected static function updateViteConfiguration() { copy(__DIR__.'/bootstrap-stubs/vite.config.js', base_path('vite.config.js')); } /** * Update the Sass files for the application. * * @return void */ protected static function updateSass() { (new Filesystem)->ensureDirectoryExists(resource_path('sass')); copy(__DIR__.'/bootstrap-stubs/_variables.scss', resource_path('sass/_variables.scss')); copy(__DIR__.'/bootstrap-stubs/app.scss', resource_path('sass/app.scss')); } /** * Update the bootstrapping files. * * @return void */ protected static function updateBootstrapping() { copy(__DIR__.'/bootstrap-stubs/bootstrap.js', resource_path('js/bootstrap.js')); } } ui/src/Presets/React.php 0000644 00000005040 15060132306 0011137 0 ustar 00 <?php namespace Laravel\Ui\Presets; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; class React extends Preset { /** * Install the preset. * * @return void */ public static function install() { static::ensureComponentDirectoryExists(); static::updatePackages(); static::updateViteConfiguration(); static::updateBootstrapping(); static::updateComponent(); static::addViteReactRefreshDirective(); static::removeNodeModules(); } /** * Update the given package array. * * @param array $packages * @return array */ protected static function updatePackageArray(array $packages) { return [ '@vitejs/plugin-react' => '^4.2.0', 'react' => '^18.2.0', 'react-dom' => '^18.2.0', ] + Arr::except($packages, [ '@vitejs/plugin-vue', 'vue' ]); } /** * Update the Vite configuration. * * @return void */ protected static function updateViteConfiguration() { copy(__DIR__.'/react-stubs/vite.config.js', base_path('vite.config.js')); } /** * Update the example component. * * @return void */ protected static function updateComponent() { (new Filesystem)->delete( resource_path('js/components/ExampleComponent.vue') ); copy( __DIR__.'/react-stubs/Example.jsx', resource_path('js/components/Example.jsx') ); } /** * Update the bootstrapping files. * * @return void */ protected static function updateBootstrapping() { copy(__DIR__.'/react-stubs/app.js', resource_path('js/app.js')); } /** * Add Vite's React Refresh Runtime * * @return void */ protected static function addViteReactRefreshDirective() { $view = static::getViewPath('layouts/app.blade.php'); if (! file_exists($view)) { return; } file_put_contents( $view, str_replace('@vite(', '@viteReactRefresh'.PHP_EOL.' @vite(', file_get_contents($view)) ); } /** * Get full view path relative to the application's configured view path. * * @param string $path * @return string */ protected static function getViewPath($path) { return implode(DIRECTORY_SEPARATOR, [ config('view.paths')[0] ?? resource_path('views'), $path, ]); } } ui/src/Presets/react-stubs/app.js 0000644 00000000764 15060132306 0012752 0 ustar 00 /** * First we will load all of this project's JavaScript dependencies which * includes React and other helpers. It's a great starting point while * building robust, powerful web applications using React + Laravel. */ import './bootstrap'; /** * Next, we will create a fresh React component instance and attach it to * the page. Then, you may begin adding components to this application * or customize the JavaScript scaffolding to fit your unique needs. */ import './components/Example'; ui/src/Presets/react-stubs/vite.config.js 0000644 00000000563 15060132306 0014402 0 ustar 00 import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [ laravel({ input: [ 'resources/sass/app.scss', 'resources/js/app.js', ], refresh: true, }), react(), ], }); ui/src/Presets/react-stubs/Example.jsx 0000644 00000001420 15060132306 0013743 0 ustar 00 import React from 'react'; import ReactDOM from 'react-dom/client'; function Example() { return ( <div className="container"> <div className="row justify-content-center"> <div className="col-md-8"> <div className="card"> <div className="card-header">Example Component</div> <div className="card-body">I'm an example component!</div> </div> </div> </div> </div> ); } export default Example; if (document.getElementById('example')) { const Index = ReactDOM.createRoot(document.getElementById("example")); Index.render( <React.StrictMode> <Example/> </React.StrictMode> ) } ui/src/Presets/bootstrap-stubs/_variables.scss 0000644 00000000212 15060132306 0015543 0 ustar 00 // Body $body-bg: #f8fafc; // Typography $font-family-sans-serif: 'Nunito', sans-serif; $font-size-base: 0.9rem; $line-height-base: 1.6; ui/src/Presets/bootstrap-stubs/vite.config.js 0000644 00000000470 15060132306 0015316 0 ustar 00 import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; export default defineConfig({ plugins: [ laravel({ input: [ 'resources/sass/app.scss', 'resources/js/app.js', ], refresh: true, }), ], }); ui/src/Presets/bootstrap-stubs/bootstrap.js 0000644 00000002323 15060132306 0015117 0 ustar 00 import 'bootstrap'; /** * We'll load the axios HTTP library which allows us to easily issue requests * to our Laravel back-end. This library automatically handles sending the * CSRF token as a header based on the value of the "XSRF" token cookie. */ import axios from 'axios'; window.axios = axios; window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; /** * Echo exposes an expressive API for subscribing to channels and listening * for events that are broadcast by Laravel. Echo and event broadcasting * allows your team to easily build robust real-time web applications. */ // import Echo from 'laravel-echo'; // import Pusher from 'pusher-js'; // window.Pusher = Pusher; // window.Echo = new Echo({ // broadcaster: 'pusher', // key: import.meta.env.VITE_PUSHER_APP_KEY, // cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1', // wsHost: import.meta.env.VITE_PUSHER_HOST ?? `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`, // wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80, // wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443, // forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https', // enabledTransports: ['ws', 'wss'], // }); ui/src/Presets/bootstrap-stubs/app.scss 0000644 00000000230 15060132306 0014214 0 ustar 00 // Fonts @import url('https://fonts.bunny.net/css?family=Nunito'); // Variables @import 'variables'; // Bootstrap @import 'bootstrap/scss/bootstrap'; ui/src/Presets/Preset.php 0000644 00000003137 15060132306 0011350 0 ustar 00 <?php namespace Laravel\Ui\Presets; use Illuminate\Filesystem\Filesystem; class Preset { /** * Ensure the component directories we need exist. * * @return void */ protected static function ensureComponentDirectoryExists() { $filesystem = new Filesystem; if (! $filesystem->isDirectory($directory = resource_path('js/components'))) { $filesystem->makeDirectory($directory, 0755, true); } } /** * Update the "package.json" file. * * @param bool $dev * @return void */ protected static function updatePackages($dev = true) { if (! file_exists(base_path('package.json'))) { return; } $configurationKey = $dev ? 'devDependencies' : 'dependencies'; $packages = json_decode(file_get_contents(base_path('package.json')), true); $packages[$configurationKey] = static::updatePackageArray( array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [], $configurationKey ); ksort($packages[$configurationKey]); file_put_contents( base_path('package.json'), json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL ); } /** * Remove the installed Node modules. * * @return void */ protected static function removeNodeModules() { tap(new Filesystem, function ($files) { $files->deleteDirectory(base_path('node_modules')); $files->delete(base_path('yarn.lock')); }); } } ui/src/Presets/vue-stubs/app.js 0000644 00000002601 15060132306 0012443 0 ustar 00 /** * First we will load all of this project's JavaScript dependencies which * includes Vue and other libraries. It is a great starting point when * building robust, powerful web applications using Vue and Laravel. */ import './bootstrap'; import { createApp } from 'vue'; /** * Next, we will create a fresh Vue application instance. You may then begin * registering components with the application instance so they are ready * to use in your application's views. An example is included for you. */ const app = createApp({}); import ExampleComponent from './components/ExampleComponent.vue'; app.component('example-component', ExampleComponent); /** * The following block of code may be used to automatically register your * Vue components. It will recursively scan this directory for the Vue * components and automatically register them with their "basename". * * Eg. ./components/ExampleComponent.vue -> <example-component></example-component> */ // Object.entries(import.meta.glob('./**/*.vue', { eager: true })).forEach(([path, definition]) => { // app.component(path.split('/').pop().replace(/\.\w+$/, ''), definition.default); // }); /** * Finally, we will attach the application instance to a HTML element with * an "id" attribute of "app". This element is included with the "auth" * scaffolding. Otherwise, you will need to add an element yourself. */ app.mount('#app'); ui/src/Presets/vue-stubs/vite.config.js 0000644 00000001206 15060132306 0014076 0 ustar 00 import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; import vue from '@vitejs/plugin-vue'; export default defineConfig({ plugins: [ laravel({ input: [ 'resources/sass/app.scss', 'resources/js/app.js', ], refresh: true, }), vue({ template: { transformAssetUrls: { base: null, includeAbsolute: false, }, }, }), ], resolve: { alias: { vue: 'vue/dist/vue.esm-bundler.js', }, }, }); ui/src/Presets/vue-stubs/ExampleComponent.vue 0000644 00000001050 15060132306 0015321 0 ustar 00 <template> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Example Component</div> <div class="card-body"> I'm an example component. </div> </div> </div> </div> </div> </template> <script> export default { mounted() { console.log('Component mounted.') } } </script> ui/src/Presets/Vue.php 0000644 00000003320 15060132306 0010637 0 ustar 00 <?php namespace Laravel\Ui\Presets; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; class Vue extends Preset { /** * Install the preset. * * @return void */ public static function install() { static::ensureComponentDirectoryExists(); static::updatePackages(); static::updateViteConfiguration(); static::updateBootstrapping(); static::updateComponent(); static::removeNodeModules(); } /** * Update the given package array. * * @param array $packages * @return array */ protected static function updatePackageArray(array $packages) { return [ '@vitejs/plugin-vue' => '^4.5.0', 'vue' => '^3.2.37', ] + Arr::except($packages, [ '@vitejs/plugin-react', 'react', 'react-dom', ]); } /** * Update the Vite configuration. * * @return void */ protected static function updateViteConfiguration() { copy(__DIR__.'/vue-stubs/vite.config.js', base_path('vite.config.js')); } /** * Update the example component. * * @return void */ protected static function updateComponent() { (new Filesystem)->delete( resource_path('js/components/Example.js') ); copy( __DIR__.'/vue-stubs/ExampleComponent.vue', resource_path('js/components/ExampleComponent.vue') ); } /** * Update the bootstrapping files. * * @return void */ protected static function updateBootstrapping() { copy(__DIR__.'/vue-stubs/app.js', resource_path('js/app.js')); } } ui/src/AuthRouteMethods.php 0000644 00000006657 15060132306 0011737 0 ustar 00 <?php namespace Laravel\Ui; class AuthRouteMethods { /** * Register the typical authentication routes for an application. * * @param array $options * @return callable */ public function auth() { return function ($options = []) { $namespace = class_exists($this->prependGroupNamespace('Auth\LoginController')) ? null : 'App\Http\Controllers'; $this->group(['namespace' => $namespace], function() use($options) { // Login Routes... if ($options['login'] ?? true) { $this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); } // Logout Routes... if ($options['logout'] ?? true) { $this->post('logout', 'Auth\LoginController@logout')->name('logout'); } // Registration Routes... if ($options['register'] ?? true) { $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); $this->post('register', 'Auth\RegisterController@register'); } // Password Reset Routes... if ($options['reset'] ?? true) { $this->resetPassword(); } // Password Confirmation Routes... if ($options['confirm'] ?? class_exists($this->prependGroupNamespace('Auth\ConfirmPasswordController'))) { $this->confirmPassword(); } // Email Verification Routes... if ($options['verify'] ?? false) { $this->emailVerification(); } }); }; } /** * Register the typical reset password routes for an application. * * @return callable */ public function resetPassword() { return function () { $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request'); $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset'); $this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update'); }; } /** * Register the typical confirm password routes for an application. * * @return callable */ public function confirmPassword() { return function () { $this->get('password/confirm', 'Auth\ConfirmPasswordController@showConfirmForm')->name('password.confirm'); $this->post('password/confirm', 'Auth\ConfirmPasswordController@confirm'); }; } /** * Register the typical email verification routes for an application. * * @return callable */ public function emailVerification() { return function () { $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice'); $this->get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify'); $this->post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend'); }; } } ui/src/Auth/stubs/routes.stub 0000644 00000000154 15060132306 0012225 0 ustar 00 Auth::routes(); Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); ui/src/Auth/stubs/controllers/Controller.stub 0000644 00000000464 15060132306 0015401 0 ustar 00 <?php namespace {{namespace}}Http\Controllers; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller as BaseController; class Controller extends BaseController { use AuthorizesRequests, ValidatesRequests; } ui/src/Auth/stubs/controllers/HomeController.stub 0000644 00000000734 15060132306 0016212 0 ustar 00 <?php namespace {{namespace}}Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('home'); } } ui/src/Auth/bootstrap-stubs/auth/register.stub 0000644 00000007054 15060132306 0015512 0 ustar 00 @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Register') }}</div> <div class="card-body"> <form method="POST" action="{{ route('register') }}"> @csrf <div class="row mb-3"> <label for="name" class="col-md-4 col-form-label text-md-end">{{ __('Name') }}</label> <div class="col-md-6"> <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus> @error('name') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email"> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password-confirm" class="col-md-4 col-form-label text-md-end">{{ __('Confirm Password') }}</label> <div class="col-md-6"> <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password"> </div> </div> <div class="row mb-0"> <div class="col-md-6 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Register') }} </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection ui/src/Auth/bootstrap-stubs/auth/verify.stub 0000644 00000002156 15060132306 0015170 0 ustar 00 @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Verify Your Email Address') }}</div> <div class="card-body"> @if (session('resent')) <div class="alert alert-success" role="alert"> {{ __('A fresh verification link has been sent to your email address.') }} </div> @endif {{ __('Before proceeding, please check your email for a verification link.') }} {{ __('If you did not receive the email') }}, <form class="d-inline" method="POST" action="{{ route('verification.resend') }}"> @csrf <button type="submit" class="btn btn-link p-0 m-0 align-baseline">{{ __('click here to request another') }}</button>. </form> </div> </div> </div> </div> </div> @endsection ui/src/Auth/bootstrap-stubs/auth/passwords/email.stub 0000644 00000003546 15060132306 0017004 0 ustar 00 @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Reset Password') }}</div> <div class="card-body"> @if (session('status')) <div class="alert alert-success" role="alert"> {{ session('status') }} </div> @endif <form method="POST" action="{{ route('password.email') }}"> @csrf <div class="row mb-3"> <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-0"> <div class="col-md-6 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Send Password Reset Link') }} </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection ui/src/Auth/bootstrap-stubs/auth/passwords/confirm.stub 0000644 00000004010 15060132306 0017335 0 ustar 00 @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Confirm Password') }}</div> <div class="card-body"> {{ __('Please confirm your password before continuing.') }} <form method="POST" action="{{ route('password.confirm') }}"> @csrf <div class="row mb-3"> <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Confirm Password') }} </button> @if (Route::has('password.request')) <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> @endif </div> </div> </form> </div> </div> </div> </div> </div> @endsection ui/src/Auth/bootstrap-stubs/auth/passwords/reset.stub 0000644 00000005650 15060132306 0017035 0 ustar 00 @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Reset Password') }}</div> <div class="card-body"> <form method="POST" action="{{ route('password.update') }}"> @csrf <input type="hidden" name="token" value="{{ $token }}"> <div class="row mb-3"> <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ $email ?? old('email') }}" required autocomplete="email" autofocus> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password-confirm" class="col-md-4 col-form-label text-md-end">{{ __('Confirm Password') }}</label> <div class="col-md-6"> <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password"> </div> </div> <div class="row mb-0"> <div class="col-md-6 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Reset Password') }} </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection ui/src/Auth/bootstrap-stubs/auth/login.stub 0000644 00000006420 15060132306 0014772 0 ustar 00 @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Login') }}</div> <div class="card-body"> <form method="POST" action="{{ route('login') }}"> @csrf <div class="row mb-3"> <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <div class="col-md-6 offset-md-4"> <div class="form-check"> <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}> <label class="form-check-label" for="remember"> {{ __('Remember Me') }} </label> </div> </div> </div> <div class="row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Login') }} </button> @if (Route::has('password.request')) <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> @endif </div> </div> </form> </div> </div> </div> </div> </div> @endsection ui/src/Auth/bootstrap-stubs/layouts/app.stub 0000644 00000006664 15060132306 0015213 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> <!-- Fonts --> <link rel="dns-prefetch" href="//fonts.bunny.net"> <link href="https://fonts.bunny.net/css?family=Nunito" rel="stylesheet"> <!-- Scripts --> @vite(['resources/sass/app.scss', 'resources/js/app.js']) </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-bs-toggle="collapse" data-bs-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 me-auto"> </ul> <!-- Right Side Of Navbar --> <ul class="navbar-nav ms-auto"> <!-- Authentication Links --> @guest @if (Route::has('login')) <li class="nav-item"> <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a> </li> @endif @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-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre> {{ Auth::user()->name }} </a> <div class="dropdown-menu dropdown-menu-end" 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" class="d-none"> @csrf </form> </div> </li> @endguest </ul> </div> </div> </nav> <main class="py-4"> @yield('content') </main> </div> </body> </html> ui/src/Auth/bootstrap-stubs/home.stub 0000644 00000001212 15060132306 0013643 0 ustar 00 @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Dashboard') }}</div> <div class="card-body"> @if (session('status')) <div class="alert alert-success" role="alert"> {{ session('status') }} </div> @endif {{ __('You are logged in!') }} </div> </div> </div> </div> </div> @endsection ui/src/UiCommand.php 0000644 00000004577 15060132306 0010346 0 ustar 00 <?php namespace Laravel\Ui; use Illuminate\Console\Command; use InvalidArgumentException; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'ui')] class UiCommand extends Command { /** * The console command signature. * * @var string */ protected $signature = 'ui { type : The preset type (bootstrap, vue, react) } { --auth : Install authentication UI scaffolding } { --option=* : Pass an option to the preset command }'; /** * The console command description. * * @var string */ protected $description = 'Swap the front-end scaffolding for the application'; /** * Execute the console command. * * @return void * * @throws \InvalidArgumentException */ public function handle() { if (static::hasMacro($this->argument('type'))) { return call_user_func(static::$macros[$this->argument('type')], $this); } if (! in_array($this->argument('type'), ['bootstrap', 'vue', 'react'])) { throw new InvalidArgumentException('Invalid preset.'); } if ($this->option('auth')) { $this->call('ui:auth'); } $this->{$this->argument('type')}(); } /** * Install the "bootstrap" preset. * * @return void */ protected function bootstrap() { Presets\Bootstrap::install(); $this->components->info('Bootstrap scaffolding installed successfully.'); $this->components->warn('Please run [npm install && npm run dev] to compile your fresh scaffolding.'); } /** * Install the "vue" preset. * * @return void */ protected function vue() { Presets\Bootstrap::install(); Presets\Vue::install(); $this->components->info('Vue scaffolding installed successfully.'); $this->components->warn('Please run [npm install && npm run dev] to compile your fresh scaffolding.'); } /** * Install the "react" preset. * * @return void */ protected function react() { Presets\Bootstrap::install(); Presets\React::install(); $this->components->info('React scaffolding installed successfully.'); $this->components->warn('Please run [npm install && npm run dev] to compile your fresh scaffolding.'); } } ui/src/UiServiceProvider.php 0000644 00000001236 15060132306 0012070 0 ustar 00 <?php namespace Laravel\Ui; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; class UiServiceProvider extends ServiceProvider { /** * Register the package services. * * @return void */ public function register() { if ($this->app->runningInConsole()) { $this->commands([ AuthCommand::class, ControllersCommand::class, UiCommand::class, ]); } } /** * Bootstrap any application services. * * @return void */ public function boot() { Route::mixin(new AuthRouteMethods); } } ui/src/AuthCommand.php 0000644 00000012437 15060132306 0010664 0 ustar 00 <?php namespace Laravel\Ui; use Illuminate\Console\Command; use InvalidArgumentException; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'ui:auth')] class AuthCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'ui:auth { type=bootstrap : The preset type (bootstrap) } {--views : Only scaffold the authentication views} {--force : Overwrite existing views by default}'; /** * The console command description. * * @var string */ protected $description = 'Scaffold basic login and registration views and routes'; /** * The views that need to be exported. * * @var array */ protected $views = [ 'auth/login.stub' => 'auth/login.blade.php', 'auth/passwords/confirm.stub' => 'auth/passwords/confirm.blade.php', 'auth/passwords/email.stub' => 'auth/passwords/email.blade.php', 'auth/passwords/reset.stub' => 'auth/passwords/reset.blade.php', 'auth/register.stub' => 'auth/register.blade.php', 'auth/verify.stub' => 'auth/verify.blade.php', 'home.stub' => 'home.blade.php', 'layouts/app.stub' => 'layouts/app.blade.php', ]; /** * Execute the console command. * * @return void * * @throws \InvalidArgumentException */ public function handle() { if (static::hasMacro($this->argument('type'))) { return call_user_func(static::$macros[$this->argument('type')], $this); } if (! in_array($this->argument('type'), ['bootstrap'])) { throw new InvalidArgumentException('Invalid preset.'); } $this->ensureDirectoriesExist(); $this->exportViews(); if (! $this->option('views')) { $this->exportBackend(); } $this->components->info('Authentication scaffolding generated successfully.'); } /** * Create the directories for the files. * * @return void */ protected function ensureDirectoriesExist() { if (! is_dir($directory = $this->getViewPath('layouts'))) { mkdir($directory, 0755, true); } if (! is_dir($directory = $this->getViewPath('auth/passwords'))) { mkdir($directory, 0755, true); } } /** * Export the authentication views. * * @return void */ protected function exportViews() { foreach ($this->views as $key => $value) { if (file_exists($view = $this->getViewPath($value)) && ! $this->option('force')) { if (! $this->components->confirm("The [$value] view already exists. Do you want to replace it?")) { continue; } } copy( __DIR__.'/Auth/'.$this->argument('type').'-stubs/'.$key, $view ); } } /** * Export the authentication backend. * * @return void */ protected function exportBackend() { $this->callSilent('ui:controllers'); $controller = app_path('Http/Controllers/HomeController.php'); if (file_exists($controller) && ! $this->option('force')) { if ($this->components->confirm("The [HomeController.php] file already exists. Do you want to replace it?", true)) { file_put_contents($controller, $this->compileStub('controllers/HomeController')); } } else { file_put_contents($controller, $this->compileStub('controllers/HomeController')); } $baseController = app_path('Http/Controllers/Controller.php'); if (file_exists($baseController) && ! $this->option('force')) { if ($this->components->confirm("The [Controller.php] file already exists. Do you want to replace it?", true)) { file_put_contents($baseController, $this->compileStub('controllers/Controller')); } } else { file_put_contents($baseController, $this->compileStub('controllers/Controller')); } if (! file_exists(database_path('migrations/0001_01_01_000000_create_users_table.php'))) { copy( __DIR__.'/../stubs/migrations/2014_10_12_100000_create_password_resets_table.php', base_path('database/migrations/2014_10_12_100000_create_password_resets_table.php') ); } file_put_contents( base_path('routes/web.php'), file_get_contents(__DIR__.'/Auth/stubs/routes.stub'), FILE_APPEND ); } /** * Compiles the given stub. * * @param string $stub * @return string */ protected function compileStub($stub) { return str_replace( '{{namespace}}', $this->laravel->getNamespace(), file_get_contents(__DIR__.'/Auth/stubs/'.$stub.'.stub') ); } /** * Get full view path relative to the application's configured view path. * * @param string $path * @return string */ protected function getViewPath($path) { return implode(DIRECTORY_SEPARATOR, [ config('view.paths')[0] ?? resource_path('views'), $path, ]); } } ui/LICENSE.md 0000644 00000002063 15060132306 0006562 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. ui/composer.json 0000644 00000002217 15060132306 0007701 0 ustar 00 { "name": "laravel/ui", "description": "Laravel UI utilities and presets.", "keywords": ["laravel", "ui"], "license": "MIT", "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.0", "illuminate/console": "^9.21|^10.0|^11.0", "illuminate/filesystem": "^9.21|^10.0|^11.0", "illuminate/support": "^9.21|^10.0|^11.0", "illuminate/validation": "^9.21|^10.0|^11.0", "symfony/console": "^6.0|^7.0" }, "require-dev": { "orchestra/testbench": "^7.35|^8.15|^9.0", "phpunit/phpunit": "^9.3|^10.4|^11.0" }, "autoload": { "psr-4": { "Laravel\\Ui\\": "src/", "Illuminate\\Foundation\\Auth\\": "auth-backend/" } }, "config": { "sort-packages": true }, "extra": { "branch-alias": { "dev-master": "4.x-dev" }, "laravel": { "providers": [ "Laravel\\Ui\\UiServiceProvider" ] } }, "minimum-stability": "dev", "prefer-stable": true } ui/auth-backend/RedirectsUsers.php 0000644 00000000617 15060132306 0013166 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; trait RedirectsUsers { /** * Get the post register / login redirect path. * * @return string */ public function redirectPath() { if (method_exists($this, 'redirectTo')) { return $this->redirectTo(); } return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; } } ui/auth-backend/ThrottlesLogins.php 0000644 00000006124 15060132306 0013363 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Auth\Events\Lockout; use Illuminate\Cache\RateLimiter; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Str; use Illuminate\Validation\ValidationException; trait ThrottlesLogins { /** * Determine if the user has too many failed login attempts. * * @param \Illuminate\Http\Request $request * @return bool */ protected function hasTooManyLoginAttempts(Request $request) { return $this->limiter()->tooManyAttempts( $this->throttleKey($request), $this->maxAttempts() ); } /** * Increment the login attempts for the user. * * @param \Illuminate\Http\Request $request * @return void */ protected function incrementLoginAttempts(Request $request) { $this->limiter()->hit( $this->throttleKey($request), $this->decayMinutes() * 60 ); } /** * Redirect the user after determining they are locked out. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Validation\ValidationException */ protected function sendLockoutResponse(Request $request) { $seconds = $this->limiter()->availableIn( $this->throttleKey($request) ); throw ValidationException::withMessages([ $this->username() => [trans('auth.throttle', [ 'seconds' => $seconds, 'minutes' => ceil($seconds / 60), ])], ])->status(Response::HTTP_TOO_MANY_REQUESTS); } /** * Clear the login locks for the given user credentials. * * @param \Illuminate\Http\Request $request * @return void */ protected function clearLoginAttempts(Request $request) { $this->limiter()->clear($this->throttleKey($request)); } /** * Fire an event when a lockout occurs. * * @param \Illuminate\Http\Request $request * @return void */ protected function fireLockoutEvent(Request $request) { event(new Lockout($request)); } /** * Get the throttle key for the given request. * * @param \Illuminate\Http\Request $request * @return string */ protected function throttleKey(Request $request) { return Str::transliterate(Str::lower($request->input($this->username())).'|'.$request->ip()); } /** * Get the rate limiter instance. * * @return \Illuminate\Cache\RateLimiter */ protected function limiter() { return app(RateLimiter::class); } /** * Get the maximum number of attempts to allow. * * @return int */ public function maxAttempts() { return property_exists($this, 'maxAttempts') ? $this->maxAttempts : 5; } /** * Get the number of minutes to throttle for. * * @return int */ public function decayMinutes() { return property_exists($this, 'decayMinutes') ? $this->decayMinutes : 1; } } ui/auth-backend/ResetsPasswords.php 0000644 00000012367 15060132306 0013400 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Auth\Events\PasswordReset; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Password; use Illuminate\Support\Str; use Illuminate\Validation\Rules; use Illuminate\Validation\ValidationException; trait ResetsPasswords { use RedirectsUsers; /** * Display the password reset view for the given token. * * If no token is present, display the link request form. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function showResetForm(Request $request) { $token = $request->route()->parameter('token'); return view('auth.passwords.reset')->with( ['token' => $token, 'email' => $request->email] ); } /** * Reset the given user's password. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ public function reset(Request $request) { $request->validate($this->rules(), $this->validationErrorMessages()); // Here we will attempt to reset the user's password. If it is successful we // will update the password on an actual user model and persist it to the // database. Otherwise we will parse the error and return the response. $response = $this->broker()->reset( $this->credentials($request), function ($user, $password) { $this->resetPassword($user, $password); } ); // If the password was successfully reset, we will redirect the user back to // the application's home authenticated view. If there is an error we can // redirect them back to where they came from with their error message. return $response == Password::PASSWORD_RESET ? $this->sendResetResponse($request, $response) : $this->sendResetFailedResponse($request, $response); } /** * Get the password reset validation rules. * * @return array */ protected function rules() { return [ 'token' => 'required', 'email' => 'required|email', 'password' => ['required', 'confirmed', Rules\Password::defaults()], ]; } /** * Get the password reset validation error messages. * * @return array */ protected function validationErrorMessages() { return []; } /** * Get the password reset credentials from the request. * * @param \Illuminate\Http\Request $request * @return array */ protected function credentials(Request $request) { return $request->only( 'email', 'password', 'password_confirmation', 'token' ); } /** * Reset the given user's password. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @param string $password * @return void */ protected function resetPassword($user, $password) { $this->setUserPassword($user, $password); $user->setRememberToken(Str::random(60)); $user->save(); event(new PasswordReset($user)); $this->guard()->login($user); } /** * Set the user's password. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @param string $password * @return void */ protected function setUserPassword($user, $password) { $user->password = Hash::make($password); } /** * Get the response for a successful password reset. * * @param \Illuminate\Http\Request $request * @param string $response * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ protected function sendResetResponse(Request $request, $response) { if ($request->wantsJson()) { return new JsonResponse(['message' => trans($response)], 200); } return redirect($this->redirectPath()) ->with('status', trans($response)); } /** * Get the response for a failed password reset. * * @param \Illuminate\Http\Request $request * @param string $response * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ protected function sendResetFailedResponse(Request $request, $response) { if ($request->wantsJson()) { throw ValidationException::withMessages([ 'email' => [trans($response)], ]); } return redirect()->back() ->withInput($request->only('email')) ->withErrors(['email' => trans($response)]); } /** * Get the broker to be used during password reset. * * @return \Illuminate\Contracts\Auth\PasswordBroker */ public function broker() { return Password::broker(); } /** * Get the guard to be used during password reset. * * @return \Illuminate\Contracts\Auth\StatefulGuard */ protected function guard() { return Auth::guard(); } } ui/auth-backend/RegistersUsers.php 0000644 00000003034 15060132306 0013205 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Auth\Events\Registered; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; trait RegistersUsers { use RedirectsUsers; /** * Show the application registration form. * * @return \Illuminate\View\View */ public function showRegistrationForm() { return view('auth.register'); } /** * Handle a registration request for the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ public function register(Request $request) { $this->validator($request->all())->validate(); event(new Registered($user = $this->create($request->all()))); $this->guard()->login($user); if ($response = $this->registered($request, $user)) { return $response; } return $request->wantsJson() ? new JsonResponse([], 201) : redirect($this->redirectPath()); } /** * Get the guard to be used during registration. * * @return \Illuminate\Contracts\Auth\StatefulGuard */ protected function guard() { return Auth::guard(); } /** * The user has been registered. * * @param \Illuminate\Http\Request $request * @param mixed $user * @return mixed */ protected function registered(Request $request, $user) { // } } ui/auth-backend/VerifiesEmails.php 0000644 00000005342 15060132306 0013127 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Events\Verified; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; trait VerifiesEmails { use RedirectsUsers; /** * Show the email verification notice. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View */ public function show(Request $request) { return $request->user()->hasVerifiedEmail() ? redirect($this->redirectPath()) : view('auth.verify'); } /** * Mark the authenticated user's email address as verified. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function verify(Request $request) { if (! hash_equals((string) $request->route('id'), (string) $request->user()->getKey())) { throw new AuthorizationException; } if (! hash_equals((string) $request->route('hash'), sha1($request->user()->getEmailForVerification()))) { throw new AuthorizationException; } if ($request->user()->hasVerifiedEmail()) { return $request->wantsJson() ? new JsonResponse([], 204) : redirect($this->redirectPath()); } if ($request->user()->markEmailAsVerified()) { event(new Verified($request->user())); } if ($response = $this->verified($request)) { return $response; } return $request->wantsJson() ? new JsonResponse([], 204) : redirect($this->redirectPath())->with('verified', true); } /** * The user has been verified. * * @param \Illuminate\Http\Request $request * @return mixed */ protected function verified(Request $request) { // } /** * Resend the email verification notification. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ public function resend(Request $request) { if ($request->user()->hasVerifiedEmail()) { return $request->wantsJson() ? new JsonResponse([], 204) : redirect($this->redirectPath()); } $request->user()->sendEmailVerificationNotification(); return $request->wantsJson() ? new JsonResponse([], 202) : back()->with('resent', true); } } ui/auth-backend/ConfirmsPasswords.php 0000644 00000003136 15060132306 0013705 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; trait ConfirmsPasswords { use RedirectsUsers; /** * Display the password confirmation view. * * @return \Illuminate\View\View */ public function showConfirmForm() { return view('auth.passwords.confirm'); } /** * Confirm the given user's password. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ public function confirm(Request $request) { $request->validate($this->rules(), $this->validationErrorMessages()); $this->resetPasswordConfirmationTimeout($request); return $request->wantsJson() ? new JsonResponse([], 204) : redirect()->intended($this->redirectPath()); } /** * Reset the password confirmation timeout. * * @param \Illuminate\Http\Request $request * @return void */ protected function resetPasswordConfirmationTimeout(Request $request) { $request->session()->put('auth.password_confirmed_at', time()); } /** * Get the password confirmation validation rules. * * @return array */ protected function rules() { return [ 'password' => 'required|current_password:web', ]; } /** * Get the password confirmation validation error messages. * * @return array */ protected function validationErrorMessages() { return []; } } ui/auth-backend/SendsPasswordResetEmails.php 0000644 00000006257 15060132306 0015163 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Password; use Illuminate\Validation\ValidationException; trait SendsPasswordResetEmails { /** * Display the form to request a password reset link. * * @return \Illuminate\View\View */ public function showLinkRequestForm() { return view('auth.passwords.email'); } /** * Send a reset link to the given user. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ public function sendResetLinkEmail(Request $request) { $this->validateEmail($request); // We will send the password reset link to this user. Once we have attempted // to send the link, we will examine the response then see the message we // need to show to the user. Finally, we'll send out a proper response. $response = $this->broker()->sendResetLink( $this->credentials($request) ); return $response == Password::RESET_LINK_SENT ? $this->sendResetLinkResponse($request, $response) : $this->sendResetLinkFailedResponse($request, $response); } /** * Validate the email for the given request. * * @param \Illuminate\Http\Request $request * @return void */ protected function validateEmail(Request $request) { $request->validate(['email' => 'required|email']); } /** * Get the needed authentication credentials from the request. * * @param \Illuminate\Http\Request $request * @return array */ protected function credentials(Request $request) { return $request->only('email'); } /** * Get the response for a successful password reset link. * * @param \Illuminate\Http\Request $request * @param string $response * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ protected function sendResetLinkResponse(Request $request, $response) { return $request->wantsJson() ? new JsonResponse(['message' => trans($response)], 200) : back()->with('status', trans($response)); } /** * Get the response for a failed password reset link. * * @param \Illuminate\Http\Request $request * @param string $response * @return \Illuminate\Http\RedirectResponse * * @throws \Illuminate\Validation\ValidationException */ protected function sendResetLinkFailedResponse(Request $request, $response) { if ($request->wantsJson()) { throw ValidationException::withMessages([ 'email' => [trans($response)], ]); } return back() ->withInput($request->only('email')) ->withErrors(['email' => trans($response)]); } /** * Get the broker to be used during password reset. * * @return \Illuminate\Contracts\Auth\PasswordBroker */ public function broker() { return Password::broker(); } } ui/auth-backend/AuthenticatesUsers.php 0000644 00000012577 15060132306 0014053 0 ustar 00 <?php namespace Illuminate\Foundation\Auth; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Validation\ValidationException; trait AuthenticatesUsers { use RedirectsUsers, ThrottlesLogins; /** * Show the application's login form. * * @return \Illuminate\View\View */ public function showLoginForm() { return view('auth.login'); } /** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse * * @throws \Illuminate\Validation\ValidationException */ public function login(Request $request) { $this->validateLogin($request); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } if ($this->attemptLogin($request)) { if ($request->hasSession()) { $request->session()->put('auth.password_confirmed_at', time()); } return $this->sendLoginResponse($request); } // If the login attempt was unsuccessful we will increment the number of attempts // to login and redirect the user back to the login form. Of course, when this // user surpasses their maximum number of attempts they will get locked out. $this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request); } /** * Validate the user login request. * * @param \Illuminate\Http\Request $request * @return void * * @throws \Illuminate\Validation\ValidationException */ protected function validateLogin(Request $request) { $request->validate([ $this->username() => 'required|string', 'password' => 'required|string', ]); } /** * Attempt to log the user into the application. * * @param \Illuminate\Http\Request $request * @return bool */ protected function attemptLogin(Request $request) { return $this->guard()->attempt( $this->credentials($request), $request->boolean('remember') ); } /** * Get the needed authorization credentials from the request. * * @param \Illuminate\Http\Request $request * @return array */ protected function credentials(Request $request) { return $request->only($this->username(), 'password'); } /** * Send the response after the user was authenticated. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ protected function sendLoginResponse(Request $request) { $request->session()->regenerate(); $this->clearLoginAttempts($request); if ($response = $this->authenticated($request, $this->guard()->user())) { return $response; } return $request->wantsJson() ? new JsonResponse([], 204) : redirect()->intended($this->redirectPath()); } /** * The user has been authenticated. * * @param \Illuminate\Http\Request $request * @param mixed $user * @return mixed */ protected function authenticated(Request $request, $user) { // } /** * Get the failed login response instance. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response * * @throws \Illuminate\Validation\ValidationException */ protected function sendFailedLoginResponse(Request $request) { throw ValidationException::withMessages([ $this->username() => [trans('auth.failed')], ]); } /** * Get the login username to be used by the controller. * * @return string */ public function username() { return 'email'; } /** * Log the user out of the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ public function logout(Request $request) { $this->guard()->logout(); $request->session()->invalidate(); $request->session()->regenerateToken(); if ($response = $this->loggedOut($request)) { return $response; } return $request->wantsJson() ? new JsonResponse([], 204) : redirect('/'); } /** * The user has logged out of the application. * * @param \Illuminate\Http\Request $request * @return mixed */ protected function loggedOut(Request $request) { // } /** * Get the guard to be used during authentication. * * @return \Illuminate\Contracts\Auth\StatefulGuard */ protected function guard() { return Auth::guard(); } } ui/README.md 0000644 00000022463 15060132306 0006443 0 ustar 00 # Laravel UI <a href="https://packagist.org/packages/laravel/ui"><img src="https://img.shields.io/packagist/dt/laravel/ui" alt="Total Downloads"></a> <a href="https://packagist.org/packages/laravel/ui"><img src="https://img.shields.io/packagist/v/laravel/ui" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/laravel/ui"><img src="https://img.shields.io/packagist/l/laravel/ui" alt="License"></a> ## Introduction While Laravel does not dictate which JavaScript or CSS pre-processors you use, it does provide a basic starting point using [Bootstrap](https://getbootstrap.com/), [React](https://reactjs.org/), and / or [Vue](https://vuejs.org/) that will be helpful for many applications. By default, Laravel uses [NPM](https://www.npmjs.org/) to install both of these frontend packages. > This legacy package is a very simple authentication scaffolding built on the Bootstrap CSS framework. While it continues to work with the latest version of Laravel, you should consider using [Laravel Breeze](https://github.com/laravel/breeze) for new projects. Or, for something more robust, consider [Laravel Jetstream](https://github.com/laravel/jetstream). ## Official Documentation ### Supported Versions Only the latest major version of Laravel UI receives bug fixes. The table below lists compatible Laravel versions: | Version | Laravel Version | |---- |----| | [1.x](https://github.com/laravel/ui/tree/1.x) | 5.8, 6.x | | [2.x](https://github.com/laravel/ui/tree/2.x) | 7.x | | [3.x](https://github.com/laravel/ui/tree/3.x) | 8.x, 9.x | | [4.x](https://github.com/laravel/ui/tree/4.x) | 9.x, 10.x, 11.x | ### Installation The Bootstrap and Vue scaffolding provided by Laravel is located in the `laravel/ui` Composer package, which may be installed using Composer: ```bash composer require laravel/ui ``` Once the `laravel/ui` package has been installed, you may install the frontend scaffolding using the `ui` Artisan command: ```bash // Generate basic scaffolding... php artisan ui bootstrap php artisan ui vue php artisan ui react // Generate login / registration scaffolding... php artisan ui bootstrap --auth php artisan ui vue --auth php artisan ui react --auth ``` #### CSS Laravel officially supports [Vite](https://laravel.com/docs/vite), a modern frontend build tool that provides an extremely fast development environment and bundles your code for production. Vite supports a variety of CSS preprocessor languages, including SASS and Less, which are extensions of plain CSS that add variables, mixins, and other powerful features that make working with CSS much more enjoyable. In this document, we will briefly discuss CSS compilation in general; however, you should consult the full [Vite documentation](https://laravel.com/docs/vite#working-with-stylesheets) for more information on compiling SASS or Less. #### JavaScript Laravel does not require you to use a specific JavaScript framework or library to build your applications. In fact, you don't have to use JavaScript at all. However, Laravel does include some basic scaffolding to make it easier to get started writing modern JavaScript using the [Vue](https://vuejs.org) library. Vue provides an expressive API for building robust JavaScript applications using components. As with CSS, we may use Vite to easily compile JavaScript components into a single, browser-ready JavaScript file. ### Writing CSS After installing the `laravel/ui` Composer package and [generating the frontend scaffolding](#introduction), Laravel's `package.json` file will include the `bootstrap` package to help you get started prototyping your application's frontend using Bootstrap. However, feel free to add or remove packages from the `package.json` file as needed for your own application. You are not required to use the Bootstrap framework to build your Laravel application - it is provided as a good starting point for those who choose to use it. Before compiling your CSS, install your project's frontend dependencies using the [Node package manager (NPM)](https://www.npmjs.org): ```bash npm install ``` Once the dependencies have been installed using `npm install`, you can compile your SASS files to plain CSS using [Vite](https://laravel.com/docs/vite#working-with-stylesheets). The `npm run dev` command will process the instructions in your `vite.config.js` file. Typically, your compiled CSS will be placed in the `public/build/assets` directory: ```bash npm run dev ``` The `vite.config.js` file included with Laravel's frontend scaffolding will compile the `resources/sass/app.scss` SASS file. This `app.scss` file imports a file of SASS variables and loads Bootstrap, which provides a good starting point for most applications. Feel free to customize the `app.scss` file however you wish or even use an entirely different pre-processor by [configuring Vite](https://laravel.com/docs/vite#working-with-stylesheets). ### Writing JavaScript All of the JavaScript dependencies required by your application can be found in the `package.json` file in the project's root directory. This file is similar to a `composer.json` file except it specifies JavaScript dependencies instead of PHP dependencies. You can install these dependencies using the [Node package manager (NPM)](https://www.npmjs.org): ```bash npm install ``` > By default, the Laravel `package.json` file includes a few packages such as `lodash` and `axios` to help you get started building your JavaScript application. Feel free to add or remove from the `package.json` file as needed for your own application. Once the packages are installed, you can use the `npm run dev` command to [compile your assets](https://laravel.com/docs/vite). Vite is a module bundler for modern JavaScript applications. When you run the `npm run dev` command, Vite will execute the instructions in your `vite.config.js` file: ```bash npm run dev ``` By default, the Laravel `vite.config.js` file compiles your SASS and the `resources/js/app.js` file. Within the `app.js` file you may register your Vue components or, if you prefer a different framework, configure your own JavaScript application. Your compiled JavaScript will typically be placed in the `public/build/assets` directory. > The `app.js` file will load the `resources/js/bootstrap.js` file which bootstraps and configures Vue, Axios, jQuery, and all other JavaScript dependencies. If you have additional JavaScript dependencies to configure, you may do so in this file. #### Writing Vue Components When using the `laravel/ui` package to scaffold your frontend, an `ExampleComponent.vue` Vue component will be placed in the `resources/js/components` directory. The `ExampleComponent.vue` file is an example of a [single file Vue component](https://vuejs.org/guide/single-file-components) which defines its JavaScript and HTML template in the same file. Single file components provide a very convenient approach to building JavaScript driven applications. The example component is registered in your `app.js` file: ```javascript import ExampleComponent from './components/ExampleComponent.vue'; Vue.component('example-component', ExampleComponent); ``` To use the component in your application, you may drop it into one of your HTML templates. For example, after running the `php artisan ui vue --auth` Artisan command to scaffold your application's authentication and registration screens, you could drop the component into the `home.blade.php` Blade template: ```blade @extends('layouts.app') @section('content') <example-component></example-component> @endsection ``` > Remember, you should run the `npm run dev` command each time you change a Vue component. Or, you may run the `npm run watch` command to monitor and automatically recompile your components each time they are modified. If you are interested in learning more about writing Vue components, you should read the [Vue documentation](https://vuejs.org/guide/), which provides a thorough, easy-to-read overview of the entire Vue framework. #### Using React If you prefer to use React to build your JavaScript application, Laravel makes it a cinch to swap the Vue scaffolding with React scaffolding: ```bash composer require laravel/ui // Generate basic scaffolding... php artisan ui react // Generate login / registration scaffolding... php artisan ui react --auth ```` ### Adding Presets Presets are "macroable", which allows you to add additional methods to the `UiCommand` class at runtime. For example, the following code adds a `nextjs` method to the `UiCommand` class. Typically, you should declare preset macros in a [service provider](https://laravel.com/docs/providers): ```php use Laravel\Ui\UiCommand; UiCommand::macro('nextjs', function (UiCommand $command) { // Scaffold your frontend... }); ``` Then, you may call the new preset via the `ui` command: ```bash php artisan ui nextjs ``` ## Contributing Thank you for considering contributing to UI! 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/ui/security/policy) on how to report security vulnerabilities. ## License Laravel UI is open-sourced software licensed under the [MIT license](LICENSE.md). tinker/src/ClassAliasAutoloader.php 0000644 00000007317 15060132306 0013403 0 ustar 00 <?php namespace Laravel\Tinker; use Illuminate\Support\Str; use Psy\Shell; class ClassAliasAutoloader { /** * The shell instance. * * @var \Psy\Shell */ protected $shell; /** * All of the discovered classes. * * @var array */ protected $classes = []; /** * Path to the vendor directory. * * @var string */ protected $vendorPath; /** * Explicitly included namespaces/classes. * * @var \Illuminate\Support\Collection */ protected $includedAliases; /** * Excluded namespaces/classes. * * @var \Illuminate\Support\Collection */ protected $excludedAliases; /** * Register a new alias loader instance. * * @param \Psy\Shell $shell * @param string $classMapPath * @param array $includedAliases * @param array $excludedAliases * @return static */ public static function register(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = []) { return tap(new static($shell, $classMapPath, $includedAliases, $excludedAliases), function ($loader) { spl_autoload_register([$loader, 'aliasClass']); }); } /** * Create a new alias loader instance. * * @param \Psy\Shell $shell * @param string $classMapPath * @param array $includedAliases * @param array $excludedAliases * @return void */ public function __construct(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = []) { $this->shell = $shell; $this->vendorPath = dirname(dirname($classMapPath)); $this->includedAliases = collect($includedAliases); $this->excludedAliases = collect($excludedAliases); $classes = require $classMapPath; foreach ($classes as $class => $path) { if (! $this->isAliasable($class, $path)) { continue; } $name = class_basename($class); if (! isset($this->classes[$name])) { $this->classes[$name] = $class; } } } /** * Find the closest class by name. * * @param string $class * @return void */ public function aliasClass($class) { if (Str::contains($class, '\\')) { return; } $fullName = $this->classes[$class] ?? false; if ($fullName) { $this->shell->writeStdout("[!] Aliasing '{$class}' to '{$fullName}' for this Tinker session.\n"); class_alias($fullName, $class); } } /** * Unregister the alias loader instance. * * @return void */ public function unregister() { spl_autoload_unregister([$this, 'aliasClass']); } /** * Handle the destruction of the instance. * * @return void */ public function __destruct() { $this->unregister(); } /** * Whether a class may be aliased. * * @param string $class * @param string $path */ public function isAliasable($class, $path) { if (! Str::contains($class, '\\')) { return false; } if (! $this->includedAliases->filter(function ($alias) use ($class) { return Str::startsWith($class, $alias); })->isEmpty()) { return true; } if (Str::startsWith($path, $this->vendorPath)) { return false; } if (! $this->excludedAliases->filter(function ($alias) use ($class) { return Str::startsWith($class, $alias); })->isEmpty()) { return false; } return true; } } tinker/src/TinkerCaster.php 0000644 00000007552 15060132306 0011743 0 ustar 00 <?php namespace Laravel\Tinker; use Exception; use Symfony\Component\VarDumper\Caster\Caster; class TinkerCaster { /** * Application methods to include in the presenter. * * @var array */ private static $appProperties = [ 'configurationIsCached', 'environment', 'environmentFile', 'isLocal', 'routesAreCached', 'runningUnitTests', 'version', 'path', 'basePath', 'configPath', 'databasePath', 'langPath', 'publicPath', 'storagePath', 'bootstrapPath', ]; /** * Get an array representing the properties of an application. * * @param \Illuminate\Foundation\Application $app * @return array */ public static function castApplication($app) { $results = []; foreach (self::$appProperties as $property) { try { $val = $app->$property(); if (! is_null($val)) { $results[Caster::PREFIX_VIRTUAL.$property] = $val; } } catch (Exception $e) { // } } return $results; } /** * Get an array representing the properties of a collection. * * @param \Illuminate\Support\Collection $collection * @return array */ public static function castCollection($collection) { return [ Caster::PREFIX_VIRTUAL.'all' => $collection->all(), ]; } /** * Get an array representing the properties of an html string. * * @param \Illuminate\Support\HtmlString $htmlString * @return array */ public static function castHtmlString($htmlString) { return [ Caster::PREFIX_VIRTUAL.'html' => $htmlString->toHtml(), ]; } /** * Get an array representing the properties of a fluent string. * * @param \Illuminate\Support\Stringable $stringable * @return array */ public static function castStringable($stringable) { return [ Caster::PREFIX_VIRTUAL.'value' => (string) $stringable, ]; } /** * Get an array representing the properties of a process result. * * @param \Illuminate\Process\ProcessResult $result * @return array */ public static function castProcessResult($result) { return [ Caster::PREFIX_VIRTUAL.'output' => $result->output(), Caster::PREFIX_VIRTUAL.'errorOutput' => $result->errorOutput(), Caster::PREFIX_VIRTUAL.'exitCode' => $result->exitCode(), Caster::PREFIX_VIRTUAL.'successful' => $result->successful(), ]; } /** * Get an array representing the properties of a model. * * @param \Illuminate\Database\Eloquent\Model $model * @return array */ public static function castModel($model) { $attributes = array_merge( $model->getAttributes(), $model->getRelations() ); $visible = array_flip( $model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden()) ); $hidden = array_flip($model->getHidden()); $appends = (function () { return array_combine($this->appends, $this->appends); // @phpstan-ignore-line })->bindTo($model, $model)(); foreach ($appends as $appended) { $attributes[$appended] = $model->{$appended}; } $results = []; foreach ($attributes as $key => $value) { $prefix = ''; if (isset($visible[$key])) { $prefix = Caster::PREFIX_VIRTUAL; } if (isset($hidden[$key])) { $prefix = Caster::PREFIX_PROTECTED; } $results[$prefix.$key] = $value; } return $results; } } tinker/src/TinkerServiceProvider.php 0000644 00000002513 15060132306 0013625 0 ustar 00 <?php namespace Laravel\Tinker; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Foundation\Application as LaravelApplication; use Illuminate\Support\ServiceProvider; use Laravel\Lumen\Application as LumenApplication; use Laravel\Tinker\Console\TinkerCommand; class TinkerServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Boot the service provider. * * @return void */ public function boot() { $source = realpath($raw = __DIR__.'/../config/tinker.php') ?: $raw; if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) { $this->publishes([$source => $this->app->configPath('tinker.php')]); } elseif ($this->app instanceof LumenApplication) { $this->app->configure('tinker'); } $this->mergeConfigFrom($source, 'tinker'); } /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('command.tinker', function () { return new TinkerCommand; }); $this->commands(['command.tinker']); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return ['command.tinker']; } } tinker/src/Console/TinkerCommand.php 0000644 00000010674 15060132306 0013501 0 ustar 00 <?php namespace Laravel\Tinker\Console; use Illuminate\Console\Command; use Illuminate\Support\Env; use Laravel\Tinker\ClassAliasAutoloader; use Psy\Configuration; use Psy\Shell; use Psy\VersionUpdater\Checker; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; class TinkerCommand extends Command { /** * Artisan commands to include in the tinker shell. * * @var array */ protected $commandWhitelist = [ 'clear-compiled', 'down', 'env', 'inspire', 'migrate', 'migrate:install', 'optimize', 'up', ]; /** * The console command name. * * @var string */ protected $name = 'tinker'; /** * The console command description. * * @var string */ protected $description = 'Interact with your application'; /** * Execute the console command. * * @return int */ public function handle() { $this->getApplication()->setCatchExceptions(false); $config = Configuration::fromInput($this->input); $config->setUpdateCheck(Checker::NEVER); $config->getPresenter()->addCasters( $this->getCasters() ); if ($this->option('execute')) { $config->setRawOutput(true); } $shell = new Shell($config); $shell->addCommands($this->getCommands()); $shell->setIncludes($this->argument('include')); $path = Env::get('COMPOSER_VENDOR_DIR', $this->getLaravel()->basePath().DIRECTORY_SEPARATOR.'vendor'); $path .= '/composer/autoload_classmap.php'; $config = $this->getLaravel()->make('config'); $loader = ClassAliasAutoloader::register( $shell, $path, $config->get('tinker.alias', []), $config->get('tinker.dont_alias', []) ); if ($code = $this->option('execute')) { try { $shell->setOutput($this->output); $shell->execute($code); } finally { $loader->unregister(); } return 0; } try { return $shell->run(); } finally { $loader->unregister(); } } /** * Get artisan commands to pass through to PsySH. * * @return array */ protected function getCommands() { $commands = []; foreach ($this->getApplication()->all() as $name => $command) { if (in_array($name, $this->commandWhitelist)) { $commands[] = $command; } } $config = $this->getLaravel()->make('config'); foreach ($config->get('tinker.commands', []) as $command) { $commands[] = $this->getApplication()->add( $this->getLaravel()->make($command) ); } return $commands; } /** * Get an array of Laravel tailored casters. * * @return array */ protected function getCasters() { $casters = [ 'Illuminate\Support\Collection' => 'Laravel\Tinker\TinkerCaster::castCollection', 'Illuminate\Support\HtmlString' => 'Laravel\Tinker\TinkerCaster::castHtmlString', 'Illuminate\Support\Stringable' => 'Laravel\Tinker\TinkerCaster::castStringable', ]; if (class_exists('Illuminate\Database\Eloquent\Model')) { $casters['Illuminate\Database\Eloquent\Model'] = 'Laravel\Tinker\TinkerCaster::castModel'; } if (class_exists('Illuminate\Process\ProcessResult')) { $casters['Illuminate\Process\ProcessResult'] = 'Laravel\Tinker\TinkerCaster::castProcessResult'; } if (class_exists('Illuminate\Foundation\Application')) { $casters['Illuminate\Foundation\Application'] = 'Laravel\Tinker\TinkerCaster::castApplication'; } $config = $this->getLaravel()->make('config'); return array_merge($casters, (array) $config->get('tinker.casters', [])); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ['include', InputArgument::IS_ARRAY, 'Include file(s) before starting tinker'], ]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['execute', null, InputOption::VALUE_OPTIONAL, 'Execute the given code using Tinker'], ]; } } tinker/LICENSE.md 0000644 00000002063 15060132306 0007441 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. tinker/composer.json 0000644 00000002713 15060132306 0010561 0 ustar 00 { "name": "laravel/tinker", "description": "Powerful REPL for the Laravel framework.", "keywords": ["tinker", "repl", "psysh", "laravel"], "license": "MIT", "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^7.2.5|^8.0", "illuminate/console": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", "illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", "psy/psysh": "^0.11.1|^0.12.0", "symfony/var-dumper": "^4.3.4|^5.0|^6.0|^7.0" }, "require-dev": { "mockery/mockery": "~1.3.3|^1.4.2", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^8.5.8|^9.3.3" }, "suggest": { "illuminate/database": "The Illuminate Database package (^6.0|^7.0|^8.0|^9.0|^10.0|^11.0)." }, "autoload": { "psr-4": { "Laravel\\Tinker\\": "src/" } }, "autoload-dev": { "psr-4": { "Laravel\\Tinker\\Tests\\": "tests/", "App\\": "tests/fixtures/app", "One\\Two\\": "tests/fixtures/vendor/one/two" } }, "extra": { "laravel": { "providers": [ "Laravel\\Tinker\\TinkerServiceProvider" ] } }, "config": { "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true } tinker/config/tinker.php 0000644 00000002740 15060132306 0011311 0 ustar 00 <?php return [ /* |-------------------------------------------------------------------------- | Console Commands |-------------------------------------------------------------------------- | | This option allows you to add additional Artisan commands that should | be available within the Tinker environment. Once the command is in | this array you may execute the command in Tinker using its name. | */ 'commands' => [ // App\Console\Commands\ExampleCommand::class, ], /* |-------------------------------------------------------------------------- | Auto Aliased Classes |-------------------------------------------------------------------------- | | Tinker will not automatically alias classes in your vendor namespaces | but you may explicitly allow a subset of classes to get aliased by | adding the names of each of those classes to the following list. | */ 'alias' => [ // ], /* |-------------------------------------------------------------------------- | Classes That Should Not Be Aliased |-------------------------------------------------------------------------- | | Typically, Tinker automatically aliases classes as you require them in | Tinker. However, you may wish to never alias certain classes, which | you may accomplish by listing the classes in the following array. | */ 'dont_alias' => [ 'App\Nova', ], ]; tinker/README.md 0000644 00000002756 15060132306 0007325 0 ustar 00 <p align="center"><img src="/art/logo.svg" alt="Logo Laravel Tinker"></p> <p align="center"> <a href="https://github.com/laravel/tinker/actions"><img src="https://github.com/laravel/tinker/workflows/tests/badge.svg" alt="Build Status"></a> <a href="https://packagist.org/packages/laravel/tinker"><img src="https://img.shields.io/packagist/dt/laravel/tinker" alt="Total Downloads"></a> <a href="https://packagist.org/packages/laravel/tinker"><img src="https://img.shields.io/packagist/v/laravel/tinker" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/laravel/tinker"><img src="https://img.shields.io/packagist/l/laravel/tinker" alt="License"></a> </p> ## Introduction Laravel Tinker is a powerful REPL for the Laravel framework. ## Official Documentation Documentation for Tinker can be found on the [Laravel website](https://laravel.com/docs/artisan#tinker). ## Contributing Thank you for considering contributing to Tinker! 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/tinker/security/policy) on how to report security vulnerabilities. ## License Laravel Tinker is open-sourced software licensed under the [MIT license](LICENSE.md). sanctum/testbench.yaml 0000644 00000000132 15060132306 0011051 0 ustar 00 providers: - Laravel\Sanctum\SanctumServiceProvider migrations: - database/migrations sanctum/src/PersonalAccessToken.php 0000644 00000004016 15060132306 0013421 0 ustar 00 <?php namespace Laravel\Sanctum; use Illuminate\Database\Eloquent\Model; use Laravel\Sanctum\Contracts\HasAbilities; class PersonalAccessToken extends Model implements HasAbilities { /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'abilities' => 'json', 'last_used_at' => 'datetime', 'expires_at' => 'datetime', ]; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'token', 'abilities', 'expires_at', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'token', ]; /** * Get the tokenable model that the access token belongs to. * * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function tokenable() { return $this->morphTo('tokenable'); } /** * Find the token instance matching the given token. * * @param string $token * @return static|null */ public static function findToken($token) { if (strpos($token, '|') === false) { return static::where('token', hash('sha256', $token))->first(); } [$id, $token] = explode('|', $token, 2); if ($instance = static::find($id)) { return hash_equals($instance->token, hash('sha256', $token)) ? $instance : null; } } /** * Determine if the token has a given ability. * * @param string $ability * @return bool */ public function can($ability) { return in_array('*', $this->abilities) || array_key_exists($ability, array_flip($this->abilities)); } /** * Determine if the token is missing a given ability. * * @param string $ability * @return bool */ public function cant($ability) { return ! $this->can($ability); } } sanctum/src/NewAccessToken.php 0000644 00000002427 15060132306 0012373 0 ustar 00 <?php namespace Laravel\Sanctum; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; class NewAccessToken implements Arrayable, Jsonable { /** * The access token instance. * * @var \Laravel\Sanctum\PersonalAccessToken */ public $accessToken; /** * The plain text version of the token. * * @var string */ public $plainTextToken; /** * Create a new access token result. * * @param \Laravel\Sanctum\PersonalAccessToken $accessToken * @param string $plainTextToken * @return void */ public function __construct(PersonalAccessToken $accessToken, string $plainTextToken) { $this->accessToken = $accessToken; $this->plainTextToken = $plainTextToken; } /** * Get the instance as an array. * * @return array */ public function toArray() { return [ 'accessToken' => $this->accessToken, 'plainTextToken' => $this->plainTextToken, ]; } /** * Convert the object to its JSON representation. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->toArray(), $options); } } sanctum/src/Guard.php 0000644 00000012216 15060132306 0010556 0 ustar 00 <?php namespace Laravel\Sanctum; use Illuminate\Contracts\Auth\Factory as AuthFactory; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Laravel\Sanctum\Events\TokenAuthenticated; class Guard { /** * The authentication factory implementation. * * @var \Illuminate\Contracts\Auth\Factory */ protected $auth; /** * The number of minutes tokens should be allowed to remain valid. * * @var int */ protected $expiration; /** * The provider name. * * @var string */ protected $provider; /** * Create a new guard instance. * * @param \Illuminate\Contracts\Auth\Factory $auth * @param int $expiration * @param string $provider * @return void */ public function __construct(AuthFactory $auth, $expiration = null, $provider = null) { $this->auth = $auth; $this->expiration = $expiration; $this->provider = $provider; } /** * Retrieve the authenticated user for the incoming request. * * @param \Illuminate\Http\Request $request * @return mixed */ public function __invoke(Request $request) { foreach (Arr::wrap(config('sanctum.guard', 'web')) as $guard) { if ($user = $this->auth->guard($guard)->user()) { return $this->supportsTokens($user) ? $user->withAccessToken(new TransientToken) : $user; } } if ($token = $this->getTokenFromRequest($request)) { $model = Sanctum::$personalAccessTokenModel; $accessToken = $model::findToken($token); if (! $this->isValidAccessToken($accessToken) || ! $this->supportsTokens($accessToken->tokenable)) { return; } $tokenable = $accessToken->tokenable->withAccessToken( $accessToken ); event(new TokenAuthenticated($accessToken)); if (method_exists($accessToken->getConnection(), 'hasModifiedRecords') && method_exists($accessToken->getConnection(), 'setRecordModificationState')) { tap($accessToken->getConnection()->hasModifiedRecords(), function ($hasModifiedRecords) use ($accessToken) { $accessToken->forceFill(['last_used_at' => now()])->save(); $accessToken->getConnection()->setRecordModificationState($hasModifiedRecords); }); } else { $accessToken->forceFill(['last_used_at' => now()])->save(); } return $tokenable; } } /** * Determine if the tokenable model supports API tokens. * * @param mixed $tokenable * @return bool */ protected function supportsTokens($tokenable = null) { return $tokenable && in_array(HasApiTokens::class, class_uses_recursive( get_class($tokenable) )); } /** * Get the token from the request. * * @param \Illuminate\Http\Request $request * @return string|null */ protected function getTokenFromRequest(Request $request) { if (is_callable(Sanctum::$accessTokenRetrievalCallback)) { return (string) (Sanctum::$accessTokenRetrievalCallback)($request); } $token = $request->bearerToken(); return $this->isValidBearerToken($token) ? $token : null; } /** * Determine if the bearer token is in the correct format. * * @param string|null $token * @return bool */ protected function isValidBearerToken(string $token = null) { if (! is_null($token) && str_contains($token, '|')) { $model = new Sanctum::$personalAccessTokenModel; if ($model->getKeyType() === 'int') { [$id, $token] = explode('|', $token, 2); return ctype_digit($id) && ! empty($token); } } return ! empty($token); } /** * Determine if the provided access token is valid. * * @param mixed $accessToken * @return bool */ protected function isValidAccessToken($accessToken): bool { if (! $accessToken) { return false; } $isValid = (! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration))) && (! $accessToken->expires_at || ! $accessToken->expires_at->isPast()) && $this->hasValidProvider($accessToken->tokenable); if (is_callable(Sanctum::$accessTokenAuthenticationCallback)) { $isValid = (bool) (Sanctum::$accessTokenAuthenticationCallback)($accessToken, $isValid); } return $isValid; } /** * Determine if the tokenable model matches the provider's model type. * * @param \Illuminate\Database\Eloquent\Model $tokenable * @return bool */ protected function hasValidProvider($tokenable) { if (is_null($this->provider)) { return true; } $model = config("auth.providers.{$this->provider}.model"); return $tokenable instanceof $model; } } sanctum/src/Events/TokenAuthenticated.php 0000644 00000000672 15060132306 0014546 0 ustar 00 <?php namespace Laravel\Sanctum\Events; class TokenAuthenticated { /** * The personal access token that was authenticated. * * @var \Laravel\Sanctum\PersonalAccessToken */ public $token; /** * Create a new event instance. * * @param \Laravel\Sanctum\PersonalAccessToken $token * @return void */ public function __construct($token) { $this->token = $token; } } sanctum/src/SanctumServiceProvider.php 0000644 00000006374 15060132306 0014172 0 ustar 00 <?php namespace Laravel\Sanctum; use Illuminate\Auth\RequestGuard; use Illuminate\Contracts\Http\Kernel; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Laravel\Sanctum\Console\Commands\PruneExpired; use Laravel\Sanctum\Http\Controllers\CsrfCookieController; use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful; class SanctumServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { config([ 'auth.guards.sanctum' => array_merge([ 'driver' => 'sanctum', 'provider' => null, ], config('auth.guards.sanctum', [])), ]); if (! app()->configurationIsCached()) { $this->mergeConfigFrom(__DIR__.'/../config/sanctum.php', 'sanctum'); } } /** * Bootstrap any application services. * * @return void */ public function boot() { if (app()->runningInConsole()) { $this->publishesMigrations([ __DIR__.'/../database/migrations' => database_path('migrations'), ], 'sanctum-migrations'); $this->publishes([ __DIR__.'/../config/sanctum.php' => config_path('sanctum.php'), ], 'sanctum-config'); $this->commands([ PruneExpired::class, ]); } $this->defineRoutes(); $this->configureGuard(); $this->configureMiddleware(); } /** * Define the Sanctum routes. * * @return void */ protected function defineRoutes() { if (app()->routesAreCached() || config('sanctum.routes') === false) { return; } Route::group(['prefix' => config('sanctum.prefix', 'sanctum')], function () { Route::get( '/csrf-cookie', CsrfCookieController::class.'@show' )->middleware('web')->name('sanctum.csrf-cookie'); }); } /** * Configure the Sanctum authentication guard. * * @return void */ protected function configureGuard() { Auth::resolved(function ($auth) { $auth->extend('sanctum', function ($app, $name, array $config) use ($auth) { return tap($this->createGuard($auth, $config), function ($guard) { app()->refresh('request', $guard, 'setRequest'); }); }); }); } /** * Register the guard. * * @param \Illuminate\Contracts\Auth\Factory $auth * @param array $config * @return RequestGuard */ protected function createGuard($auth, $config) { return new RequestGuard( new Guard($auth, config('sanctum.expiration'), $config['provider']), request(), $auth->createUserProvider($config['provider'] ?? null) ); } /** * Configure the Sanctum middleware and priority. * * @return void */ protected function configureMiddleware() { $kernel = app()->make(Kernel::class); $kernel->prependToMiddlewarePriority(EnsureFrontendRequestsAreStateful::class); } } sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php 0000644 00000005252 15060132306 0021347 0 ustar 00 <?php namespace Laravel\Sanctum\Http\Middleware; use Illuminate\Routing\Pipeline; use Illuminate\Support\Collection; use Illuminate\Support\Str; class EnsureFrontendRequestsAreStateful { /** * Handle the incoming requests. * * @param \Illuminate\Http\Request $request * @param callable $next * @return \Illuminate\Http\Response */ public function handle($request, $next) { $this->configureSecureCookieSessions(); return (new Pipeline(app()))->send($request)->through( static::fromFrontend($request) ? $this->frontendMiddleware() : [] )->then(function ($request) use ($next) { return $next($request); }); } /** * Configure secure cookie sessions. * * @return void */ protected function configureSecureCookieSessions() { config([ 'session.http_only' => true, 'session.same_site' => 'lax', ]); } /** * Get the middleware that should be applied to requests from the "frontend". * * @return array */ protected function frontendMiddleware() { $middleware = array_values(array_filter(array_unique([ config('sanctum.middleware.encrypt_cookies', \Illuminate\Cookie\Middleware\EncryptCookies::class), \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, config('sanctum.middleware.validate_csrf_token', config('sanctum.middleware.verify_csrf_token', \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class)), config('sanctum.middleware.authenticate_session'), ]))); array_unshift($middleware, function ($request, $next) { $request->attributes->set('sanctum', true); return $next($request); }); return $middleware; } /** * Determine if the given request is from the first-party application frontend. * * @param \Illuminate\Http\Request $request * @return bool */ public static function fromFrontend($request) { $domain = $request->headers->get('referer') ?: $request->headers->get('origin'); if (is_null($domain)) { return false; } $domain = Str::replaceFirst('https://', '', $domain); $domain = Str::replaceFirst('http://', '', $domain); $domain = Str::endsWith($domain, '/') ? $domain : "{$domain}/"; $stateful = array_filter(config('sanctum.stateful', [])); return Str::is(Collection::make($stateful)->map(function ($uri) { return trim($uri).'/*'; })->all(), $domain); } } sanctum/src/Http/Middleware/AuthenticateSession.php 0000644 00000006045 15060132306 0016475 0 ustar 00 <?php namespace Laravel\Sanctum\Http\Middleware; use Closure; use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\SessionGuard; use Illuminate\Contracts\Auth\Factory as AuthFactory; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Symfony\Component\HttpFoundation\Response; class AuthenticateSession { /** * 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 \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next * * @throws \Illuminate\Auth\AuthenticationException */ public function handle(Request $request, Closure $next): Response { if (! $request->hasSession() || ! $request->user()) { return $next($request); } $guards = Collection::make(Arr::wrap(config('sanctum.guard'))) ->mapWithKeys(fn ($guard) => [$guard => $this->auth->guard($guard)]) ->filter(fn ($guard) => $guard instanceof SessionGuard); $shouldLogout = $guards->filter( fn ($guard, $driver) => $request->session()->has('password_hash_'.$driver) )->filter( fn ($guard, $driver) => $request->session()->get('password_hash_'.$driver) !== $request->user()->getAuthPassword() ); if ($shouldLogout->isNotEmpty()) { $shouldLogout->each->logoutCurrentDevice(); $request->session()->flush(); throw new AuthenticationException('Unauthenticated.', [...$shouldLogout->keys()->all(), 'sanctum']); } return tap($next($request), function () use ($request, $guards) { if (! is_null($guard = $this->getFirstGuardWithUser($guards->keys()))) { $this->storePasswordHashInSession($request, $guard); } }); } /** * Get the first authentication guard that has a user. * * @param \Illuminate\Support\Collection $guards * @return string|null */ protected function getFirstGuardWithUser(Collection $guards) { return $guards->first(function ($guard) { $guardInstance = $this->auth->guard($guard); return method_exists($guardInstance, 'hasUser') && $guardInstance->hasUser(); }); } /** * Store the user's current password hash in the session. * * @param \Illuminate\Http\Request $request * @param string $guard * @return void */ protected function storePasswordHashInSession($request, string $guard) { $request->session()->put([ "password_hash_{$guard}" => $this->auth->guard($guard)->user()->getAuthPassword(), ]); } } sanctum/src/Http/Middleware/CheckForAnyAbility.php 0000644 00000001654 15060132306 0016166 0 ustar 00 <?php namespace Laravel\Sanctum\Http\Middleware; use Illuminate\Auth\AuthenticationException; use Laravel\Sanctum\Exceptions\MissingAbilityException; class CheckForAnyAbility { /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param mixed ...$abilities * @return \Illuminate\Http\Response * * @throws \Illuminate\Auth\AuthenticationException|\Laravel\Sanctum\Exceptions\MissingAbilityException */ public function handle($request, $next, ...$abilities) { if (! $request->user() || ! $request->user()->currentAccessToken()) { throw new AuthenticationException; } foreach ($abilities as $ability) { if ($request->user()->tokenCan($ability)) { return $next($request); } } throw new MissingAbilityException($abilities); } } sanctum/src/Http/Middleware/CheckAbilities.php 0000644 00000001650 15060132306 0015353 0 ustar 00 <?php namespace Laravel\Sanctum\Http\Middleware; use Illuminate\Auth\AuthenticationException; use Laravel\Sanctum\Exceptions\MissingAbilityException; class CheckAbilities { /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param mixed ...$abilities * @return \Illuminate\Http\Response * * @throws \Illuminate\Auth\AuthenticationException|\Laravel\Sanctum\Exceptions\MissingAbilityException */ public function handle($request, $next, ...$abilities) { if (! $request->user() || ! $request->user()->currentAccessToken()) { throw new AuthenticationException; } foreach ($abilities as $ability) { if (! $request->user()->tokenCan($ability)) { throw new MissingAbilityException($ability); } } return $next($request); } } sanctum/src/Http/Middleware/CheckForAnyScope.php 0000644 00000001530 15060132306 0015633 0 ustar 00 <?php namespace Laravel\Sanctum\Http\Middleware; use Laravel\Sanctum\Exceptions\MissingScopeException; /** * @deprecated * @see \Laravel\Sanctum\Http\Middleware\CheckForAnyAbility */ class CheckForAnyScope { /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param mixed ...$scopes * @return \Illuminate\Http\Response * * @throws \Illuminate\Auth\AuthenticationException|\Laravel\Sanctum\Exceptions\MissingScopeException */ public function handle($request, $next, ...$scopes) { try { return (new CheckForAnyAbility())->handle($request, $next, ...$scopes); } catch (\Laravel\Sanctum\Exceptions\MissingAbilityException $e) { throw new MissingScopeException($e->abilities()); } } } sanctum/src/Http/Middleware/CheckScopes.php 0000644 00000001513 15060132306 0014700 0 ustar 00 <?php namespace Laravel\Sanctum\Http\Middleware; use Laravel\Sanctum\Exceptions\MissingScopeException; /** * @deprecated * @see \Laravel\Sanctum\Http\Middleware\CheckAbilities */ class CheckScopes { /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param mixed ...$scopes * @return \Illuminate\Http\Response * * @throws \Illuminate\Auth\AuthenticationException|\Laravel\Sanctum\Exceptions\MissingScopeException */ public function handle($request, $next, ...$scopes) { try { return (new CheckAbilities())->handle($request, $next, ...$scopes); } catch (\Laravel\Sanctum\Exceptions\MissingAbilityException $e) { throw new MissingScopeException($e->abilities()); } } } sanctum/src/Http/Controllers/CsrfCookieController.php 0000644 00000001147 15060132306 0017035 0 ustar 00 <?php namespace Laravel\Sanctum\Http\Controllers; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; class CsrfCookieController { /** * Return an empty response simply to trigger the storage of the CSRF cookie in the browser. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse */ public function show(Request $request) { if ($request->expectsJson()) { return new JsonResponse(status: 204); } return new Response(status: 204); } } sanctum/src/TransientToken.php 0000644 00000001015 15060132306 0012457 0 ustar 00 <?php namespace Laravel\Sanctum; use Laravel\Sanctum\Contracts\HasAbilities; class TransientToken implements HasAbilities { /** * Determine if the token has a given ability. * * @param string $ability * @return bool */ public function can($ability) { return true; } /** * Determine if the token is missing a given ability. * * @param string $ability * @return bool */ public function cant($ability) { return false; } } sanctum/src/Sanctum.php 0000644 00000006076 15060132306 0011135 0 ustar 00 <?php namespace Laravel\Sanctum; use Mockery; class Sanctum { /** * The personal access client model class name. * * @var string */ public static $personalAccessTokenModel = 'Laravel\\Sanctum\\PersonalAccessToken'; /** * A callback that can get the token from the request. * * @var callable|null */ public static $accessTokenRetrievalCallback; /** * A callback that can add to the validation of the access token. * * @var callable|null */ public static $accessTokenAuthenticationCallback; /** * Get the current application URL from the "APP_URL" environment variable - with port. * * @return string */ public static function currentApplicationUrlWithPort() { $appUrl = config('app.url'); return $appUrl ? ','.parse_url($appUrl, PHP_URL_HOST).(parse_url($appUrl, PHP_URL_PORT) ? ':'.parse_url($appUrl, PHP_URL_PORT) : '') : ''; } /** * Set the current user for the application with the given abilities. * * @param \Illuminate\Contracts\Auth\Authenticatable|\Laravel\Sanctum\HasApiTokens $user * @param array $abilities * @param string $guard * @return \Illuminate\Contracts\Auth\Authenticatable */ public static function actingAs($user, $abilities = [], $guard = 'sanctum') { $token = Mockery::mock(self::personalAccessTokenModel())->shouldIgnoreMissing(false); if (in_array('*', $abilities)) { $token->shouldReceive('can')->withAnyArgs()->andReturn(true); } else { foreach ($abilities as $ability) { $token->shouldReceive('can')->with($ability)->andReturn(true); } } $user->withAccessToken($token); if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) { $user->wasRecentlyCreated = false; } app('auth')->guard($guard)->setUser($user); app('auth')->shouldUse($guard); return $user; } /** * Set the personal access token model name. * * @param string $model * @return void */ public static function usePersonalAccessTokenModel($model) { static::$personalAccessTokenModel = $model; } /** * Specify a callback that should be used to fetch the access token from the request. * * @param callable|null $callback * @return void */ public static function getAccessTokenFromRequestUsing(?callable $callback) { static::$accessTokenRetrievalCallback = $callback; } /** * Specify a callback that should be used to authenticate access tokens. * * @param callable $callback * @return void */ public static function authenticateAccessTokensUsing(callable $callback) { static::$accessTokenAuthenticationCallback = $callback; } /** * Get the token model class name. * * @return string */ public static function personalAccessTokenModel() { return static::$personalAccessTokenModel; } } sanctum/src/HasApiTokens.php 0000644 00000004512 15060132306 0012045 0 ustar 00 <?php namespace Laravel\Sanctum; use DateTimeInterface; use Illuminate\Support\Str; trait HasApiTokens { /** * The access token the user is using for the current request. * * @var \Laravel\Sanctum\Contracts\HasAbilities */ protected $accessToken; /** * Get the access tokens that belong to model. * * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function tokens() { return $this->morphMany(Sanctum::$personalAccessTokenModel, 'tokenable'); } /** * Determine if the current API token has a given scope. * * @param string $ability * @return bool */ public function tokenCan(string $ability) { return $this->accessToken && $this->accessToken->can($ability); } /** * Create a new personal access token for the user. * * @param string $name * @param array $abilities * @param \DateTimeInterface|null $expiresAt * @return \Laravel\Sanctum\NewAccessToken */ public function createToken(string $name, array $abilities = ['*'], DateTimeInterface $expiresAt = null) { $plainTextToken = $this->generateTokenString(); $token = $this->tokens()->create([ 'name' => $name, 'token' => hash('sha256', $plainTextToken), 'abilities' => $abilities, 'expires_at' => $expiresAt, ]); return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken); } /** * Generate the token string. * * @return string */ public function generateTokenString() { return sprintf( '%s%s%s', config('sanctum.token_prefix', ''), $tokenEntropy = Str::random(40), hash('crc32b', $tokenEntropy) ); } /** * Get the access token currently associated with the user. * * @return \Laravel\Sanctum\Contracts\HasAbilities */ public function currentAccessToken() { return $this->accessToken; } /** * Set the current access token for the user. * * @param \Laravel\Sanctum\Contracts\HasAbilities $accessToken * @return $this */ public function withAccessToken($accessToken) { $this->accessToken = $accessToken; return $this; } } sanctum/src/Console/Commands/PruneExpired.php 0000644 00000003117 15060132306 0015271 0 ustar 00 <?php namespace Laravel\Sanctum\Console\Commands; use Illuminate\Console\Command; use Laravel\Sanctum\Sanctum; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'sanctum:prune-expired')] class PruneExpired extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'sanctum:prune-expired {--hours=24 : The number of hours to retain expired Sanctum tokens}'; /** * The console command description. * * @var string */ protected $description = 'Prune tokens expired for more than specified number of hours'; /** * Execute the console command. * * @return int */ public function handle() { $model = Sanctum::$personalAccessTokenModel; $hours = $this->option('hours'); $this->components->task( 'Pruning tokens with expired expires_at timestamps', fn () => $model::where('expires_at', '<', now()->subHours($hours))->delete() ); if ($expiration = config('sanctum.expiration')) { $this->components->task( 'Pruning tokens with expired expiration value based on configuration file', fn () => $model::where('created_at', '<', now()->subMinutes($expiration + ($hours * 60)))->delete() ); } else { $this->components->warn('Expiration value not specified in configuration file.'); } $this->components->info("Tokens expired for more than [$hours hours] pruned successfully."); return 0; } } sanctum/src/Contracts/HasAbilities.php 0000644 00000000623 15060132306 0014014 0 ustar 00 <?php namespace Laravel\Sanctum\Contracts; interface HasAbilities { /** * Determine if the token has a given ability. * * @param string $ability * @return bool */ public function can($ability); /** * Determine if the token is missing a given ability. * * @param string $ability * @return bool */ public function cant($ability); } sanctum/src/Contracts/HasApiTokens.php 0000644 00000002321 15060132306 0014001 0 ustar 00 <?php namespace Laravel\Sanctum\Contracts; use DateTimeInterface; interface HasApiTokens { /** * Get the access tokens that belong to model. * * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function tokens(); /** * Determine if the current API token has a given scope. * * @param string $ability * @return bool */ public function tokenCan(string $ability); /** * Create a new personal access token for the user. * * @param string $name * @param array $abilities * @param \DateTimeInterface|null $expiresAt * @return \Laravel\Sanctum\NewAccessToken */ public function createToken(string $name, array $abilities = ['*'], DateTimeInterface $expiresAt = null); /** * Get the access token currently associated with the user. * * @return \Laravel\Sanctum\Contracts\HasAbilities */ public function currentAccessToken(); /** * Set the current access token for the user. * * @param \Laravel\Sanctum\Contracts\HasAbilities $accessToken * @return \Laravel\Sanctum\Contracts\HasApiTokens */ public function withAccessToken($accessToken); } sanctum/src/Exceptions/MissingScopeException.php 0000644 00000001571 15060132306 0016121 0 ustar 00 <?php namespace Laravel\Sanctum\Exceptions; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Support\Arr; /** * @deprecated * @see \Laravel\Sanctum\Exceptions\MissingAbilityException */ class MissingScopeException extends AuthorizationException { /** * The scopes that the user did not have. * * @var array */ protected $scopes; /** * Create a new missing scope exception. * * @param array|string $scopes * @param string $message * @return void */ public function __construct($scopes = [], $message = 'Invalid scope(s) provided.') { parent::__construct($message); $this->scopes = Arr::wrap($scopes); } /** * Get the scopes that the user did not have. * * @return array */ public function scopes() { return $this->scopes; } } sanctum/src/Exceptions/MissingAbilityException.php 0000644 00000001502 15060132306 0016437 0 ustar 00 <?php namespace Laravel\Sanctum\Exceptions; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Support\Arr; class MissingAbilityException extends AuthorizationException { /** * The abilities that the user did not have. * * @var array */ protected $abilities; /** * Create a new missing scope exception. * * @param array|string $abilities * @param string $message * @return void */ public function __construct($abilities = [], $message = 'Invalid ability provided.') { parent::__construct($message); $this->abilities = Arr::wrap($abilities); } /** * Get the abilities that the user did not have. * * @return array */ public function abilities() { return $this->abilities; } } sanctum/LICENSE.md 0000644 00000002063 15060132306 0007617 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. sanctum/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php 0000644 00000001530 15060132306 0024144 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('personal_access_tokens', function (Blueprint $table) { $table->id(); $table->morphs('tokenable'); $table->string('name'); $table->string('token', 64)->unique(); $table->text('abilities')->nullable(); $table->timestamp('last_used_at')->nullable(); $table->timestamp('expires_at')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('personal_access_tokens'); } }; sanctum/UPGRADE.md 0000644 00000001743 15060132306 0007630 0 ustar 00 # Upgrade Guide ## Upgrading To 4.0 From 3.x ### Minimum PHP Version PHP 8.2 is now the minimum required version. ### Minimum Laravel Version Laravel 11.0 is now the minimum required version. ### Migration Changes Sanctum 4.0 no longer automatically loads migrations from its own migrations directory. Instead, you should run the following command to publish Sanctum's migrations to your application: ```bash php artisan vendor:publish --tag=sanctum-migrations ``` ### Configuration Changes In your application's `config/sanctum.php` configuration file, you should update the references to the `authenticate_session`, `encrypt_cookies`, and `validate_csrf_token` middleware to the following: ```php 'middleware' => [ 'authenticate_session' => Laravel\Sanctum\Http\Middleware\AuthenticateSession::class, 'encrypt_cookies' => Illuminate\Cookie\Middleware\EncryptCookies::class, 'validate_csrf_token' => Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class, ], ``` sanctum/composer.json 0000644 00000002755 15060132306 0010745 0 ustar 00 { "name": "laravel/sanctum", "description": "Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.", "keywords": ["laravel", "sanctum", "auth"], "license": "MIT", "support": { "issues": "https://github.com/laravel/sanctum/issues", "source": "https://github.com/laravel/sanctum" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.2", "ext-json": "*", "illuminate/console": "^11.0", "illuminate/contracts": "^11.0", "illuminate/database": "^11.0", "illuminate/support": "^11.0", "symfony/console": "^7.0" }, "require-dev": { "mockery/mockery": "^1.6", "orchestra/testbench": "^9.0", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^10.5" }, "autoload": { "psr-4": { "Laravel\\Sanctum\\": "src/" } }, "autoload-dev": { "psr-4": { "Laravel\\Sanctum\\Tests\\": "tests/", "Workbench\\App\\": "workbench/app/", "Workbench\\Database\\Factories\\": "workbench/database/factories/" } }, "extra": { "laravel": { "providers": [ "Laravel\\Sanctum\\SanctumServiceProvider" ] } }, "config": { "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true } sanctum/config/sanctum.php 0000644 00000005662 15060132306 0011653 0 ustar 00 <?php use Laravel\Sanctum\Sanctum; return [ /* |-------------------------------------------------------------------------- | Stateful Domains |-------------------------------------------------------------------------- | | Requests from the following domains / hosts will receive stateful API | authentication cookies. Typically, these should include your local | and production domains which access your API via a frontend SPA. | */ 'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf( '%s%s', 'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1', Sanctum::currentApplicationUrlWithPort() ))), /* |-------------------------------------------------------------------------- | Sanctum Guards |-------------------------------------------------------------------------- | | This array contains the authentication guards that will be checked when | Sanctum is trying to authenticate a request. If none of these guards | are able to authenticate the request, Sanctum will use the bearer | token that's present on an incoming request for authentication. | */ 'guard' => ['web'], /* |-------------------------------------------------------------------------- | Expiration Minutes |-------------------------------------------------------------------------- | | This value controls the number of minutes until an issued token will be | considered expired. This will override any values set in the token's | "expires_at" attribute, but first-party sessions are not affected. | */ 'expiration' => null, /* |-------------------------------------------------------------------------- | Token Prefix |-------------------------------------------------------------------------- | | Sanctum can prefix new tokens in order to take advantage of numerous | security scanning initiatives maintained by open source platforms | that notify developers if they commit tokens into repositories. | | See: https://docs.github.com/en/code-security/secret-scanning/about-secret-scanning | */ 'token_prefix' => env('SANCTUM_TOKEN_PREFIX', ''), /* |-------------------------------------------------------------------------- | Sanctum Middleware |-------------------------------------------------------------------------- | | When authenticating your first-party SPA with Sanctum you may need to | customize some of the middleware Sanctum uses while processing the | request. You may change the middleware listed below as required. | */ 'middleware' => [ 'authenticate_session' => Laravel\Sanctum\Http\Middleware\AuthenticateSession::class, 'encrypt_cookies' => Illuminate\Cookie\Middleware\EncryptCookies::class, 'validate_csrf_token' => Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class, ], ]; sanctum/README.md 0000644 00000003020 15060132306 0007464 0 ustar 00 <p align="center"><img src="/art/logo.svg" alt="Logo Laravel Sanctum"></p> <p align="center"> <a href="https://github.com/laravel/sanctum/actions"><img src="https://github.com/laravel/sanctum/workflows/tests/badge.svg" alt="Build Status"></a> <a href="https://packagist.org/packages/laravel/sanctum"><img src="https://img.shields.io/packagist/dt/laravel/sanctum" alt="Total Downloads"></a> <a href="https://packagist.org/packages/laravel/sanctum"><img src="https://img.shields.io/packagist/v/laravel/sanctum" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/laravel/sanctum"><img src="https://img.shields.io/packagist/l/laravel/sanctum" alt="License"></a> </p> ## Introduction Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs. ## Official Documentation Documentation for Sanctum can be found on the [Laravel website](https://laravel.com/docs/sanctum). ## Contributing Thank you for considering contributing to Sanctum! 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/sanctum/security/policy) on how to report security vulnerabilities. ## License Laravel Sanctum is open-sourced software licensed under the [MIT license](LICENSE.md). prompts/src/FormStep.php 0000644 00000002511 15060132306 0011302 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; class FormStep { protected readonly Closure $condition; public function __construct( protected readonly Closure $step, bool|Closure $condition, public readonly ?string $name, protected readonly bool $ignoreWhenReverting, ) { $this->condition = is_bool($condition) ? fn () => $condition : $condition; } /** * Execute this step. * * @param array<mixed> $responses */ public function run(array $responses, mixed $previousResponse): mixed { if (! $this->shouldRun($responses)) { return null; } return ($this->step)($responses, $previousResponse); } /** * Whether the step should run based on the given condition. * * @param array<mixed> $responses */ protected function shouldRun(array $responses): bool { return ($this->condition)($responses); } /** * Whether this step should be skipped over when a subsequent step is reverted. * * @param array<mixed> $responses */ public function shouldIgnoreWhenReverting(array $responses): bool { if (! $this->shouldRun($responses)) { return true; } return $this->ignoreWhenReverting; } } prompts/src/TextareaPrompt.php 0000644 00000015435 15060132306 0012533 0 ustar 00 <?php namespace Laravel\Prompts; class TextareaPrompt extends Prompt { use Concerns\Scrolling; use Concerns\Truncation; use Concerns\TypedValue; /** * The width of the textarea. */ public int $width = 60; /** * Create a new TextareaPrompt instance. */ public function __construct( public string $label, public string $placeholder = '', public string $default = '', public bool|string $required = false, public mixed $validate = null, public string $hint = '', int $rows = 5, ) { $this->scroll = $rows; $this->initializeScrolling(); $this->trackTypedValue( default: $default, submit: false, allowNewLine: true, ); $this->on('key', function ($key) { if ($key[0] === "\e") { match ($key) { Key::UP, Key::UP_ARROW, Key::CTRL_P => $this->handleUpKey(), Key::DOWN, Key::DOWN_ARROW, Key::CTRL_N => $this->handleDownKey(), default => null, }; return; } // Keys may be buffered. foreach (mb_str_split($key) as $key) { if ($key === Key::CTRL_D) { $this->submit(); return; } } }); } /** * Get the formatted value with a virtual cursor. */ public function valueWithCursor(): string { if ($this->value() === '') { return $this->wrappedPlaceholderWithCursor(); } return $this->addCursor($this->wrappedValue(), $this->cursorPosition + $this->cursorOffset(), -1); } /** * The word-wrapped version of the typed value. */ public function wrappedValue(): string { return $this->mbWordwrap($this->value(), $this->width, PHP_EOL, true); } /** * The formatted lines. * * @return array<int, string> */ public function lines(): array { return explode(PHP_EOL, $this->wrappedValue()); } /** * The currently visible lines. * * @return array<int, string> */ public function visible(): array { $this->adjustVisibleWindow(); $withCursor = $this->valueWithCursor(); return array_slice(explode(PHP_EOL, $withCursor), $this->firstVisible, $this->scroll, preserve_keys: true); } /** * Handle the up key press. */ protected function handleUpKey(): void { if ($this->cursorPosition === 0) { return; } $lines = collect($this->lines()); // Line length + 1 for the newline character $lineLengths = $lines->map(fn ($line, $index) => mb_strlen($line) + ($index === $lines->count() - 1 ? 0 : 1)); $currentLineIndex = $this->currentLineIndex(); if ($currentLineIndex === 0) { // They're already at the first line, jump them to the first position $this->cursorPosition = 0; return; } $currentLines = $lineLengths->slice(0, $currentLineIndex + 1); $currentColumn = $currentLines->last() - ($currentLines->sum() - $this->cursorPosition); $destinationLineLength = ($lineLengths->get($currentLineIndex - 1) ?? $currentLines->first()) - 1; $newColumn = min($destinationLineLength, $currentColumn); $fullLines = $currentLines->slice(0, -2); $this->cursorPosition = $fullLines->sum() + $newColumn; } /** * Handle the down key press. */ protected function handleDownKey(): void { $lines = collect($this->lines()); // Line length + 1 for the newline character $lineLengths = $lines->map(fn ($line, $index) => mb_strlen($line) + ($index === $lines->count() - 1 ? 0 : 1)); $currentLineIndex = $this->currentLineIndex(); if ($currentLineIndex === $lines->count() - 1) { // They're already at the last line, jump them to the last position $this->cursorPosition = mb_strlen($lines->implode(PHP_EOL)); return; } // Lines up to and including the current line $currentLines = $lineLengths->slice(0, $currentLineIndex + 1); $currentColumn = $currentLines->last() - ($currentLines->sum() - $this->cursorPosition); $destinationLineLength = $lineLengths->get($currentLineIndex + 1) ?? $currentLines->last(); if ($currentLineIndex + 1 !== $lines->count() - 1) { $destinationLineLength--; } $newColumn = min(max(0, $destinationLineLength), $currentColumn); $this->cursorPosition = $currentLines->sum() + $newColumn; } /** * Adjust the visible window to ensure the cursor is always visible. */ protected function adjustVisibleWindow(): void { if (count($this->lines()) < $this->scroll) { return; } $currentLineIndex = $this->currentLineIndex(); while ($this->firstVisible + $this->scroll <= $currentLineIndex) { $this->firstVisible++; } if ($currentLineIndex === $this->firstVisible - 1) { $this->firstVisible = max(0, $this->firstVisible - 1); } // Make sure there are always the scroll amount visible if ($this->firstVisible + $this->scroll > count($this->lines())) { $this->firstVisible = count($this->lines()) - $this->scroll; } } /** * Get the index of the current line that the cursor is on. */ protected function currentLineIndex(): int { $totalLineLength = 0; return (int) collect($this->lines())->search(function ($line) use (&$totalLineLength) { $totalLineLength += mb_strlen($line) + 1; return $totalLineLength > $this->cursorPosition; }) ?: 0; } /** * Calculate the cursor offset considering wrapped words. */ protected function cursorOffset(): int { $cursorOffset = 0; preg_match_all('/\S{'.$this->width.',}/u', $this->value(), $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $match) { if ($this->cursorPosition + $cursorOffset >= $match[1] + mb_strwidth($match[0])) { $cursorOffset += (int) floor(mb_strwidth($match[0]) / $this->width); } } return $cursorOffset; } /** * A wrapped version of the placeholder with the virtual cursor. */ protected function wrappedPlaceholderWithCursor(): string { return implode(PHP_EOL, array_map( $this->dim(...), explode(PHP_EOL, $this->addCursor( $this->mbWordwrap($this->placeholder, $this->width, PHP_EOL, true), cursorPosition: 0, )) )); } } prompts/src/Note.php 0000644 00000001276 15060132306 0010457 0 ustar 00 <?php namespace Laravel\Prompts; class Note extends Prompt { /** * Create a new Note instance. */ public function __construct(public string $message, public ?string $type = null) { // } /** * Display the note. */ public function display(): void { $this->prompt(); } /** * Display the note. */ public function prompt(): bool { $this->capturePreviousNewLines(); $this->state = 'submit'; static::output()->write($this->renderTheme()); return true; } /** * Get the value of the prompt. */ public function value(): bool { return true; } } prompts/src/Concerns/Themes.php 0000644 00000007442 15060132306 0012552 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use InvalidArgumentException; use Laravel\Prompts\ConfirmPrompt; use Laravel\Prompts\MultiSearchPrompt; use Laravel\Prompts\MultiSelectPrompt; use Laravel\Prompts\Note; use Laravel\Prompts\PasswordPrompt; use Laravel\Prompts\PausePrompt; use Laravel\Prompts\Progress; use Laravel\Prompts\SearchPrompt; use Laravel\Prompts\SelectPrompt; use Laravel\Prompts\Spinner; use Laravel\Prompts\SuggestPrompt; use Laravel\Prompts\Table; use Laravel\Prompts\TextareaPrompt; use Laravel\Prompts\TextPrompt; use Laravel\Prompts\Themes\Default\ConfirmPromptRenderer; use Laravel\Prompts\Themes\Default\MultiSearchPromptRenderer; use Laravel\Prompts\Themes\Default\MultiSelectPromptRenderer; use Laravel\Prompts\Themes\Default\NoteRenderer; use Laravel\Prompts\Themes\Default\PasswordPromptRenderer; use Laravel\Prompts\Themes\Default\PausePromptRenderer; use Laravel\Prompts\Themes\Default\ProgressRenderer; use Laravel\Prompts\Themes\Default\SearchPromptRenderer; use Laravel\Prompts\Themes\Default\SelectPromptRenderer; use Laravel\Prompts\Themes\Default\SpinnerRenderer; use Laravel\Prompts\Themes\Default\SuggestPromptRenderer; use Laravel\Prompts\Themes\Default\TableRenderer; use Laravel\Prompts\Themes\Default\TextareaPromptRenderer; use Laravel\Prompts\Themes\Default\TextPromptRenderer; trait Themes { /** * The name of the active theme. */ protected static string $theme = 'default'; /** * The available themes. * * @var array<string, array<class-string<\Laravel\Prompts\Prompt>, class-string<object&callable>>> */ protected static array $themes = [ 'default' => [ TextPrompt::class => TextPromptRenderer::class, TextareaPrompt::class => TextareaPromptRenderer::class, PasswordPrompt::class => PasswordPromptRenderer::class, SelectPrompt::class => SelectPromptRenderer::class, MultiSelectPrompt::class => MultiSelectPromptRenderer::class, ConfirmPrompt::class => ConfirmPromptRenderer::class, PausePrompt::class => PausePromptRenderer::class, SearchPrompt::class => SearchPromptRenderer::class, MultiSearchPrompt::class => MultiSearchPromptRenderer::class, SuggestPrompt::class => SuggestPromptRenderer::class, Spinner::class => SpinnerRenderer::class, Note::class => NoteRenderer::class, Table::class => TableRenderer::class, Progress::class => ProgressRenderer::class, ], ]; /** * Get or set the active theme. * * @throws \InvalidArgumentException */ public static function theme(?string $name = null): string { if ($name === null) { return static::$theme; } if (! isset(static::$themes[$name])) { throw new InvalidArgumentException("Prompt theme [{$name}] not found."); } return static::$theme = $name; } /** * Add a new theme. * * @param array<class-string<\Laravel\Prompts\Prompt>, class-string<object&callable>> $renderers */ public static function addTheme(string $name, array $renderers): void { if ($name === 'default') { throw new InvalidArgumentException('The default theme cannot be overridden.'); } static::$themes[$name] = $renderers; } /** * Get the renderer for the current prompt. */ protected function getRenderer(): callable { $class = get_class($this); return new (static::$themes[static::$theme][$class] ?? static::$themes['default'][$class])($this); } /** * Render the prompt using the active theme. */ protected function renderTheme(): string { $renderer = $this->getRenderer(); return $renderer($this); } } prompts/src/Concerns/Truncation.php 0000644 00000005541 15060132306 0013451 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use InvalidArgumentException; trait Truncation { /** * Truncate a value with an ellipsis if it exceeds the given width. */ protected function truncate(string $string, int $width): string { if ($width <= 0) { throw new InvalidArgumentException("Width [{$width}] must be greater than zero."); } return mb_strwidth($string) <= $width ? $string : (mb_strimwidth($string, 0, $width - 1).'…'); } /** * Multi-byte version of wordwrap. * * @param non-empty-string $break */ protected function mbWordwrap( string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false ): string { $lines = explode($break, $string); $result = []; foreach ($lines as $originalLine) { if (mb_strwidth($originalLine) <= $width) { $result[] = $originalLine; continue; } $words = explode(' ', $originalLine); $line = null; $lineWidth = 0; if ($cut_long_words) { foreach ($words as $index => $word) { $characters = mb_str_split($word); $strings = []; $str = ''; foreach ($characters as $character) { $tmp = $str.$character; if (mb_strwidth($tmp) > $width) { $strings[] = $str; $str = $character; } else { $str = $tmp; } } if ($str !== '') { $strings[] = $str; } $words[$index] = implode(' ', $strings); } $words = explode(' ', implode(' ', $words)); } foreach ($words as $word) { $tmp = ($line === null) ? $word : $line.' '.$word; // Look for zero-width joiner characters (combined emojis) preg_match('/\p{Cf}/u', $word, $joinerMatches); $wordWidth = count($joinerMatches) > 0 ? 2 : mb_strwidth($word); $lineWidth += $wordWidth; if ($line !== null) { // Space between words $lineWidth += 1; } if ($lineWidth <= $width) { $line = $tmp; } else { $result[] = $line; $line = $word; $lineWidth = $wordWidth; } } if ($line !== '') { $result[] = $line; } $line = null; } return implode($break, $result); } } prompts/src/Concerns/Fallback.php 0000644 00000002630 15060132306 0013016 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use Closure; use RuntimeException; trait Fallback { /** * Whether to fallback to a custom implementation */ protected static bool $shouldFallback = false; /** * The fallback implementations. * * @var array<class-string, Closure($this): mixed> */ protected static array $fallbacks = []; /** * Enable the fallback implementation. */ public static function fallbackWhen(bool $condition): void { static::$shouldFallback = $condition || static::$shouldFallback; } /** * Whether the prompt should fallback to a custom implementation. */ public static function shouldFallback(): bool { return static::$shouldFallback && isset(static::$fallbacks[static::class]); } /** * Set the fallback implementation. * * @param Closure($this): mixed $fallback */ public static function fallbackUsing(Closure $fallback): void { static::$fallbacks[static::class] = $fallback; } /** * Call the registered fallback implementation. */ public function fallback(): mixed { $fallback = static::$fallbacks[static::class] ?? null; if ($fallback === null) { throw new RuntimeException('No fallback implementation registered for ['.static::class.']'); } return $fallback($this); } } prompts/src/Concerns/Erase.php 0000644 00000001143 15060132306 0012354 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; trait Erase { /** * Erase the given number of lines downwards from the cursor position. */ public function eraseLines(int $count): void { $clear = ''; for ($i = 0; $i < $count; $i++) { $clear .= "\e[2K".($i < $count - 1 ? "\e[{$count}A" : ''); } if ($count) { $clear .= "\e[G"; } static::writeDirectly($clear); } /** * Erase from cursor until end of screen. */ public function eraseDown(): void { static::writeDirectly("\e[J"); } } prompts/src/Concerns/Scrolling.php 0000644 00000005655 15060132306 0013265 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use Laravel\Prompts\Themes\Contracts\Scrolling as ScrollingRenderer; trait Scrolling { /** * The number of items to display before scrolling. */ public int $scroll; /** * The index of the highlighted option. */ public ?int $highlighted; /** * The index of the first visible option. */ public int $firstVisible = 0; /** * Initialize scrolling. */ protected function initializeScrolling(?int $highlighted = null): void { $this->highlighted = $highlighted; $this->reduceScrollingToFitTerminal(); } /** * Reduce the scroll property to fit the terminal height. */ protected function reduceScrollingToFitTerminal(): void { $reservedLines = ($renderer = $this->getRenderer()) instanceof ScrollingRenderer ? $renderer->reservedLines() : 0; $this->scroll = max(1, min($this->scroll, $this->terminal()->lines() - $reservedLines)); } /** * Highlight the given index. */ protected function highlight(?int $index): void { $this->highlighted = $index; if ($this->highlighted === null) { return; } if ($this->highlighted < $this->firstVisible) { $this->firstVisible = $this->highlighted; } elseif ($this->highlighted > $this->firstVisible + $this->scroll - 1) { $this->firstVisible = $this->highlighted - $this->scroll + 1; } } /** * Highlight the previous entry, or wrap around to the last entry. */ protected function highlightPrevious(int $total, bool $allowNull = false): void { if ($total === 0) { return; } if ($this->highlighted === null) { $this->highlight($total - 1); } elseif ($this->highlighted === 0) { $this->highlight($allowNull ? null : ($total - 1)); } else { $this->highlight($this->highlighted - 1); } } /** * Highlight the next entry, or wrap around to the first entry. */ protected function highlightNext(int $total, bool $allowNull = false): void { if ($total === 0) { return; } if ($this->highlighted === $total - 1) { $this->highlight($allowNull ? null : 0); } else { $this->highlight(($this->highlighted ?? -1) + 1); } } /** * Center the highlighted option. */ protected function scrollToHighlighted(int $total): void { if ($this->highlighted < $this->scroll) { return; } $remaining = $total - $this->highlighted - 1; $halfScroll = (int) floor($this->scroll / 2); $endOffset = max(0, $halfScroll - $remaining); if ($this->scroll % 2 === 0) { $endOffset--; } $this->firstVisible = $this->highlighted - $halfScroll - $endOffset; } } prompts/src/Concerns/Termwind.php 0000644 00000001017 15060132306 0013106 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use Laravel\Prompts\Output\BufferedConsoleOutput; use function Termwind\render; use function Termwind\renderUsing; trait Termwind { protected function termwind(string $html) { renderUsing($output = new BufferedConsoleOutput()); render($html); return $this->restoreEscapeSequences($output->fetch()); } protected function restoreEscapeSequences(string $string) { return preg_replace('/\[(\d+)m/', "\e[".'\1m', $string); } } prompts/src/Concerns/TypedValue.php 0000644 00000011570 15060132306 0013404 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use Laravel\Prompts\Key; trait TypedValue { /** * The value that has been typed. */ protected string $typedValue = ''; /** * The position of the virtual cursor. */ protected int $cursorPosition = 0; /** * Track the value as the user types. */ protected function trackTypedValue(string $default = '', bool $submit = true, ?callable $ignore = null, bool $allowNewLine = false): void { $this->typedValue = $default; if ($this->typedValue) { $this->cursorPosition = mb_strlen($this->typedValue); } $this->on('key', function ($key) use ($submit, $ignore, $allowNewLine) { if ($key[0] === "\e" || in_array($key, [Key::CTRL_B, Key::CTRL_F, Key::CTRL_A, Key::CTRL_E])) { if ($ignore !== null && $ignore($key)) { return; } match ($key) { Key::LEFT, Key::LEFT_ARROW, Key::CTRL_B => $this->cursorPosition = max(0, $this->cursorPosition - 1), Key::RIGHT, Key::RIGHT_ARROW, Key::CTRL_F => $this->cursorPosition = min(mb_strlen($this->typedValue), $this->cursorPosition + 1), Key::oneOf([Key::HOME, Key::CTRL_A], $key) => $this->cursorPosition = 0, Key::oneOf([Key::END, Key::CTRL_E], $key) => $this->cursorPosition = mb_strlen($this->typedValue), Key::DELETE => $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition).mb_substr($this->typedValue, $this->cursorPosition + 1), default => null, }; return; } // Keys may be buffered. foreach (mb_str_split($key) as $key) { if ($ignore !== null && $ignore($key)) { return; } if ($key === Key::ENTER) { if ($submit) { $this->submit(); return; } if ($allowNewLine) { $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition).PHP_EOL.mb_substr($this->typedValue, $this->cursorPosition); $this->cursorPosition++; } } elseif ($key === Key::BACKSPACE || $key === Key::CTRL_H) { if ($this->cursorPosition === 0) { return; } $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition - 1).mb_substr($this->typedValue, $this->cursorPosition); $this->cursorPosition--; } elseif (ord($key) >= 32) { $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition).$key.mb_substr($this->typedValue, $this->cursorPosition); $this->cursorPosition++; } } }); } /** * Get the value of the prompt. */ public function value(): string { return $this->typedValue; } /** * Add a virtual cursor to the value and truncate if necessary. */ protected function addCursor(string $value, int $cursorPosition, ?int $maxWidth = null): string { $before = mb_substr($value, 0, $cursorPosition); $current = mb_substr($value, $cursorPosition, 1); $after = mb_substr($value, $cursorPosition + 1); $cursor = mb_strlen($current) && $current !== PHP_EOL ? $current : ' '; $spaceBefore = $maxWidth < 0 || $maxWidth === null ? mb_strwidth($before) : $maxWidth - mb_strwidth($cursor) - (mb_strwidth($after) > 0 ? 1 : 0); [$truncatedBefore, $wasTruncatedBefore] = mb_strwidth($before) > $spaceBefore ? [$this->trimWidthBackwards($before, 0, $spaceBefore - 1), true] : [$before, false]; $spaceAfter = $maxWidth < 0 || $maxWidth === null ? mb_strwidth($after) : $maxWidth - ($wasTruncatedBefore ? 1 : 0) - mb_strwidth($truncatedBefore) - mb_strwidth($cursor); [$truncatedAfter, $wasTruncatedAfter] = mb_strwidth($after) > $spaceAfter ? [mb_strimwidth($after, 0, $spaceAfter - 1), true] : [$after, false]; return ($wasTruncatedBefore ? $this->dim('…') : '') .$truncatedBefore .$this->inverse($cursor) .($current === PHP_EOL ? PHP_EOL : '') .$truncatedAfter .($wasTruncatedAfter ? $this->dim('…') : ''); } /** * Get a truncated string with the specified width from the end. */ private function trimWidthBackwards(string $string, int $start, int $width): string { $reversed = implode('', array_reverse(mb_str_split($string, 1))); $trimmed = mb_strimwidth($reversed, $start, $width); return implode('', array_reverse(mb_str_split($trimmed, 1))); } } prompts/src/Concerns/Colors.php 0000644 00000007406 15060132306 0012566 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; trait Colors { /** * Reset all colors and styles. */ public function reset(string $text): string { return "\e[0m{$text}\e[0m"; } /** * Make the text bold. */ public function bold(string $text): string { return "\e[1m{$text}\e[22m"; } /** * Make the text dim. */ public function dim(string $text): string { return "\e[2m{$text}\e[22m"; } /** * Make the text italic. */ public function italic(string $text): string { return "\e[3m{$text}\e[23m"; } /** * Underline the text. */ public function underline(string $text): string { return "\e[4m{$text}\e[24m"; } /** * Invert the text and background colors. */ public function inverse(string $text): string { return "\e[7m{$text}\e[27m"; } /** * Hide the text. */ public function hidden(string $text): string { return "\e[8m{$text}\e[28m"; } /** * Strike through the text. */ public function strikethrough(string $text): string { return "\e[9m{$text}\e[29m"; } /** * Set the text color to black. */ public function black(string $text): string { return "\e[30m{$text}\e[39m"; } /** * Set the text color to red. */ public function red(string $text): string { return "\e[31m{$text}\e[39m"; } /** * Set the text color to green. */ public function green(string $text): string { return "\e[32m{$text}\e[39m"; } /** * Set the text color to yellow. */ public function yellow(string $text): string { return "\e[33m{$text}\e[39m"; } /** * Set the text color to blue. */ public function blue(string $text): string { return "\e[34m{$text}\e[39m"; } /** * Set the text color to magenta. */ public function magenta(string $text): string { return "\e[35m{$text}\e[39m"; } /** * Set the text color to cyan. */ public function cyan(string $text): string { return "\e[36m{$text}\e[39m"; } /** * Set the text color to white. */ public function white(string $text): string { return "\e[37m{$text}\e[39m"; } /** * Set the text background to black. */ public function bgBlack(string $text): string { return "\e[40m{$text}\e[49m"; } /** * Set the text background to red. */ public function bgRed(string $text): string { return "\e[41m{$text}\e[49m"; } /** * Set the text background to green. */ public function bgGreen(string $text): string { return "\e[42m{$text}\e[49m"; } /** * Set the text background to yellow. */ public function bgYellow(string $text): string { return "\e[43m{$text}\e[49m"; } /** * Set the text background to blue. */ public function bgBlue(string $text): string { return "\e[44m{$text}\e[49m"; } /** * Set the text background to magenta. */ public function bgMagenta(string $text): string { return "\e[45m{$text}\e[49m"; } /** * Set the text background to cyan. */ public function bgCyan(string $text): string { return "\e[46m{$text}\e[49m"; } /** * Set the text background to white. */ public function bgWhite(string $text): string { return "\e[47m{$text}\e[49m"; } /** * Set the text color to gray. */ public function gray(string $text): string { return "\e[90m{$text}\e[39m"; } } prompts/src/Concerns/FakesInputOutput.php 0000644 00000005243 15060132306 0014614 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use Laravel\Prompts\Output\BufferedConsoleOutput; use Laravel\Prompts\Terminal; use PHPUnit\Framework\Assert; use RuntimeException; trait FakesInputOutput { /** * Fake the terminal and queue key presses to be simulated. * * @param array<string> $keys */ public static function fake(array $keys = []): void { // Force interactive mode when testing because we will be mocking the terminal. static::interactive(); $mock = \Mockery::mock(Terminal::class); $mock->shouldReceive('write')->byDefault(); $mock->shouldReceive('exit')->byDefault(); $mock->shouldReceive('setTty')->byDefault(); $mock->shouldReceive('restoreTty')->byDefault(); $mock->shouldReceive('cols')->byDefault()->andReturn(80); $mock->shouldReceive('lines')->byDefault()->andReturn(24); $mock->shouldReceive('initDimensions')->byDefault(); foreach ($keys as $key) { $mock->shouldReceive('read')->once()->andReturn($key); } static::$terminal = $mock; self::setOutput(new BufferedConsoleOutput()); } /** * Assert that the output contains the given string. */ public static function assertOutputContains(string $string): void { Assert::assertStringContainsString($string, static::content()); } /** * Assert that the output doesn't contain the given string. */ public static function assertOutputDoesntContain(string $string): void { Assert::assertStringNotContainsString($string, static::content()); } /** * Assert that the stripped output contains the given string. */ public static function assertStrippedOutputContains(string $string): void { Assert::assertStringContainsString($string, static::strippedContent()); } /** * Assert that the stripped output doesn't contain the given string. */ public static function assertStrippedOutputDoesntContain(string $string): void { Assert::assertStringNotContainsString($string, static::strippedContent()); } /** * Get the buffered console output. */ public static function content(): string { if (! static::output() instanceof BufferedConsoleOutput) { throw new RuntimeException('Prompt must be faked before accessing content.'); } return static::output()->content(); } /** * Get the buffered console output, stripped of escape sequences. */ public static function strippedContent(): string { return preg_replace("/\e\[[0-9;?]*[A-Za-z]/", '', static::content()); } } prompts/src/Concerns/Events.php 0000644 00000001361 15060132306 0012563 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use Closure; trait Events { /** * The registered event listeners. * * @var array<string, array<int, Closure>> */ protected array $listeners = []; /** * Register an event listener. */ public function on(string $event, Closure $callback): void { $this->listeners[$event][] = $callback; } /** * Emit an event. */ public function emit(string $event, mixed ...$data): void { foreach ($this->listeners[$event] ?? [] as $listener) { $listener(...$data); } } /** * Clean the event listeners. */ public function clearListeners(): void { $this->listeners = []; } } prompts/src/Concerns/Interactivity.php 0000644 00000001371 15060132306 0014156 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; use Laravel\Prompts\Exceptions\NonInteractiveValidationException; trait Interactivity { /** * Whether to render the prompt interactively. */ protected static bool $interactive; /** * Set interactive mode. */ public static function interactive(bool $interactive = true): void { static::$interactive = $interactive; } /** * Return the default value if it passes validation. */ protected function default(): mixed { $default = $this->value(); $this->validate($default); if ($this->state === 'error') { throw new NonInteractiveValidationException($this->error); } return $default; } } prompts/src/Concerns/Cursor.php 0000644 00000003042 15060132306 0012572 0 ustar 00 <?php namespace Laravel\Prompts\Concerns; trait Cursor { /** * Indicates if the cursor has been hidden. */ protected static bool $cursorHidden = false; /** * Hide the cursor. */ public function hideCursor(): void { static::writeDirectly("\e[?25l"); static::$cursorHidden = true; } /** * Show the cursor. */ public function showCursor(): void { static::writeDirectly("\e[?25h"); static::$cursorHidden = false; } /** * Restore the cursor if it was hidden. */ public function restoreCursor(): void { if (static::$cursorHidden) { $this->showCursor(); } } /** * Move the cursor. */ public function moveCursor(int $x, int $y = 0): void { $sequence = ''; if ($x < 0) { $sequence .= "\e[".abs($x).'D'; // Left } elseif ($x > 0) { $sequence .= "\e[{$x}C"; // Right } if ($y < 0) { $sequence .= "\e[".abs($y).'A'; // Up } elseif ($y > 0) { $sequence .= "\e[{$y}B"; // Down } static::writeDirectly($sequence); } /** * Move the cursor to the given column. */ public function moveCursorToColumn(int $column): void { static::writeDirectly("\e[{$column}G"); } /** * Move the cursor up by the given number of lines. */ public function moveCursorUp(int $lines): void { static::writeDirectly("\e[{$lines}A"); } } prompts/src/ConfirmPrompt.php 0000644 00000002471 15060132306 0012347 0 ustar 00 <?php namespace Laravel\Prompts; class ConfirmPrompt extends Prompt { /** * Whether the prompt has been confirmed. */ public bool $confirmed; /** * Create a new ConfirmPrompt instance. */ public function __construct( public string $label, public bool $default = true, public string $yes = 'Yes', public string $no = 'No', public bool|string $required = false, public mixed $validate = null, public string $hint = '', ) { $this->confirmed = $default; $this->on('key', fn ($key) => match ($key) { 'y' => $this->confirmed = true, 'n' => $this->confirmed = false, Key::TAB, Key::UP, Key::UP_ARROW, Key::DOWN, Key::DOWN_ARROW, Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::CTRL_P, Key::CTRL_F, Key::CTRL_N, Key::CTRL_B, 'h', 'j', 'k', 'l' => $this->confirmed = ! $this->confirmed, Key::ENTER => $this->submit(), default => null, }); } /** * Get the value of the prompt. */ public function value(): bool { return $this->confirmed; } /** * Get the label of the selected option. */ public function label(): string { return $this->confirmed ? $this->yes : $this->no; } } prompts/src/MultiSelectPrompt.php 0000644 00000010121 15060132306 0013173 0 ustar 00 <?php namespace Laravel\Prompts; use Illuminate\Support\Collection; class MultiSelectPrompt extends Prompt { use Concerns\Scrolling; /** * The options for the multi-select prompt. * * @var array<int|string, string> */ public array $options; /** * The default values the multi-select prompt. * * @var array<int|string> */ public array $default; /** * The selected values. * * @var array<int|string> */ protected array $values = []; /** * Create a new MultiSelectPrompt instance. * * @param array<int|string, string>|Collection<int|string, string> $options * @param array<int|string>|Collection<int, int|string> $default */ public function __construct( public string $label, array|Collection $options, array|Collection $default = [], public int $scroll = 5, public bool|string $required = false, public mixed $validate = null, public string $hint = '', ) { $this->options = $options instanceof Collection ? $options->all() : $options; $this->default = $default instanceof Collection ? $default->all() : $default; $this->values = $this->default; $this->initializeScrolling(0); $this->on('key', fn ($key) => match ($key) { Key::UP, Key::UP_ARROW, Key::LEFT, Key::LEFT_ARROW, Key::SHIFT_TAB, Key::CTRL_P, Key::CTRL_B, 'k', 'h' => $this->highlightPrevious(count($this->options)), Key::DOWN, Key::DOWN_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::TAB, Key::CTRL_N, Key::CTRL_F, 'j', 'l' => $this->highlightNext(count($this->options)), Key::oneOf(Key::HOME, $key) => $this->highlight(0), Key::oneOf(Key::END, $key) => $this->highlight(count($this->options) - 1), Key::SPACE => $this->toggleHighlighted(), Key::CTRL_A => $this->toggleAll(), Key::ENTER => $this->submit(), default => null, }); } /** * Get the selected values. * * @return array<int|string> */ public function value(): array { return array_values($this->values); } /** * Get the selected labels. * * @return array<string> */ public function labels(): array { if (array_is_list($this->options)) { return array_map(fn ($value) => (string) $value, $this->values); } return array_values(array_intersect_key($this->options, array_flip($this->values))); } /** * The currently visible options. * * @return array<int|string, string> */ public function visible(): array { return array_slice($this->options, $this->firstVisible, $this->scroll, preserve_keys: true); } /** * Check whether the value is currently highlighted. */ public function isHighlighted(string $value): bool { if (array_is_list($this->options)) { return $this->options[$this->highlighted] === $value; } return array_keys($this->options)[$this->highlighted] === $value; } /** * Check whether the value is currently selected. */ public function isSelected(string $value): bool { return in_array($value, $this->values); } /** * Toggle all options. */ protected function toggleAll(): void { if (count($this->values) === count($this->options)) { $this->values = []; } else { $this->values = array_is_list($this->options) ? array_values($this->options) : array_keys($this->options); } } /** * Toggle the highlighted entry. */ protected function toggleHighlighted(): void { $value = array_is_list($this->options) ? $this->options[$this->highlighted] : array_keys($this->options)[$this->highlighted]; if (in_array($value, $this->values)) { $this->values = array_filter($this->values, fn ($v) => $v !== $value); } else { $this->values[] = $value; } } } prompts/src/Key.php 0000644 00000003040 15060132306 0010271 0 ustar 00 <?php namespace Laravel\Prompts; class Key { const UP = "\e[A"; const SHIFT_UP = "\e[1;2A"; const DOWN = "\e[B"; const SHIFT_DOWN = "\e[1;2B"; const RIGHT = "\e[C"; const LEFT = "\e[D"; const UP_ARROW = "\eOA"; const DOWN_ARROW = "\eOB"; const RIGHT_ARROW = "\eOC"; const LEFT_ARROW = "\eOD"; const ESCAPE = "\e"; const DELETE = "\e[3~"; const BACKSPACE = "\177"; const ENTER = "\n"; const SPACE = ' '; const TAB = "\t"; const SHIFT_TAB = "\e[Z"; const HOME = ["\e[1~", "\eOH", "\e[H", "\e[7~"]; const END = ["\e[4~", "\eOF", "\e[F", "\e[8~"]; /** * Cancel/SIGINT */ const CTRL_C = "\x03"; /** * Previous/Up */ const CTRL_P = "\x10"; /** * Next/Down */ const CTRL_N = "\x0E"; /** * Forward/Right */ const CTRL_F = "\x06"; /** * Back/Left */ const CTRL_B = "\x02"; /** * Backspace */ const CTRL_H = "\x08"; /** * Home */ const CTRL_A = "\x01"; /** * EOF */ const CTRL_D = "\x04"; /** * End */ const CTRL_E = "\x05"; /** * Negative affirmation */ const CTRL_U = "\x15"; /** * Checks for the constant values for the given match and returns the match * * @param array<string|array<string>> $keys */ public static function oneOf(array $keys, string $match): ?string { return collect($keys)->flatten()->contains($match) ? $match : null; } } prompts/src/Themes/Default/SelectPromptRenderer.php 0000644 00000006246 15060132306 0016475 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\SelectPrompt; use Laravel\Prompts\Themes\Contracts\Scrolling; class SelectPromptRenderer extends Renderer implements Scrolling { use Concerns\DrawsBoxes; use Concerns\DrawsScrollbars; /** * Render the select prompt. */ public function __invoke(SelectPrompt $prompt): string { $maxWidth = $prompt->terminal()->cols() - 6; return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->truncate($prompt->label(), $maxWidth), ), 'cancel' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->renderOptions($prompt), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->renderOptions($prompt), color: 'yellow', ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->renderOptions($prompt), ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ), }; } /** * Render the options. */ protected function renderOptions(SelectPrompt $prompt): string { return $this->scrollbar( collect($prompt->visible()) ->map(fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 12)) ->map(function ($label, $key) use ($prompt) { $index = array_search($key, array_keys($prompt->options)); if ($prompt->state === 'cancel') { return $this->dim($prompt->highlighted === $index ? "› ● {$this->strikethrough($label)} " : " ○ {$this->strikethrough($label)} " ); } return $prompt->highlighted === $index ? "{$this->cyan('›')} {$this->cyan('●')} {$label} " : " {$this->dim('○')} {$this->dim($label)} "; }) ->values(), $prompt->firstVisible, $prompt->scroll, count($prompt->options), min($this->longest($prompt->options, padding: 6), $prompt->terminal()->cols() - 6), $prompt->state === 'cancel' ? 'dim' : 'cyan' )->implode(PHP_EOL); } /** * The number of lines to reserve outside of the scrollable area. */ public function reservedLines(): int { return 5; } } prompts/src/Themes/Default/SpinnerRenderer.php 0000644 00000001646 15060132306 0015471 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\Spinner; class SpinnerRenderer extends Renderer { /** * The frames of the spinner. * * @var array<string> */ protected array $frames = ['⠂', '⠒', '⠐', '⠰', '⠠', '⠤', '⠄', '⠆']; /** * The frame to render when the spinner is static. */ protected string $staticFrame = '⠶'; /** * The interval between frames. */ protected int $interval = 75; /** * Render the spinner. */ public function __invoke(Spinner $spinner): string { if ($spinner->static) { return $this->line(" {$this->cyan($this->staticFrame)} {$spinner->message}"); } $spinner->interval = $this->interval; $frame = $this->frames[$spinner->count % count($this->frames)]; return $this->line(" {$this->cyan($frame)} {$spinner->message}"); } } prompts/src/Themes/Default/PausePromptRenderer.php 0000644 00000001171 15060132306 0016323 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\PausePrompt; class PausePromptRenderer extends Renderer { use Concerns\DrawsBoxes; /** * Render the pause prompt. */ public function __invoke(PausePrompt $prompt): string { match ($prompt->state) { 'submit' => collect(explode(PHP_EOL, $prompt->message)) ->each(fn ($line) => $this->line($this->gray(" {$line}"))), default => collect(explode(PHP_EOL, $prompt->message)) ->each(fn ($line) => $this->line($this->green(" {$line}"))) }; return $this; } } prompts/src/Themes/Default/TextareaPromptRenderer.php 0000644 00000005057 15060132306 0017032 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\TextareaPrompt; use Laravel\Prompts\Themes\Contracts\Scrolling; class TextareaPromptRenderer extends Renderer implements Scrolling { use Concerns\DrawsBoxes; use Concerns\DrawsScrollbars; /** * Render the textarea prompt. */ public function __invoke(TextareaPrompt $prompt): string { $prompt->width = $prompt->terminal()->cols() - 8; return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->width)), collect($prompt->lines())->implode(PHP_EOL), ), 'cancel' => $this ->box( $this->truncate($prompt->label, $prompt->width), collect($prompt->lines())->map(fn ($line) => $this->strikethrough($this->dim($line)))->implode(PHP_EOL), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->width), $this->renderText($prompt), color: 'yellow', info: 'Ctrl+D to submit' ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->width)), $this->renderText($prompt), info: 'Ctrl+D to submit' ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ) }; } /** * Render the text in the prompt. */ protected function renderText(TextareaPrompt $prompt): string { $visible = collect($prompt->visible()); while ($visible->count() < $prompt->scroll) { $visible->push(''); } $longest = $this->longest($prompt->lines()) + 2; return $this->scrollbar( $visible, $prompt->firstVisible, $prompt->scroll, count($prompt->lines()), min($longest, $prompt->width + 2), )->implode(PHP_EOL); } /** * The number of lines to reserve outside of the scrollable area. */ public function reservedLines(): int { return 5; } } prompts/src/Themes/Default/Concerns/DrawsScrollbars.php 0000644 00000003044 15060132306 0017237 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default\Concerns; use Illuminate\Support\Collection; trait DrawsScrollbars { /** * Render a scrollbar beside the visible items. * * @param \Illuminate\Support\Collection<int, string> $visible * @return \Illuminate\Support\Collection<int, string> */ protected function scrollbar(Collection $visible, int $firstVisible, int $height, int $total, int $width, string $color = 'cyan'): Collection { if ($height >= $total) { return $visible; } $scrollPosition = $this->scrollPosition($firstVisible, $height, $total); return $visible // @phpstan-ignore return.type ->values() ->map(fn ($line) => $this->pad($line, $width)) ->map(fn ($line, $index) => match ($index) { $scrollPosition => preg_replace('/.$/', $this->{$color}('┃'), $line), default => preg_replace('/.$/', $this->gray('│'), $line), }); } /** * Return the position where the scrollbar "handle" should be rendered. */ protected function scrollPosition(int $firstVisible, int $height, int $total): int { if ($firstVisible === 0) { return 0; } $maxPosition = $total - $height; if ($firstVisible === $maxPosition) { return $height - 1; } if ($height <= 2) { return -1; } $percent = $firstVisible / $maxPosition; return (int) round($percent * ($height - 3)) + 1; } } prompts/src/Themes/Default/Concerns/DrawsBoxes.php 0000644 00000003506 15060132306 0016214 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default\Concerns; use Laravel\Prompts\Prompt; trait DrawsBoxes { use InteractsWithStrings; protected int $minWidth = 60; /** * Draw a box. * * @return $this */ protected function box( string $title, string $body, string $footer = '', string $color = 'gray', string $info = '', ): self { $this->minWidth = min($this->minWidth, Prompt::terminal()->cols() - 6); $bodyLines = collect(explode(PHP_EOL, $body)); $footerLines = collect(explode(PHP_EOL, $footer))->filter(); $width = $this->longest( $bodyLines ->merge($footerLines) ->push($title) ->toArray() ); $titleLength = mb_strwidth($this->stripEscapeSequences($title)); $titleLabel = $titleLength > 0 ? " {$title} " : ''; $topBorder = str_repeat('─', $width - $titleLength + ($titleLength > 0 ? 0 : 2)); $this->line("{$this->{$color}(' ┌')}{$titleLabel}{$this->{$color}($topBorder.'┐')}"); $bodyLines->each(function ($line) use ($width, $color) { $this->line("{$this->{$color}(' │')} {$this->pad($line, $width)} {$this->{$color}('│')}"); }); if ($footerLines->isNotEmpty()) { $this->line($this->{$color}(' ├'.str_repeat('─', $width + 2).'┤')); $footerLines->each(function ($line) use ($width, $color) { $this->line("{$this->{$color}(' │')} {$this->pad($line, $width)} {$this->{$color}('│')}"); }); } $this->line($this->{$color}(' └'.str_repeat( '─', $info ? ($width - mb_strwidth($this->stripEscapeSequences($info))) : ($width + 2) ).($info ? " {$info} " : '').'┘')); return $this; } } prompts/src/Themes/Default/Concerns/InteractsWithStrings.php 0000644 00000002436 15060132306 0020276 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default\Concerns; trait InteractsWithStrings { /** * Get the length of the longest line. * * @param array<string> $lines */ protected function longest(array $lines, int $padding = 0): int { return max( $this->minWidth, collect($lines) ->map(fn ($line) => mb_strwidth($this->stripEscapeSequences($line)) + $padding) ->max() ); } /** * Pad text ignoring ANSI escape sequences. */ protected function pad(string $text, int $length, string $char = ' '): string { $rightPadding = str_repeat($char, max(0, $length - mb_strwidth($this->stripEscapeSequences($text)))); return "{$text}{$rightPadding}"; } /** * Strip ANSI escape sequences from the given text. */ protected function stripEscapeSequences(string $text): string { // Strip ANSI escape sequences. $text = preg_replace("/\e[^m]*m/", '', $text); // Strip Symfony named style tags. $text = preg_replace("/<(info|comment|question|error)>(.*?)<\/\\1>/", '$2', $text); // Strip Symfony inline style tags. return preg_replace("/<(?:(?:[fb]g|options)=[a-z,;]+)+>(.*?)<\/>/i", '$1', $text); } } prompts/src/Themes/Default/SearchPromptRenderer.php 0000644 00000010655 15060132306 0016462 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\SearchPrompt; use Laravel\Prompts\Themes\Contracts\Scrolling; class SearchPromptRenderer extends Renderer implements Scrolling { use Concerns\DrawsBoxes; use Concerns\DrawsScrollbars; /** * Render the suggest prompt. */ public function __invoke(SearchPrompt $prompt): string { $maxWidth = $prompt->terminal()->cols() - 6; return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->truncate($prompt->label(), $maxWidth), ), 'cancel' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->strikethrough($this->dim($this->truncate($prompt->searchValue() ?: $prompt->placeholder, $maxWidth))), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $prompt->valueWithCursor($maxWidth), $this->renderOptions($prompt), color: 'yellow', ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), 'searching' => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->valueWithCursorAndSearchIcon($prompt, $maxWidth), $this->renderOptions($prompt), ) ->hint($prompt->hint), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $prompt->valueWithCursor($maxWidth), $this->renderOptions($prompt), ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ) ->spaceForDropdown($prompt) }; } /** * Render the value with the cursor and a search icon. */ protected function valueWithCursorAndSearchIcon(SearchPrompt $prompt, int $maxWidth): string { return preg_replace( '/\s$/', $this->cyan('…'), $this->pad($prompt->valueWithCursor($maxWidth - 1).' ', min($this->longest($prompt->matches(), padding: 2), $maxWidth)) ); } /** * Render a spacer to prevent jumping when the suggestions are displayed. */ protected function spaceForDropdown(SearchPrompt $prompt): self { if ($prompt->searchValue() !== '') { return $this; } $this->newLine(max( 0, min($prompt->scroll, $prompt->terminal()->lines() - 7) - count($prompt->matches()), )); if ($prompt->matches() === []) { $this->newLine(); } return $this; } /** * Render the options. */ protected function renderOptions(SearchPrompt $prompt): string { if ($prompt->searchValue() !== '' && empty($prompt->matches())) { return $this->gray(' '.($prompt->state === 'searching' ? 'Searching...' : 'No results.')); } return $this->scrollbar( collect($prompt->visible()) ->map(fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 10)) ->map(function ($label, $key) use ($prompt) { $index = array_search($key, array_keys($prompt->matches())); return $prompt->highlighted === $index ? "{$this->cyan('›')} {$label} " : " {$this->dim($label)} "; }) ->values(), $prompt->firstVisible, $prompt->scroll, count($prompt->matches()), min($this->longest($prompt->matches(), padding: 4), $prompt->terminal()->cols() - 6) )->implode(PHP_EOL); } /** * The number of lines to reserve outside of the scrollable area. */ public function reservedLines(): int { return 7; } } prompts/src/Themes/Default/ProgressRenderer.php 0000644 00000004035 15060132306 0015652 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\Progress; class ProgressRenderer extends Renderer { use Concerns\DrawsBoxes; /** * The character to use for the progress bar. */ protected string $barCharacter = '█'; /** * Render the progress bar. * * @param Progress<int|iterable<mixed>> $progress */ public function __invoke(Progress $progress): string { $filled = str_repeat($this->barCharacter, (int) ceil($progress->percentage() * min($this->minWidth, $progress->terminal()->cols() - 6))); return match ($progress->state) { 'submit' => $this ->box( $this->dim($this->truncate($progress->label, $progress->terminal()->cols() - 6)), $this->dim($filled), info: $progress->progress.'/'.$progress->total, ), 'error' => $this ->box( $this->truncate($progress->label, $progress->terminal()->cols() - 6), $this->dim($filled), color: 'red', info: $progress->progress.'/'.$progress->total, ), 'cancel' => $this ->box( $this->truncate($progress->label, $progress->terminal()->cols() - 6), $this->dim($filled), color: 'red', info: $progress->progress.'/'.$progress->total, ) ->error($progress->cancelMessage), default => $this ->box( $this->cyan($this->truncate($progress->label, $progress->terminal()->cols() - 6)), $this->dim($filled), info: $progress->progress.'/'.$progress->total, ) ->when( $progress->hint, fn () => $this->hint($progress->hint), fn () => $this->newLine() // Space for errors ) }; } } prompts/src/Themes/Default/Renderer.php 0000644 00000004145 15060132306 0014127 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\Concerns\Colors; use Laravel\Prompts\Concerns\Truncation; use Laravel\Prompts\Prompt; abstract class Renderer { use Colors; use Truncation; /** * The output to be rendered. */ protected string $output = ''; /** * Create a new renderer instance. */ public function __construct(protected Prompt $prompt) { // } /** * Render a line of output. */ protected function line(string $message): self { $this->output .= $message.PHP_EOL; return $this; } /** * Render a new line. */ protected function newLine(int $count = 1): self { $this->output .= str_repeat(PHP_EOL, $count); return $this; } /** * Render a warning message. */ protected function warning(string $message): self { return $this->line($this->yellow(" ⚠ {$message}")); } /** * Render an error message. */ protected function error(string $message): self { return $this->line($this->red(" ⚠ {$message}")); } /** * Render an hint message. */ protected function hint(string $message): self { if ($message === '') { return $this; } $message = $this->truncate($message, $this->prompt->terminal()->cols() - 6); return $this->line($this->gray(" {$message}")); } /** * Apply the callback if the given "value" is truthy. * * @return $this */ protected function when(mixed $value, callable $callback, ?callable $default = null): self { if ($value) { $callback($this); } elseif ($default) { $default($this); } return $this; } /** * Render the output with a blank line above and below. */ public function __toString() { return str_repeat(PHP_EOL, max(2 - $this->prompt->newLinesWritten(), 0)) .$this->output .(in_array($this->prompt->state, ['submit', 'cancel']) ? PHP_EOL : ''); } } prompts/src/Themes/Default/MultiSearchPromptRenderer.php 0000644 00000013643 15060132306 0017475 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\MultiSearchPrompt; use Laravel\Prompts\Themes\Contracts\Scrolling; class MultiSearchPromptRenderer extends Renderer implements Scrolling { use Concerns\DrawsBoxes; use Concerns\DrawsScrollbars; /** * Render the suggest prompt. */ public function __invoke(MultiSearchPrompt $prompt): string { $maxWidth = $prompt->terminal()->cols() - 6; return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->renderSelectedOptions($prompt), ), 'cancel' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->strikethrough($this->dim($this->truncate($prompt->searchValue() ?: $prompt->placeholder, $maxWidth))), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $prompt->valueWithCursor($maxWidth), $this->renderOptions($prompt), color: 'yellow', info: $this->getInfoText($prompt), ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), 'searching' => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->valueWithCursorAndSearchIcon($prompt, $maxWidth), $this->renderOptions($prompt), info: $this->getInfoText($prompt), ) ->hint($prompt->hint), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $prompt->valueWithCursor($maxWidth), $this->renderOptions($prompt), info: $this->getInfoText($prompt), ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ) ->spaceForDropdown($prompt) }; } /** * Render the value with the cursor and a search icon. */ protected function valueWithCursorAndSearchIcon(MultiSearchPrompt $prompt, int $maxWidth): string { return preg_replace( '/\s$/', $this->cyan('…'), $this->pad($prompt->valueWithCursor($maxWidth - 1).' ', min($this->longest($prompt->matches(), padding: 2), $maxWidth)) ); } /** * Render a spacer to prevent jumping when the suggestions are displayed. */ protected function spaceForDropdown(MultiSearchPrompt $prompt): self { if ($prompt->searchValue() !== '') { return $this; } $this->newLine(max( 0, min($prompt->scroll, $prompt->terminal()->lines() - 7) - count($prompt->matches()), )); if ($prompt->matches() === []) { $this->newLine(); } return $this; } /** * Render the options. */ protected function renderOptions(MultiSearchPrompt $prompt): string { if ($prompt->searchValue() !== '' && empty($prompt->matches())) { return $this->gray(' '.($prompt->state === 'searching' ? 'Searching...' : 'No results.')); } return $this->scrollbar( collect($prompt->visible()) ->map(fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 12)) ->map(function ($label, $key) use ($prompt) { $index = array_search($key, array_keys($prompt->matches())); $active = $index === $prompt->highlighted; $selected = $prompt->isList() ? in_array($label, $prompt->value()) : in_array($key, $prompt->value()); return match (true) { $active && $selected => "{$this->cyan('› ◼')} {$label} ", $active => "{$this->cyan('›')} ◻ {$label} ", $selected => " {$this->cyan('◼')} {$this->dim($label)} ", default => " {$this->dim('◻')} {$this->dim($label)} ", }; }), $prompt->firstVisible, $prompt->scroll, count($prompt->matches()), min($this->longest($prompt->matches(), padding: 4), $prompt->terminal()->cols() - 6) )->implode(PHP_EOL); } /** * Render the selected options. */ protected function renderSelectedOptions(MultiSearchPrompt $prompt): string { if (count($prompt->labels()) === 0) { return $this->gray('None'); } return implode("\n", array_map( fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 6), $prompt->labels() )); } /** * Render the info text. */ protected function getInfoText(MultiSearchPrompt $prompt): string { $info = count($prompt->value()).' selected'; $hiddenCount = count($prompt->value()) - collect($prompt->matches()) ->filter(fn ($label, $key) => in_array($prompt->isList() ? $label : $key, $prompt->value())) ->count(); if ($hiddenCount > 0) { $info .= " ($hiddenCount hidden)"; } return $info; } /** * The number of lines to reserve outside of the scrollable area. */ public function reservedLines(): int { return 7; } } prompts/src/Themes/Default/NoteRenderer.php 0000644 00000002734 15060132306 0014757 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\Note; class NoteRenderer extends Renderer { /** * Render the note. */ public function __invoke(Note $note): string { $lines = collect(explode(PHP_EOL, $note->message)); switch ($note->type) { case 'intro': case 'outro': $lines = $lines->map(fn ($line) => " {$line} "); $longest = $lines->map(fn ($line) => strlen($line))->max(); $lines ->each(function ($line) use ($longest) { $line = str_pad($line, $longest, ' '); $this->line(" {$this->bgCyan($this->black($line))}"); }); return $this; case 'warning': $lines->each(fn ($line) => $this->line($this->yellow(" {$line}"))); return $this; case 'error': $lines->each(fn ($line) => $this->line($this->red(" {$line}"))); return $this; case 'alert': $lines->each(fn ($line) => $this->line(" {$this->bgRed($this->white(" {$line} "))}")); return $this; case 'info': $lines->each(fn ($line) => $this->line($this->green(" {$line}"))); return $this; default: $lines->each(fn ($line) => $this->line(" {$line}")); return $this; } } } prompts/src/Themes/Default/PasswordPromptRenderer.php 0000644 00000003307 15060132306 0017053 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\PasswordPrompt; class PasswordPromptRenderer extends Renderer { use Concerns\DrawsBoxes; /** * Render the password prompt. */ public function __invoke(PasswordPrompt $prompt): string { $maxWidth = $prompt->terminal()->cols() - 6; return match ($prompt->state) { 'submit' => $this ->box( $this->dim($prompt->label), $this->truncate($prompt->masked(), $maxWidth), ), 'cancel' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->strikethrough($this->dim($this->truncate($prompt->masked() ?: $prompt->placeholder, $maxWidth))), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $prompt->maskedWithCursor($maxWidth), color: 'yellow', ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $prompt->maskedWithCursor($maxWidth), ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ), }; } } prompts/src/Themes/Default/ConfirmPromptRenderer.php 0000644 00000004572 15060132306 0016653 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\ConfirmPrompt; class ConfirmPromptRenderer extends Renderer { use Concerns\DrawsBoxes; /** * Render the confirm prompt. */ public function __invoke(ConfirmPrompt $prompt): string { return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->truncate($prompt->label(), $prompt->terminal()->cols() - 6) ), 'cancel' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->renderOptions($prompt), color: 'red' ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->renderOptions($prompt), color: 'yellow', ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->renderOptions($prompt), ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ), }; } /** * Render the confirm prompt options. */ protected function renderOptions(ConfirmPrompt $prompt): string { $length = (int) floor(($prompt->terminal()->cols() - 14) / 2); $yes = $this->truncate($prompt->yes, $length); $no = $this->truncate($prompt->no, $length); if ($prompt->state === 'cancel') { return $this->dim($prompt->confirmed ? "● {$this->strikethrough($yes)} / ○ {$this->strikethrough($no)}" : "○ {$this->strikethrough($yes)} / ● {$this->strikethrough($no)}"); } return $prompt->confirmed ? "{$this->green('●')} {$yes} {$this->dim('/ ○ '.$no)}" : "{$this->dim('○ '.$yes.' /')} {$this->green('●')} {$no}"; } } prompts/src/Themes/Default/TextPromptRenderer.php 0000644 00000003330 15060132306 0016171 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\TextPrompt; class TextPromptRenderer extends Renderer { use Concerns\DrawsBoxes; /** * Render the text prompt. */ public function __invoke(TextPrompt $prompt): string { $maxWidth = $prompt->terminal()->cols() - 6; return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->truncate($prompt->value(), $maxWidth), ), 'cancel' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->strikethrough($this->dim($this->truncate($prompt->value() ?: $prompt->placeholder, $maxWidth))), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $prompt->valueWithCursor($maxWidth), color: 'yellow', ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $prompt->valueWithCursor($maxWidth), ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ) }; } } prompts/src/Themes/Default/SuggestPromptRenderer.php 0000644 00000010102 15060132306 0016661 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\SuggestPrompt; use Laravel\Prompts\Themes\Contracts\Scrolling; class SuggestPromptRenderer extends Renderer implements Scrolling { use Concerns\DrawsBoxes; use Concerns\DrawsScrollbars; /** * Render the suggest prompt. */ public function __invoke(SuggestPrompt $prompt): string { $maxWidth = $prompt->terminal()->cols() - 6; return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->truncate($prompt->value(), $maxWidth), ), 'cancel' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->strikethrough($this->dim($this->truncate($prompt->value() ?: $prompt->placeholder, $maxWidth))), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->valueWithCursorAndArrow($prompt, $maxWidth), $this->renderOptions($prompt), color: 'yellow', ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->valueWithCursorAndArrow($prompt, $maxWidth), $this->renderOptions($prompt), ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ) ->spaceForDropdown($prompt), }; } /** * Render the value with the cursor and an arrow. */ protected function valueWithCursorAndArrow(SuggestPrompt $prompt, int $maxWidth): string { if ($prompt->highlighted !== null || $prompt->value() !== '' || count($prompt->matches()) === 0) { return $prompt->valueWithCursor($maxWidth); } return preg_replace( '/\s$/', $this->cyan('⌄'), $this->pad($prompt->valueWithCursor($maxWidth - 1).' ', min($this->longest($prompt->matches(), padding: 2), $maxWidth)) ); } /** * Render a spacer to prevent jumping when the suggestions are displayed. */ protected function spaceForDropdown(SuggestPrompt $prompt): self { if ($prompt->value() === '' && $prompt->highlighted === null) { $this->newLine(min( count($prompt->matches()), $prompt->scroll, $prompt->terminal()->lines() - 7 ) + 1); } return $this; } /** * Render the options. */ protected function renderOptions(SuggestPrompt $prompt): string { if (empty($prompt->matches()) || ($prompt->value() === '' && $prompt->highlighted === null)) { return ''; } return $this->scrollbar( collect($prompt->visible()) ->map(fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 10)) ->map(fn ($label, $key) => $prompt->highlighted === $key ? "{$this->cyan('›')} {$label} " : " {$this->dim($label)} " ), $prompt->firstVisible, $prompt->scroll, count($prompt->matches()), min($this->longest($prompt->matches(), padding: 4), $prompt->terminal()->cols() - 6), $prompt->state === 'cancel' ? 'dim' : 'cyan' )->implode(PHP_EOL); } /** * The number of lines to reserve outside of the scrollable area. */ public function reservedLines(): int { return 7; } } prompts/src/Themes/Default/TableRenderer.php 0000644 00000002466 15060132306 0015103 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\Output\BufferedConsoleOutput; use Laravel\Prompts\Table; use Symfony\Component\Console\Helper\Table as SymfonyTable; use Symfony\Component\Console\Helper\TableStyle; class TableRenderer extends Renderer { /** * Render the table. */ public function __invoke(Table $table): string { $tableStyle = (new TableStyle()) ->setHorizontalBorderChars('─') ->setVerticalBorderChars('│', '│') ->setCellHeaderFormat($this->dim('<fg=default>%s</>')) ->setCellRowFormat('<fg=default>%s</>'); if (empty($table->headers)) { $tableStyle->setCrossingChars('┼', '', '', '', '┤', '┘</>', '┴', '└', '├', '<fg=gray>┌', '┬', '┐'); } else { $tableStyle->setCrossingChars('┼', '<fg=gray>┌', '┬', '┐', '┤', '┘</>', '┴', '└', '├'); } $buffered = new BufferedConsoleOutput(); (new SymfonyTable($buffered)) ->setHeaders($table->headers) ->setRows($table->rows) ->setStyle($tableStyle) ->render(); collect(explode(PHP_EOL, trim($buffered->content(), PHP_EOL))) ->each(fn ($line) => $this->line(' '.$line)); return $this; } } prompts/src/Themes/Default/MultiSelectPromptRenderer.php 0000644 00000010735 15060132306 0017506 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Default; use Laravel\Prompts\MultiSelectPrompt; use Laravel\Prompts\Themes\Contracts\Scrolling; class MultiSelectPromptRenderer extends Renderer implements Scrolling { use Concerns\DrawsBoxes; use Concerns\DrawsScrollbars; /** * Render the multiselect prompt. */ public function __invoke(MultiSelectPrompt $prompt): string { return match ($prompt->state) { 'submit' => $this ->box( $this->dim($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->renderSelectedOptions($prompt) ), 'cancel' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->renderOptions($prompt), color: 'red', ) ->error($prompt->cancelMessage), 'error' => $this ->box( $this->truncate($prompt->label, $prompt->terminal()->cols() - 6), $this->renderOptions($prompt), color: 'yellow', info: count($prompt->options) > $prompt->scroll ? (count($prompt->value()).' selected') : '', ) ->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), default => $this ->box( $this->cyan($this->truncate($prompt->label, $prompt->terminal()->cols() - 6)), $this->renderOptions($prompt), info: count($prompt->options) > $prompt->scroll ? (count($prompt->value()).' selected') : '', ) ->when( $prompt->hint, fn () => $this->hint($prompt->hint), fn () => $this->newLine() // Space for errors ), }; } /** * Render the options. */ protected function renderOptions(MultiSelectPrompt $prompt): string { return $this->scrollbar( collect($prompt->visible()) ->map(fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 12)) ->map(function ($label, $key) use ($prompt) { $index = array_search($key, array_keys($prompt->options)); $active = $index === $prompt->highlighted; if (array_is_list($prompt->options)) { $value = $prompt->options[$index]; } else { $value = array_keys($prompt->options)[$index]; } $selected = in_array($value, $prompt->value()); if ($prompt->state === 'cancel') { return $this->dim(match (true) { $active && $selected => "› ◼ {$this->strikethrough($label)} ", $active => "› ◻ {$this->strikethrough($label)} ", $selected => " ◼ {$this->strikethrough($label)} ", default => " ◻ {$this->strikethrough($label)} ", }); } return match (true) { $active && $selected => "{$this->cyan('› ◼')} {$label} ", $active => "{$this->cyan('›')} ◻ {$label} ", $selected => " {$this->cyan('◼')} {$this->dim($label)} ", default => " {$this->dim('◻')} {$this->dim($label)} ", }; }) ->values(), $prompt->firstVisible, $prompt->scroll, count($prompt->options), min($this->longest($prompt->options, padding: 6), $prompt->terminal()->cols() - 6), $prompt->state === 'cancel' ? 'dim' : 'cyan' )->implode(PHP_EOL); } /** * Render the selected options. */ protected function renderSelectedOptions(MultiSelectPrompt $prompt): string { if (count($prompt->labels()) === 0) { return $this->gray('None'); } return implode("\n", array_map( fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 6), $prompt->labels() )); } /** * The number of lines to reserve outside of the scrollable area. */ public function reservedLines(): int { return 5; } } prompts/src/Themes/Contracts/Scrolling.php 0000644 00000000314 15060132306 0014663 0 ustar 00 <?php namespace Laravel\Prompts\Themes\Contracts; interface Scrolling { /** * The number of lines to reserve outside of the scrollable area. */ public function reservedLines(): int; } prompts/src/SuggestPrompt.php 0000644 00000007666 15060132306 0012406 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; use Illuminate\Support\Collection; class SuggestPrompt extends Prompt { use Concerns\Scrolling; use Concerns\Truncation; use Concerns\TypedValue; /** * The options for the suggest prompt. * * @var array<string>|Closure(string): (array<string>|Collection<int, string>) */ public array|Closure $options; /** * The cache of matches. * * @var array<string>|null */ protected ?array $matches = null; /** * Create a new SuggestPrompt instance. * * @param array<string>|Collection<int, string>|Closure(string): (array<string>|Collection<int, string>) $options */ public function __construct( public string $label, array|Collection|Closure $options, public string $placeholder = '', public string $default = '', public int $scroll = 5, public bool|string $required = false, public mixed $validate = null, public string $hint = '', ) { $this->options = $options instanceof Collection ? $options->all() : $options; $this->initializeScrolling(null); $this->on('key', fn ($key) => match ($key) { Key::UP, Key::UP_ARROW, Key::SHIFT_TAB, Key::CTRL_P => $this->highlightPrevious(count($this->matches()), true), Key::DOWN, Key::DOWN_ARROW, Key::TAB, Key::CTRL_N => $this->highlightNext(count($this->matches()), true), Key::oneOf([Key::HOME, Key::CTRL_A], $key) => $this->highlighted !== null ? $this->highlight(0) : null, Key::oneOf([Key::END, Key::CTRL_E], $key) => $this->highlighted !== null ? $this->highlight(count($this->matches()) - 1) : null, Key::ENTER => $this->selectHighlighted(), Key::oneOf([Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::CTRL_B, Key::CTRL_F], $key) => $this->highlighted = null, default => (function () { $this->highlighted = null; $this->matches = null; $this->firstVisible = 0; })(), }); $this->trackTypedValue($default, ignore: fn ($key) => Key::oneOf([Key::HOME, Key::END, Key::CTRL_A, Key::CTRL_E], $key) && $this->highlighted !== null); } /** * Get the entered value with a virtual cursor. */ public function valueWithCursor(int $maxWidth): string { if ($this->highlighted !== null) { return $this->value() === '' ? $this->dim($this->truncate($this->placeholder, $maxWidth)) : $this->truncate($this->value(), $maxWidth); } if ($this->value() === '') { return $this->dim($this->addCursor($this->placeholder, 0, $maxWidth)); } return $this->addCursor($this->value(), $this->cursorPosition, $maxWidth); } /** * Get options that match the input. * * @return array<string> */ public function matches(): array { if (is_array($this->matches)) { return $this->matches; } if ($this->options instanceof Closure) { $matches = ($this->options)($this->value()); return $this->matches = array_values($matches instanceof Collection ? $matches->all() : $matches); } return $this->matches = array_values(array_filter($this->options, function ($option) { return str_starts_with(strtolower($option), strtolower($this->value())); })); } /** * The current visible matches. * * @return array<string> */ public function visible(): array { return array_slice($this->matches(), $this->firstVisible, $this->scroll, preserve_keys: true); } /** * Select the highlighted entry. */ protected function selectHighlighted(): void { if ($this->highlighted === null) { return; } $this->typedValue = $this->matches()[$this->highlighted]; } } prompts/src/Terminal.php 0000644 00000004671 15060132306 0011327 0 ustar 00 <?php namespace Laravel\Prompts; use ReflectionClass; use RuntimeException; use Symfony\Component\Console\Terminal as SymfonyTerminal; class Terminal { /** * The initial TTY mode. */ protected ?string $initialTtyMode; /** * The Symfony Terminal instance. */ protected SymfonyTerminal $terminal; /** * Create a new Terminal instance. */ public function __construct() { $this->terminal = new SymfonyTerminal(); } /** * Read a line from the terminal. */ public function read(): string { $input = fread(STDIN, 1024); return $input !== false ? $input : ''; } /** * Set the TTY mode. */ public function setTty(string $mode): void { $this->initialTtyMode ??= $this->exec('stty -g'); $this->exec("stty $mode"); } /** * Restore the initial TTY mode. */ public function restoreTty(): void { if (isset($this->initialTtyMode)) { $this->exec("stty {$this->initialTtyMode}"); $this->initialTtyMode = null; } } /** * Get the number of columns in the terminal. */ public function cols(): int { return $this->terminal->getWidth(); } /** * Get the number of lines in the terminal. */ public function lines(): int { return $this->terminal->getHeight(); } /** * (Re)initialize the terminal dimensions. */ public function initDimensions(): void { (new ReflectionClass($this->terminal)) ->getMethod('initDimensions') ->invoke($this->terminal); } /** * Exit the interactive session. */ public function exit(): void { exit(1); } /** * Execute the given command and return the output. */ protected function exec(string $command): string { $process = proc_open($command, [ 1 => ['pipe', 'w'], 2 => ['pipe', 'w'], ], $pipes); if (! $process) { throw new RuntimeException('Failed to create process.'); } $stdout = stream_get_contents($pipes[1]); $stderr = stream_get_contents($pipes[2]); $code = proc_close($process); if ($code !== 0 || $stdout === false) { throw new RuntimeException(trim($stderr ?: "Unknown error (code: $code)"), $code); } return $stdout; } } prompts/src/Output/BufferedConsoleOutput.php 0000644 00000001655 15060132306 0015341 0 ustar 00 <?php namespace Laravel\Prompts\Output; class BufferedConsoleOutput extends ConsoleOutput { /** * The output buffer. */ protected string $buffer = ''; /** * Empties the buffer and returns its content. */ public function fetch(): string { $content = $this->buffer; $this->buffer = ''; return $content; } /** * Return the content of the buffer. */ public function content(): string { return $this->buffer; } /** * Write to the output buffer. */ protected function doWrite(string $message, bool $newline): void { $this->buffer .= $message; if ($newline) { $this->buffer .= \PHP_EOL; } } /** * Write output directly, bypassing newline capture. */ public function writeDirectly(string $message): void { $this->doWrite($message, false); } } prompts/src/Output/ConsoleOutput.php 0000644 00000002241 15060132306 0013666 0 ustar 00 <?php namespace Laravel\Prompts\Output; use Symfony\Component\Console\Output\ConsoleOutput as SymfonyConsoleOutput; class ConsoleOutput extends SymfonyConsoleOutput { /** * How many new lines were written by the last output. */ protected int $newLinesWritten = 1; /** * How many new lines were written by the last output. */ public function newLinesWritten(): int { return $this->newLinesWritten; } /** * Write the output and capture the number of trailing new lines. */ protected function doWrite(string $message, bool $newline): void { parent::doWrite($message, $newline); if ($newline) { $message .= \PHP_EOL; } $trailingNewLines = strlen($message) - strlen(rtrim($message, \PHP_EOL)); if (trim($message) === '') { $this->newLinesWritten += $trailingNewLines; } else { $this->newLinesWritten = $trailingNewLines; } } /** * Write output directly, bypassing newline capture. */ public function writeDirectly(string $message): void { parent::doWrite($message, false); } } prompts/src/PasswordPrompt.php 0000644 00000001711 15060132306 0012550 0 ustar 00 <?php namespace Laravel\Prompts; class PasswordPrompt extends Prompt { use Concerns\TypedValue; /** * Create a new PasswordPrompt instance. */ public function __construct( public string $label, public string $placeholder = '', public bool|string $required = false, public mixed $validate = null, public string $hint = '', ) { $this->trackTypedValue(); } /** * Get a masked version of the entered value. */ public function masked(): string { return str_repeat('•', mb_strlen($this->value())); } /** * Get the masked value with a virtual cursor. */ public function maskedWithCursor(int $maxWidth): string { if ($this->value() === '') { return $this->dim($this->addCursor($this->placeholder, 0, $maxWidth)); } return $this->addCursor($this->masked(), $this->cursorPosition, $maxWidth); } } prompts/src/PausePrompt.php 0000644 00000001070 15060132306 0012021 0 ustar 00 <?php namespace Laravel\Prompts; class PausePrompt extends Prompt { /** * Create a new PausePrompt instance. */ public function __construct(public string $message = 'Press enter to continue...') { $this->required = false; $this->validate = null; $this->on('key', fn ($key) => match ($key) { Key::ENTER => $this->submit(), default => null, }); } /** * Get the value of the prompt. */ public function value(): bool { return static::$interactive; } } prompts/src/TextPrompt.php 0000644 00000001474 15060132306 0011700 0 ustar 00 <?php namespace Laravel\Prompts; class TextPrompt extends Prompt { use Concerns\TypedValue; /** * Create a new TextPrompt instance. */ public function __construct( public string $label, public string $placeholder = '', public string $default = '', public bool|string $required = false, public mixed $validate = null, public string $hint = '', ) { $this->trackTypedValue($default); } /** * Get the entered value with a virtual cursor. */ public function valueWithCursor(int $maxWidth): string { if ($this->value() === '') { return $this->dim($this->addCursor($this->placeholder, 0, $maxWidth)); } return $this->addCursor($this->value(), $this->cursorPosition, $maxWidth); } } prompts/src/helpers.php 0000644 00000017003 15060132306 0011207 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; use Illuminate\Support\Collection; if (! function_exists('\Laravel\Prompts\text')) { /** * Prompt the user for text input. */ function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, mixed $validate = null, string $hint = ''): string { return (new TextPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\textarea')) { /** * Prompt the user for multiline text input. */ function textarea(string $label, string $placeholder = '', string $default = '', bool|string $required = false, ?Closure $validate = null, string $hint = '', int $rows = 5): string { return (new TextareaPrompt($label, $placeholder, $default, $required, $validate, $hint, $rows))->prompt(); } } if (! function_exists('\Laravel\Prompts\password')) { /** * Prompt the user for input, hiding the value. */ function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = ''): string { return (new PasswordPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\select')) { /** * Prompt the user to select an option. * * @param array<int|string, string>|Collection<int|string, string> $options * @param true|string $required */ function select(string $label, array|Collection $options, int|string|null $default = null, int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true): int|string { return (new SelectPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\multiselect')) { /** * Prompt the user to select multiple options. * * @param array<int|string, string>|Collection<int|string, string> $options * @param array<int|string>|Collection<int, int|string> $default * @return array<int|string> */ function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.'): array { return (new MultiSelectPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\confirm')) { /** * Prompt the user to confirm an action. */ function confirm(string $label, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, mixed $validate = null, string $hint = ''): bool { return (new ConfirmPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\pause')) { /** * Prompt the user to continue or cancel after pausing. */ function pause(string $message = 'Press enter to continue...'): bool { return (new PausePrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\suggest')) { /** * Prompt the user for text input with auto-completion. * * @param array<string>|Collection<int, string>|Closure(string): array<string> $options */ function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = ''): string { return (new SuggestPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\search')) { /** * Allow the user to search for an option. * * @param Closure(string): array<int|string, string> $options * @param true|string $required */ function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true): int|string { return (new SearchPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\multisearch')) { /** * Allow the user to search for multiple option. * * @param Closure(string): array<int|string, string> $options * @return array<int|string> */ function multisearch(string $label, Closure $options, string $placeholder = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.'): array { return (new MultiSearchPrompt(...func_get_args()))->prompt(); } } if (! function_exists('\Laravel\Prompts\spin')) { /** * Render a spinner while the given callback is executing. * * @template TReturn of mixed * * @param \Closure(): TReturn $callback * @return TReturn */ function spin(Closure $callback, string $message = ''): mixed { return (new Spinner($message))->spin($callback); } } if (! function_exists('\Laravel\Prompts\note')) { /** * Display a note. */ function note(string $message, ?string $type = null): void { (new Note($message, $type))->display(); } } if (! function_exists('\Laravel\Prompts\error')) { /** * Display an error. */ function error(string $message): void { (new Note($message, 'error'))->display(); } } if (! function_exists('\Laravel\Prompts\warning')) { /** * Display a warning. */ function warning(string $message): void { (new Note($message, 'warning'))->display(); } } if (! function_exists('\Laravel\Prompts\alert')) { /** * Display an alert. */ function alert(string $message): void { (new Note($message, 'alert'))->display(); } } if (! function_exists('\Laravel\Prompts\info')) { /** * Display an informational message. */ function info(string $message): void { (new Note($message, 'info'))->display(); } } if (! function_exists('\Laravel\Prompts\intro')) { /** * Display an introduction. */ function intro(string $message): void { (new Note($message, 'intro'))->display(); } } if (! function_exists('\Laravel\Prompts\outro')) { /** * Display a closing message. */ function outro(string $message): void { (new Note($message, 'outro'))->display(); } } if (! function_exists('\Laravel\Prompts\table')) { /** * Display a table. * * @param array<int, string|array<int, string>>|Collection<int, string|array<int, string>> $headers * @param array<int, array<int, string>>|Collection<int, array<int, string>> $rows */ function table(array|Collection $headers = [], array|Collection|null $rows = null): void { (new Table($headers, $rows))->display(); } } if (! function_exists('\Laravel\Prompts\progress')) { /** * Display a progress bar. * * @template TSteps of iterable<mixed>|int * @template TReturn * * @param TSteps $steps * @param ?Closure((TSteps is int ? int : value-of<TSteps>), Progress<TSteps>): TReturn $callback * @return ($callback is null ? Progress<TSteps> : array<TReturn>) */ function progress(string $label, iterable|int $steps, ?Closure $callback = null, string $hint = ''): array|Progress { $progress = new Progress($label, $steps, $hint); if ($callback !== null) { return $progress->map($callback); } return $progress; } } if (! function_exists('\Laravel\Prompts\form')) { function form(): FormBuilder { return new FormBuilder(); } } prompts/src/Prompt.php 0000644 00000023016 15060132306 0011027 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; use Laravel\Prompts\Exceptions\FormRevertedException; use Laravel\Prompts\Output\ConsoleOutput; use RuntimeException; use Symfony\Component\Console\Output\OutputInterface; use Throwable; abstract class Prompt { use Concerns\Colors; use Concerns\Cursor; use Concerns\Erase; use Concerns\Events; use Concerns\FakesInputOutput; use Concerns\Fallback; use Concerns\Interactivity; use Concerns\Themes; /** * The current state of the prompt. */ public string $state = 'initial'; /** * The error message from the validator. */ public string $error = ''; /** * The cancel message displayed when this prompt is cancelled. */ public string $cancelMessage = 'Cancelled.'; /** * The previously rendered frame. */ protected string $prevFrame = ''; /** * How many new lines were written by the last output. */ protected int $newLinesWritten = 1; /** * Whether user input is required. */ public bool|string $required; /** * The validator callback or rules. */ public mixed $validate; /** * The cancellation callback. */ protected static ?Closure $cancelUsing; /** * Indicates if the prompt has been validated. */ protected bool $validated = false; /** * The custom validation callback. */ protected static ?Closure $validateUsing; /** * The revert handler from the StepBuilder. */ protected static ?Closure $revertUsing = null; /** * The output instance. */ protected static OutputInterface $output; /** * The terminal instance. */ protected static Terminal $terminal; /** * Get the value of the prompt. */ abstract public function value(): mixed; /** * Render the prompt and listen for input. */ public function prompt(): mixed { try { $this->capturePreviousNewLines(); if (static::shouldFallback()) { return $this->fallback(); } static::$interactive ??= stream_isatty(STDIN); if (! static::$interactive) { return $this->default(); } $this->checkEnvironment(); try { static::terminal()->setTty('-icanon -isig -echo'); } catch (Throwable $e) { static::output()->writeln("<comment>{$e->getMessage()}</comment>"); static::fallbackWhen(true); return $this->fallback(); } $this->hideCursor(); $this->render(); while (($key = static::terminal()->read()) !== null) { $continue = $this->handleKeyPress($key); $this->render(); if ($continue === false || $key === Key::CTRL_C) { if ($key === Key::CTRL_C) { if (isset(static::$cancelUsing)) { return (static::$cancelUsing)(); } else { static::terminal()->exit(); } } if ($key === Key::CTRL_U && self::$revertUsing) { throw new FormRevertedException(); } return $this->value(); } } } finally { $this->clearListeners(); } } /** * Register a callback to be invoked when a user cancels a prompt. */ public static function cancelUsing(?Closure $callback): void { static::$cancelUsing = $callback; } /** * How many new lines were written by the last output. */ public function newLinesWritten(): int { return $this->newLinesWritten; } /** * Capture the number of new lines written by the last output. */ protected function capturePreviousNewLines(): void { $this->newLinesWritten = method_exists(static::output(), 'newLinesWritten') ? static::output()->newLinesWritten() : 1; } /** * Set the output instance. */ public static function setOutput(OutputInterface $output): void { self::$output = $output; } /** * Get the current output instance. */ protected static function output(): OutputInterface { return self::$output ??= new ConsoleOutput(); } /** * Write output directly, bypassing newline capture. */ protected static function writeDirectly(string $message): void { match (true) { method_exists(static::output(), 'writeDirectly') => static::output()->writeDirectly($message), method_exists(static::output(), 'getOutput') => static::output()->getOutput()->write($message), default => static::output()->write($message), }; } /** * Get the terminal instance. */ public static function terminal(): Terminal { return static::$terminal ??= new Terminal(); } /** * Set the custom validation callback. */ public static function validateUsing(Closure $callback): void { static::$validateUsing = $callback; } /** * Revert the prompt using the given callback. * * @internal */ public static function revertUsing(Closure $callback): void { static::$revertUsing = $callback; } /** * Clear any previous revert callback. * * @internal */ public static function preventReverting(): void { static::$revertUsing = null; } /** * Render the prompt. */ protected function render(): void { $this->terminal()->initDimensions(); $frame = $this->renderTheme(); if ($frame === $this->prevFrame) { return; } if ($this->state === 'initial') { static::output()->write($frame); $this->state = 'active'; $this->prevFrame = $frame; return; } $terminalHeight = $this->terminal()->lines(); $previousFrameHeight = count(explode(PHP_EOL, $this->prevFrame)); $renderableLines = array_slice(explode(PHP_EOL, $frame), abs(min(0, $terminalHeight - $previousFrameHeight))); $this->moveCursorToColumn(1); $this->moveCursorUp(min($terminalHeight, $previousFrameHeight) - 1); $this->eraseDown(); $this->output()->write(implode(PHP_EOL, $renderableLines)); $this->prevFrame = $frame; } /** * Submit the prompt. */ protected function submit(): void { $this->validate($this->value()); if ($this->state !== 'error') { $this->state = 'submit'; } } /** * Handle a key press and determine whether to continue. */ private function handleKeyPress(string $key): bool { if ($this->state === 'error') { $this->state = 'active'; } $this->emit('key', $key); if ($this->state === 'submit') { return false; } if ($key === Key::CTRL_U) { if (! self::$revertUsing) { $this->state = 'error'; $this->error = 'This cannot be reverted.'; return true; } $this->state = 'cancel'; $this->cancelMessage = 'Reverted.'; call_user_func(self::$revertUsing); return false; } if ($key === Key::CTRL_C) { $this->state = 'cancel'; return false; } if ($this->validated) { $this->validate($this->value()); } return true; } /** * Validate the input. */ private function validate(mixed $value): void { $this->validated = true; if ($this->required !== false && $this->isInvalidWhenRequired($value)) { $this->state = 'error'; $this->error = is_string($this->required) && strlen($this->required) > 0 ? $this->required : 'Required.'; return; } if (! isset($this->validate) && ! isset(static::$validateUsing)) { return; } $error = match (true) { is_callable($this->validate) => ($this->validate)($value), isset(static::$validateUsing) => (static::$validateUsing)($this), default => throw new RuntimeException('The validation logic is missing.'), }; if (! is_string($error) && ! is_null($error)) { throw new RuntimeException('The validator must return a string or null.'); } if (is_string($error) && strlen($error) > 0) { $this->state = 'error'; $this->error = $error; } } /** * Determine whether the given value is invalid when the prompt is required. */ protected function isInvalidWhenRequired(mixed $value): bool { return $value === '' || $value === [] || $value === false || $value === null; } /** * Check whether the environment can support the prompt. */ private function checkEnvironment(): void { if (PHP_OS_FAMILY === 'Windows') { throw new RuntimeException('Prompts is not currently supported on Windows. Please use WSL or configure a fallback.'); } } /** * Restore the cursor and terminal state. */ public function __destruct() { $this->restoreCursor(); static::terminal()->restoreTty(); } } prompts/src/Table.php 0000644 00000003121 15060132306 0010570 0 ustar 00 <?php namespace Laravel\Prompts; use Illuminate\Support\Collection; class Table extends Prompt { /** * The table headers. * * @var array<int, string|array<int, string>> */ public array $headers; /** * The table rows. * * @var array<int, array<int, string>> */ public array $rows; /** * Create a new Table instance. * * @param array<int, string|array<int, string>>|Collection<int, string|array<int, string>> $headers * @param array<int, array<int, string>>|Collection<int, array<int, string>> $rows * * @phpstan-param ($rows is null ? list<list<string>>|Collection<int, list<string>> : list<string|list<string>>|Collection<int, string|list<string>>) $headers */ public function __construct(array|Collection $headers = [], array|Collection|null $rows = null) { if ($rows === null) { $rows = $headers; $headers = []; } $this->headers = $headers instanceof Collection ? $headers->all() : $headers; $this->rows = $rows instanceof Collection ? $rows->all() : $rows; } /** * Display the table. */ public function display(): void { $this->prompt(); } /** * Display the table. */ public function prompt(): bool { $this->capturePreviousNewLines(); $this->state = 'submit'; static::output()->write($this->renderTheme()); return true; } /** * Get the value of the prompt. */ public function value(): bool { return true; } } prompts/src/SearchPrompt.php 0000644 00000007767 15060132306 0012174 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; use InvalidArgumentException; class SearchPrompt extends Prompt { use Concerns\Scrolling; use Concerns\Truncation; use Concerns\TypedValue; /** * The cached matches. * * @var array<int|string, string>|null */ protected ?array $matches = null; /** * Create a new SearchPrompt instance. * * @param Closure(string): array<int|string, string> $options */ public function __construct( public string $label, public Closure $options, public string $placeholder = '', public int $scroll = 5, public mixed $validate = null, public string $hint = '', public bool|string $required = true, ) { if ($this->required === false) { throw new InvalidArgumentException('Argument [required] must be true or a string.'); } $this->trackTypedValue(submit: false, ignore: fn ($key) => Key::oneOf([Key::HOME, Key::END, Key::CTRL_A, Key::CTRL_E], $key) && $this->highlighted !== null); $this->initializeScrolling(null); $this->on('key', fn ($key) => match ($key) { Key::UP, Key::UP_ARROW, Key::SHIFT_TAB, Key::CTRL_P => $this->highlightPrevious(count($this->matches), true), Key::DOWN, Key::DOWN_ARROW, Key::TAB, Key::CTRL_N => $this->highlightNext(count($this->matches), true), Key::oneOf([Key::HOME, Key::CTRL_A], $key) => $this->highlighted !== null ? $this->highlight(0) : null, Key::oneOf([Key::END, Key::CTRL_E], $key) => $this->highlighted !== null ? $this->highlight(count($this->matches()) - 1) : null, Key::ENTER => $this->highlighted !== null ? $this->submit() : $this->search(), Key::oneOf([Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::CTRL_B, Key::CTRL_F], $key) => $this->highlighted = null, default => $this->search(), }); } /** * Perform the search. */ protected function search(): void { $this->state = 'searching'; $this->highlighted = null; $this->render(); $this->matches = null; $this->firstVisible = 0; $this->state = 'active'; } /** * Get the entered value with a virtual cursor. */ public function valueWithCursor(int $maxWidth): string { if ($this->highlighted !== null) { return $this->typedValue === '' ? $this->dim($this->truncate($this->placeholder, $maxWidth)) : $this->truncate($this->typedValue, $maxWidth); } if ($this->typedValue === '') { return $this->dim($this->addCursor($this->placeholder, 0, $maxWidth)); } return $this->addCursor($this->typedValue, $this->cursorPosition, $maxWidth); } /** * Get options that match the input. * * @return array<string> */ public function matches(): array { if (is_array($this->matches)) { return $this->matches; } return $this->matches = ($this->options)($this->typedValue); } /** * The currently visible matches. * * @return array<string> */ public function visible(): array { return array_slice($this->matches(), $this->firstVisible, $this->scroll, preserve_keys: true); } /** * Get the current search query. */ public function searchValue(): string { return $this->typedValue; } /** * Get the selected value. */ public function value(): int|string|null { if ($this->matches === null || $this->highlighted === null) { return null; } return array_is_list($this->matches) ? $this->matches[$this->highlighted] : array_keys($this->matches)[$this->highlighted]; } /** * Get the selected label. */ public function label(): ?string { return $this->matches[array_keys($this->matches)[$this->highlighted]] ?? null; } } prompts/src/FormBuilder.php 0000644 00000022046 15060132306 0011762 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; use Illuminate\Support\Collection; use Laravel\Prompts\Exceptions\FormRevertedException; class FormBuilder { /** * Each step that should be executed. * * @var array<int, \Laravel\Prompts\FormStep> */ protected array $steps = []; /** * The responses provided by each step. * * @var array<mixed> */ protected array $responses = []; /** * Add a new step. */ public function add(Closure $step, ?string $name = null, bool $ignoreWhenReverting = false): self { $this->steps[] = new FormStep($step, true, $name, $ignoreWhenReverting); return $this; } /** * Run all of the given steps. * * @return array<mixed> */ public function submit(): array { $index = 0; $wasReverted = false; while ($index < count($this->steps)) { $step = $this->steps[$index]; if ($wasReverted && $index > 0 && $step->shouldIgnoreWhenReverting($this->responses)) { $index--; continue; } $wasReverted = false; $index > 0 ? Prompt::revertUsing(function () use (&$wasReverted) { $wasReverted = true; }) : Prompt::preventReverting(); try { $this->responses[$step->name ?? $index] = $step->run( $this->responses, $this->responses[$step->name ?? $index] ?? null, ); } catch (FormRevertedException) { $wasReverted = true; } $wasReverted ? $index-- : $index++; } Prompt::preventReverting(); return $this->responses; } /** * Prompt the user for text input. */ public function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self { return $this->runPrompt(text(...), get_defined_vars()); } /** * Prompt the user for multiline text input. */ public function textarea(string $label, string $placeholder = '', string $default = '', bool|string $required = false, ?Closure $validate = null, string $hint = '', int $rows = 5, ?string $name = null): self { return $this->runPrompt(textarea(...), get_defined_vars()); } /** * Prompt the user for input, hiding the value. */ public function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self { return $this->runPrompt(password(...), get_defined_vars()); } /** * Prompt the user to select an option. * * @param array<int|string, string>|Collection<int|string, string> $options * @param true|string $required */ public function select(string $label, array|Collection $options, int|string|null $default = null, int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null): self { return $this->runPrompt(select(...), get_defined_vars()); } /** * Prompt the user to select multiple options. * * @param array<int|string, string>|Collection<int|string, string> $options * @param array<int|string>|Collection<int, int|string> $default */ public function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null): self { return $this->runPrompt(multiselect(...), get_defined_vars()); } /** * Prompt the user to confirm an action. */ public function confirm(string $label, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self { return $this->runPrompt(confirm(...), get_defined_vars()); } /** * Prompt the user to continue or cancel after pausing. */ public function pause(string $message = 'Press enter to continue...', ?string $name = null): self { return $this->runPrompt(pause(...), get_defined_vars()); } /** * Prompt the user for text input with auto-completion. * * @param array<string>|Collection<int, string>|Closure(string): array<string> $options */ public function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self { return $this->runPrompt(suggest(...), get_defined_vars()); } /** * Allow the user to search for an option. * * @param Closure(string): array<int|string, string> $options * @param true|string $required */ public function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null): self { return $this->runPrompt(search(...), get_defined_vars()); } /** * Allow the user to search for multiple option. * * @param Closure(string): array<int|string, string> $options */ public function multisearch(string $label, Closure $options, string $placeholder = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null): self { return $this->runPrompt(multisearch(...), get_defined_vars()); } /** * Render a spinner while the given callback is executing. * * @param \Closure(): mixed $callback */ public function spin(Closure $callback, string $message = '', ?string $name = null): self { return $this->runPrompt(spin(...), get_defined_vars(), true); } /** * Display a note. */ public function note(string $message, ?string $type = null, ?string $name = null): self { return $this->runPrompt(note(...), get_defined_vars(), true); } /** * Display an error. */ public function error(string $message, ?string $name = null): self { return $this->runPrompt(error(...), get_defined_vars(), true); } /** * Display a warning. */ public function warning(string $message, ?string $name = null): self { return $this->runPrompt(warning(...), get_defined_vars(), true); } /** * Display an alert. */ public function alert(string $message, ?string $name = null): self { return $this->runPrompt(alert(...), get_defined_vars(), true); } /** * Display an informational message. */ public function info(string $message, ?string $name = null): self { return $this->runPrompt(info(...), get_defined_vars(), true); } /** * Display an introduction. */ public function intro(string $message, ?string $name = null): self { return $this->runPrompt(intro(...), get_defined_vars(), true); } /** * Display a closing message. */ public function outro(string $message, ?string $name = null): self { return $this->runPrompt(outro(...), get_defined_vars(), true); } /** * Display a table. * * @param array<int, string|array<int, string>>|Collection<int, string|array<int, string>> $headers * @param array<int, array<int, string>>|Collection<int, array<int, string>> $rows */ public function table(array|Collection $headers = [], array|Collection|null $rows = null, ?string $name = null): self { return $this->runPrompt(table(...), get_defined_vars(), true); } /** * Display a progress bar. * * @template TSteps of iterable<mixed>|int * @template TReturn * * @param TSteps $steps * @param ?Closure((TSteps is int ? int : value-of<TSteps>), Progress<TSteps>): TReturn $callback */ public function progress(string $label, iterable|int $steps, ?Closure $callback = null, string $hint = '', ?string $name = null): self { return $this->runPrompt(progress(...), get_defined_vars(), true); } /** * Execute the given prompt passing the given arguments. * * @param array<mixed> $arguments */ protected function runPrompt(callable $prompt, array $arguments, bool $ignoreWhenReverting = false): self { return $this->add(function (array $responses, mixed $previousResponse) use ($prompt, $arguments) { unset($arguments['name']); if (array_key_exists('default', $arguments) && $previousResponse !== null) { $arguments['default'] = $previousResponse; } return $prompt(...$arguments); }, name: $arguments['name'], ignoreWhenReverting: $ignoreWhenReverting); } } prompts/src/Progress.php 0000644 00000010725 15060132306 0011355 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; use InvalidArgumentException; use RuntimeException; use Throwable; /** * @template TSteps of iterable<mixed>|int */ class Progress extends Prompt { /** * The current progress bar item count. */ public int $progress = 0; /** * The total number of steps. */ public int $total = 0; /** * The original value of pcntl_async_signals */ protected bool $originalAsync; /** * Create a new ProgressBar instance. * * @param TSteps $steps */ public function __construct(public string $label, public iterable|int $steps, public string $hint = '') { $this->total = match (true) { // @phpstan-ignore assign.propertyType is_int($this->steps) => $this->steps, is_countable($this->steps) => count($this->steps), is_iterable($this->steps) => iterator_count($this->steps), default => throw new InvalidArgumentException('Unable to count steps.'), }; if ($this->total === 0) { throw new InvalidArgumentException('Progress bar must have at least one item.'); } } /** * Map over the steps while rendering the progress bar. * * @template TReturn * * @param Closure((TSteps is int ? int : value-of<TSteps>), $this): TReturn $callback * @return array<TReturn> */ public function map(Closure $callback): array { $this->start(); $result = []; try { if (is_int($this->steps)) { for ($i = 0; $i < $this->steps; $i++) { $result[] = $callback($i, $this); $this->advance(); } } else { foreach ($this->steps as $step) { $result[] = $callback($step, $this); $this->advance(); } } } catch (Throwable $e) { $this->state = 'error'; $this->render(); $this->restoreCursor(); $this->resetSignals(); throw $e; } if ($this->hint !== '') { // Just pause for one moment to show the final hint // so it doesn't look like it was skipped usleep(250_000); } $this->finish(); return $result; } /** * Start the progress bar. */ public function start(): void { $this->capturePreviousNewLines(); if (function_exists('pcntl_signal')) { $this->originalAsync = pcntl_async_signals(true); pcntl_signal(SIGINT, function () { $this->state = 'cancel'; $this->render(); exit(); }); } $this->state = 'active'; $this->hideCursor(); $this->render(); } /** * Advance the progress bar. */ public function advance(int $step = 1): void { $this->progress += $step; if ($this->progress > $this->total) { $this->progress = $this->total; } $this->render(); } /** * Finish the progress bar. */ public function finish(): void { $this->state = 'submit'; $this->render(); $this->restoreCursor(); $this->resetSignals(); } /** * Update the label. */ public function label(string $label): static { $this->label = $label; return $this; } /** * Update the hint. */ public function hint(string $hint): static { $this->hint = $hint; return $this; } /** * Get the completion percentage. */ public function percentage(): int|float { return $this->progress / $this->total; } /** * Disable prompting for input. * * @throws \RuntimeException */ public function prompt(): never { throw new RuntimeException('Progress Bar cannot be prompted.'); } /** * Get the value of the prompt. */ public function value(): bool { return true; } /** * Reset the signal handling. */ protected function resetSignals(): void { if (isset($this->originalAsync)) { pcntl_async_signals($this->originalAsync); pcntl_signal(SIGINT, SIG_DFL); } } /** * Restore the cursor. */ public function __destruct() { $this->restoreCursor(); } } prompts/src/Spinner.php 0000644 00000006371 15060132306 0011171 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; use RuntimeException; class Spinner extends Prompt { /** * How long to wait between rendering each frame. */ public int $interval = 100; /** * The number of times the spinner has been rendered. */ public int $count = 0; /** * Whether the spinner can only be rendered once. */ public bool $static = false; /** * The process ID after forking. */ protected int $pid; /** * Create a new Spinner instance. */ public function __construct(public string $message = '') { // } /** * Render the spinner and execute the callback. * * @template TReturn of mixed * * @param \Closure(): TReturn $callback * @return TReturn */ public function spin(Closure $callback): mixed { $this->capturePreviousNewLines(); if (! function_exists('pcntl_fork')) { return $this->renderStatically($callback); } $originalAsync = pcntl_async_signals(true); pcntl_signal(SIGINT, fn () => exit()); try { $this->hideCursor(); $this->render(); $this->pid = pcntl_fork(); if ($this->pid === 0) { while (true) { // @phpstan-ignore-line $this->render(); $this->count++; usleep($this->interval * 1000); } } else { $result = $callback(); $this->resetTerminal($originalAsync); return $result; } } catch (\Throwable $e) { $this->resetTerminal($originalAsync); throw $e; } } /** * Reset the terminal. */ protected function resetTerminal(bool $originalAsync): void { pcntl_async_signals($originalAsync); pcntl_signal(SIGINT, SIG_DFL); $this->eraseRenderedLines(); } /** * Render a static version of the spinner. * * @template TReturn of mixed * * @param \Closure(): TReturn $callback * @return TReturn */ protected function renderStatically(Closure $callback): mixed { $this->static = true; try { $this->hideCursor(); $this->render(); $result = $callback(); } finally { $this->eraseRenderedLines(); } return $result; } /** * Disable prompting for input. * * @throws \RuntimeException */ public function prompt(): never { throw new RuntimeException('Spinner cannot be prompted.'); } /** * Get the current value of the prompt. */ public function value(): bool { return true; } /** * Clear the lines rendered by the spinner. */ protected function eraseRenderedLines(): void { $lines = explode(PHP_EOL, $this->prevFrame); $this->moveCursor(-999, -count($lines) + 1); $this->eraseDown(); } /** * Clean up after the spinner. */ public function __destruct() { if (! empty($this->pid)) { posix_kill($this->pid, SIGHUP); } parent::__destruct(); } } prompts/src/MultiSearchPrompt.php 0000644 00000014114 15060132306 0013167 0 ustar 00 <?php namespace Laravel\Prompts; use Closure; class MultiSearchPrompt extends Prompt { use Concerns\Scrolling; use Concerns\Truncation; use Concerns\TypedValue; /** * The cached matches. * * @var array<int|string, string>|null */ protected ?array $matches = null; /** * Whether the matches are initially a list. */ protected bool $isList; /** * The selected values. * * @var array<int|string, string> */ public array $values = []; /** * Create a new MultiSearchPrompt instance. * * @param Closure(string): array<int|string, string> $options */ public function __construct( public string $label, public Closure $options, public string $placeholder = '', public int $scroll = 5, public bool|string $required = false, public mixed $validate = null, public string $hint = '', ) { $this->trackTypedValue(submit: false, ignore: fn ($key) => Key::oneOf([Key::SPACE, Key::HOME, Key::END, Key::CTRL_A, Key::CTRL_E], $key) && $this->highlighted !== null); $this->initializeScrolling(null); $this->on('key', fn ($key) => match ($key) { Key::UP, Key::UP_ARROW, Key::SHIFT_TAB => $this->highlightPrevious(count($this->matches), true), Key::DOWN, Key::DOWN_ARROW, Key::TAB => $this->highlightNext(count($this->matches), true), Key::oneOf(Key::HOME, $key) => $this->highlighted !== null ? $this->highlight(0) : null, Key::oneOf(Key::END, $key) => $this->highlighted !== null ? $this->highlight(count($this->matches()) - 1) : null, Key::SPACE => $this->highlighted !== null ? $this->toggleHighlighted() : null, Key::CTRL_A => $this->highlighted !== null ? $this->toggleAll() : null, Key::CTRL_E => null, Key::ENTER => $this->submit(), Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW => $this->highlighted = null, default => $this->search(), }); } /** * Perform the search. */ protected function search(): void { $this->state = 'searching'; $this->highlighted = null; $this->render(); $this->matches = null; $this->firstVisible = 0; $this->state = 'active'; } /** * Get the entered value with a virtual cursor. */ public function valueWithCursor(int $maxWidth): string { if ($this->highlighted !== null) { return $this->typedValue === '' ? $this->dim($this->truncate($this->placeholder, $maxWidth)) : $this->truncate($this->typedValue, $maxWidth); } if ($this->typedValue === '') { return $this->dim($this->addCursor($this->placeholder, 0, $maxWidth)); } return $this->addCursor($this->typedValue, $this->cursorPosition, $maxWidth); } /** * Get options that match the input. * * @return array<string> */ public function matches(): array { if (is_array($this->matches)) { return $this->matches; } $matches = ($this->options)($this->typedValue); if (! isset($this->isList) && count($matches) > 0) { // This needs to be captured the first time we receive matches so // we know what we're dealing with later if matches is empty. $this->isList = array_is_list($matches); } if (! isset($this->isList)) { return $this->matches = []; } if (strlen($this->typedValue) > 0) { return $this->matches = $matches; } return $this->matches = $this->isList ? [...array_diff(array_values($this->values), $matches), ...$matches] : array_diff($this->values, $matches) + $matches; } /** * The currently visible matches * * @return array<string> */ public function visible(): array { return array_slice($this->matches(), $this->firstVisible, $this->scroll, preserve_keys: true); } /** * Toggle all options. */ protected function toggleAll(): void { $allMatchesSelected = collect($this->matches)->every(fn ($label, $key) => $this->isList() ? array_key_exists($label, $this->values) : array_key_exists($key, $this->values)); if ($allMatchesSelected) { $this->values = array_filter($this->values, fn ($value) => $this->isList() ? ! in_array($value, $this->matches) : ! array_key_exists(array_search($value, $this->matches), $this->matches) ); } else { $this->values = $this->isList() ? array_merge($this->values, array_combine(array_values($this->matches), array_values($this->matches))) : array_merge($this->values, array_combine(array_keys($this->matches), array_values($this->matches))); } } /** * Toggle the highlighted entry. */ protected function toggleHighlighted(): void { if ($this->isList()) { $label = $this->matches[$this->highlighted]; $key = $label; } else { $key = array_keys($this->matches)[$this->highlighted]; $label = $this->matches[$key]; } if (array_key_exists($key, $this->values)) { unset($this->values[$key]); } else { $this->values[$key] = $label; } } /** * Get the current search query. */ public function searchValue(): string { return $this->typedValue; } /** * Get the selected value. * * @return array<int|string> */ public function value(): array { return array_keys($this->values); } /** * Get the selected labels. * * @return array<string> */ public function labels(): array { return array_values($this->values); } /** * Whether the matches are initially a list. */ public function isList(): bool { return $this->isList; } } prompts/src/SelectPrompt.php 0000644 00000006312 15060132306 0012167 0 ustar 00 <?php namespace Laravel\Prompts; use Illuminate\Support\Collection; use InvalidArgumentException; class SelectPrompt extends Prompt { use Concerns\Scrolling; /** * The options for the select prompt. * * @var array<int|string, string> */ public array $options; /** * Create a new SelectPrompt instance. * * @param array<int|string, string>|Collection<int|string, string> $options */ public function __construct( public string $label, array|Collection $options, public int|string|null $default = null, public int $scroll = 5, public mixed $validate = null, public string $hint = '', public bool|string $required = true, ) { if ($this->required === false) { throw new InvalidArgumentException('Argument [required] must be true or a string.'); } $this->options = $options instanceof Collection ? $options->all() : $options; if ($this->default) { if (array_is_list($this->options)) { $this->initializeScrolling(array_search($this->default, $this->options) ?: 0); } else { $this->initializeScrolling(array_search($this->default, array_keys($this->options)) ?: 0); } $this->scrollToHighlighted(count($this->options)); } else { $this->initializeScrolling(0); } $this->on('key', fn ($key) => match ($key) { Key::UP, Key::UP_ARROW, Key::LEFT, Key::LEFT_ARROW, Key::SHIFT_TAB, Key::CTRL_P, Key::CTRL_B, 'k', 'h' => $this->highlightPrevious(count($this->options)), Key::DOWN, Key::DOWN_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::TAB, Key::CTRL_N, Key::CTRL_F, 'j', 'l' => $this->highlightNext(count($this->options)), Key::oneOf([Key::HOME, Key::CTRL_A], $key) => $this->highlight(0), Key::oneOf([Key::END, Key::CTRL_E], $key) => $this->highlight(count($this->options) - 1), Key::ENTER => $this->submit(), default => null, }); } /** * Get the selected value. */ public function value(): int|string|null { if (static::$interactive === false) { return $this->default; } if (array_is_list($this->options)) { return $this->options[$this->highlighted] ?? null; } else { return array_keys($this->options)[$this->highlighted]; } } /** * Get the selected label. */ public function label(): ?string { if (array_is_list($this->options)) { return $this->options[$this->highlighted] ?? null; } else { return $this->options[array_keys($this->options)[$this->highlighted]] ?? null; } } /** * The currently visible options. * * @return array<int|string, string> */ public function visible(): array { return array_slice($this->options, $this->firstVisible, $this->scroll, preserve_keys: true); } /** * Determine whether the given value is invalid when the prompt is required. */ protected function isInvalidWhenRequired(mixed $value): bool { return $value === null; } } prompts/src/Exceptions/NonInteractiveValidationException.php 0000644 00000000221 15060132306 0020502 0 ustar 00 <?php namespace Laravel\Prompts\Exceptions; use RuntimeException; class NonInteractiveValidationException extends RuntimeException { // } prompts/src/Exceptions/FormRevertedException.php 0000644 00000000205 15060132306 0016145 0 ustar 00 <?php namespace Laravel\Prompts\Exceptions; use RuntimeException; class FormRevertedException extends RuntimeException { // } prompts/LICENSE.md 0000644 00000002063 15060132306 0007651 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. prompts/phpunit.xml 0000644 00000001050 15060132306 0010451 0 ustar 00 <?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" backupStaticProperties="true"> <testsuites> <testsuite name="Test Suite"> <directory suffix="Test.php">./tests</directory> </testsuite> </testsuites> <source> <include> <directory suffix=".php">./app</directory> <directory suffix=".php">./src</directory> </include> </source> </phpunit> prompts/composer.json 0000644 00000002310 15060132306 0010762 0 ustar 00 { "name": "laravel/prompts", "type": "library", "description": "Add beautiful and user-friendly forms to your command-line applications.", "license": "MIT", "autoload": { "psr-4": { "Laravel\\Prompts\\": "src/" }, "files": [ "src/helpers.php" ] }, "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }, "require": { "php": "^8.1", "ext-mbstring": "*", "illuminate/collections": "^10.0|^11.0", "symfony/console": "^6.2|^7.0" }, "require-dev": { "phpstan/phpstan": "^1.11", "pestphp/pest": "^2.3", "mockery/mockery": "^1.5", "phpstan/phpstan-mockery": "^1.1" }, "conflict": { "illuminate/console": ">=10.17.0 <10.25.0", "laravel/framework": ">=10.17.0 <10.25.0" }, "suggest": { "ext-pcntl": "Required for the spinner to be animated." }, "config": { "allow-plugins": { "pestphp/pest-plugin": true } }, "extra": { "branch-alias": { "dev-main": "0.1.x-dev" } }, "prefer-stable": true, "minimum-stability": "dev" } prompts/README.md 0000644 00000003526 15060132306 0007531 0 ustar 00 <p align="center"><img width="386" height="68" src="/art/logo.svg" alt="Laravel Prompts"></p> <p align="center"> <a href="https://github.com/laravel/prompts/actions"><img src="https://github.com/laravel/prompts/workflows/tests/badge.svg" alt="Build Status"></a> <a href="https://packagist.org/packages/laravel/prompts"><img src="https://img.shields.io/packagist/dt/laravel/prompts" alt="Total Downloads"></a> <a href="https://packagist.org/packages/laravel/prompts"><img src="https://img.shields.io/packagist/v/laravel/prompts" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/laravel/prompts"><img src="https://img.shields.io/packagist/l/laravel/prompts" alt="License"></a> </p> ## Introduction Laravel Prompts is a PHP package for adding beautiful and user-friendly forms to your command-line applications, with browser-like features including placeholder text and validation. Laravel Prompts is perfect for accepting user input in your [Artisan console commands](https://laravel.com/docs/artisan#writing-commands), but it may also be used in any command-line PHP project. ## Official Documentation Documentation for Laravel Prompts can be found on the [Laravel website](https://laravel.com/docs/prompts). ## Contributing Thank you for considering contributing to Laravel Prompts! 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/prompts/security/policy) on how to report security vulnerabilities. ## License Laravel Prompts is open-sourced software licensed under the [MIT license](LICENSE.md). socialite/src/SocialiteServiceProvider.php 0000644 00000001247 15060132306 0014770 0 ustar 00 <?php namespace Laravel\Socialite; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; use Laravel\Socialite\Contracts\Factory; class SocialiteServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton(Factory::class, function ($app) { return new SocialiteManager($app); }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return [Factory::class]; } } socialite/src/SocialiteManager.php 0000644 00000016274 15060132306 0013235 0 ustar 00 <?php namespace Laravel\Socialite; use Illuminate\Support\Arr; use Illuminate\Support\Manager; use Illuminate\Support\Str; use InvalidArgumentException; use Laravel\Socialite\One\TwitterProvider; use Laravel\Socialite\Two\BitbucketProvider; use Laravel\Socialite\Two\FacebookProvider; use Laravel\Socialite\Two\GithubProvider; use Laravel\Socialite\Two\GitlabProvider; use Laravel\Socialite\Two\GoogleProvider; use Laravel\Socialite\Two\LinkedInOpenIdProvider; use Laravel\Socialite\Two\LinkedInProvider; use Laravel\Socialite\Two\SlackOpenIdProvider; use Laravel\Socialite\Two\SlackProvider; use Laravel\Socialite\Two\TwitterProvider as TwitterOAuth2Provider; use League\OAuth1\Client\Server\Twitter as TwitterServer; class SocialiteManager extends Manager implements Contracts\Factory { /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application * * @deprecated Will be removed in a future Socialite release. */ protected $app; /** * Get a driver instance. * * @param string $driver * @return mixed */ public function with($driver) { return $this->driver($driver); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createGithubDriver() { $config = $this->config->get('services.github'); return $this->buildProvider( GithubProvider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createFacebookDriver() { $config = $this->config->get('services.facebook'); return $this->buildProvider( FacebookProvider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createGoogleDriver() { $config = $this->config->get('services.google'); return $this->buildProvider( GoogleProvider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createLinkedinDriver() { $config = $this->config->get('services.linkedin'); return $this->buildProvider( LinkedInProvider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createLinkedinOpenidDriver() { $config = $this->config->get('services.linkedin-openid'); return $this->buildProvider( LinkedInOpenIdProvider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createBitbucketDriver() { $config = $this->config->get('services.bitbucket'); return $this->buildProvider( BitbucketProvider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createGitlabDriver() { $config = $this->config->get('services.gitlab'); return $this->buildProvider( GitlabProvider::class, $config )->setHost($config['host'] ?? null); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider */ protected function createTwitterDriver() { $config = $this->config->get('services.twitter'); if (($config['oauth'] ?? null) === 2) { return $this->createTwitterOAuth2Driver(); } return new TwitterProvider( $this->container->make('request'), new TwitterServer($this->formatConfig($config)) ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createTwitterOAuth2Driver() { $config = $this->config->get('services.twitter') ?? $this->config->get('services.twitter-oauth-2'); return $this->buildProvider( TwitterOAuth2Provider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createSlackDriver() { $config = $this->config->get('services.slack'); return $this->buildProvider( SlackProvider::class, $config ); } /** * Create an instance of the specified driver. * * @return \Laravel\Socialite\Two\AbstractProvider */ protected function createSlackOpenidDriver() { $config = $this->config->get('services.slack-openid'); return $this->buildProvider( SlackOpenIdProvider::class, $config ); } /** * Build an OAuth 2 provider instance. * * @param string $provider * @param array $config * @return \Laravel\Socialite\Two\AbstractProvider */ public function buildProvider($provider, $config) { return new $provider( $this->container->make('request'), $config['client_id'], $config['client_secret'], $this->formatRedirectUrl($config), Arr::get($config, 'guzzle', []) ); } /** * Format the server configuration. * * @param array $config * @return array */ public function formatConfig(array $config) { return array_merge([ 'identifier' => $config['client_id'], 'secret' => $config['client_secret'], 'callback_uri' => $this->formatRedirectUrl($config), ], $config); } /** * Format the callback URL, resolving a relative URI if needed. * * @param array $config * @return string */ protected function formatRedirectUrl(array $config) { $redirect = value($config['redirect']); return Str::startsWith($redirect ?? '', '/') ? $this->container->make('url')->to($redirect) : $redirect; } /** * Forget all of the resolved driver instances. * * @return $this */ public function forgetDrivers() { $this->drivers = []; return $this; } /** * Set the container instance used by the manager. * * @param \Illuminate\Contracts\Container\Container $container * @return $this */ public function setContainer($container) { $this->app = $container; $this->container = $container; $this->config = $container->make('config'); return $this; } /** * Get the default driver name. * * @return string * * @throws \InvalidArgumentException */ public function getDefaultDriver() { throw new InvalidArgumentException('No Socialite driver was specified.'); } } socialite/src/Two/SlackProvider.php 0000644 00000005137 15060132306 0013343 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; class SlackProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['identity.basic', 'identity.email', 'identity.team', 'identity.avatar']; /** * The key used for scopes. * * @var string */ protected $scopeKey = 'user_scope'; /** * Indicate that the requested token should be for a bot user. * * @return $this */ public function asBotUser() { $this->scopeKey = 'scope'; return $this; } /** * {@inheritdoc} */ public function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://slack.com/oauth/v2/authorize', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://slack.com/api/oauth.v2.access'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $response = $this->getHttpClient()->get('https://slack.com/api/users.identity', [ RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token], ]); return json_decode($response->getBody(), true); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { return (new User)->setRaw($user)->map([ 'id' => Arr::get($user, 'user.id'), 'name' => Arr::get($user, 'user.name'), 'email' => Arr::get($user, 'user.email'), 'avatar' => Arr::get($user, 'user.image_512'), 'organization_id' => Arr::get($user, 'team.id'), ]); } /** * {@inheritdoc} */ protected function getCodeFields($state = null) { $fields = parent::getCodeFields($state); if ($this->scopeKey === 'user_scope') { $fields['scope'] = ''; $fields['user_scope'] = $this->formatScopes($this->scopes, $this->scopeSeparator); } return $fields; } /** * {@inheritdoc} */ public function getAccessTokenResponse($code) { $response = $this->getHttpClient()->post($this->getTokenUrl(), [ RequestOptions::HEADERS => $this->getTokenHeaders($code), RequestOptions::FORM_PARAMS => $this->getTokenFields($code), ]); $result = json_decode($response->getBody(), true); if ($this->scopeKey === 'user_scope') { return $result['authed_user']; } return $result; } } socialite/src/Two/TwitterProvider.php 0000644 00000006041 15060132306 0013743 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; class TwitterProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['users.read', 'tweet.read']; /** * Indicates if PKCE should be used. * * @var bool */ protected $usesPKCE = true; /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ' '; /** * The query encoding format. * * @var int */ protected $encodingType = PHP_QUERY_RFC3986; /** * {@inheritdoc} */ public function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://twitter.com/i/oauth2/authorize', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://api.twitter.com/2/oauth2/token'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $response = $this->getHttpClient()->get('https://api.twitter.com/2/users/me', [ RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token], RequestOptions::QUERY => ['user.fields' => 'profile_image_url'], ]); return Arr::get(json_decode($response->getBody(), true), 'data'); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { return (new User)->setRaw($user)->map([ 'id' => $user['id'], 'nickname' => $user['username'], 'name' => $user['name'], 'avatar' => $user['profile_image_url'], ]); } /** * {@inheritdoc} */ public function getAccessTokenResponse($code) { $response = $this->getHttpClient()->post($this->getTokenUrl(), [ RequestOptions::HEADERS => ['Accept' => 'application/json'], RequestOptions::AUTH => [$this->clientId, $this->clientSecret], RequestOptions::FORM_PARAMS => $this->getTokenFields($code), ]); return json_decode($response->getBody(), true); } /** * {@inheritdoc} */ protected function getRefreshTokenResponse($refreshToken) { $response = $this->getHttpClient()->post($this->getTokenUrl(), [ RequestOptions::HEADERS => ['Accept' => 'application/json'], RequestOptions::AUTH => [$this->clientId, $this->clientSecret], RequestOptions::FORM_PARAMS => [ 'grant_type' => 'refresh_token', 'refresh_token' => $refreshToken, 'client_id' => $this->clientId, ], ]); return json_decode($response->getBody(), true); } /** * {@inheritdoc} */ protected function getCodeFields($state = null) { $fields = parent::getCodeFields($state); if ($this->isStateless()) { $fields['state'] = 'state'; } return $fields; } } socialite/src/Two/SlackOpenIdProvider.php 0000644 00000003100 15060132306 0014426 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; class SlackOpenIdProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['openid', 'email', 'profile']; /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ' '; /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://slack.com/openid/connect/authorize', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://slack.com/api/openid.connect.token'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $response = $this->getHttpClient()->get('https://slack.com/api/openid.connect.userInfo', [ RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token], ]); return json_decode($response->getBody(), true); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { return (new User)->setRaw($user)->map([ 'id' => Arr::get($user, 'sub'), 'nickname' => null, 'name' => Arr::get($user, 'name'), 'email' => Arr::get($user, 'email'), 'avatar' => Arr::get($user, 'picture'), 'organization_id' => Arr::get($user, 'https://slack.com/team_id'), ]); } } socialite/src/Two/InvalidStateException.php 0000644 00000000220 15060132306 0015025 0 ustar 00 <?php namespace Laravel\Socialite\Two; use InvalidArgumentException; class InvalidStateException extends InvalidArgumentException { // } socialite/src/Two/User.php 0000644 00000003232 15060132306 0011503 0 ustar 00 <?php namespace Laravel\Socialite\Two; use Laravel\Socialite\AbstractUser; class User extends AbstractUser { /** * The user's access token. * * @var string */ public $token; /** * The refresh token that can be exchanged for a new access token. * * @var string */ public $refreshToken; /** * The number of seconds the access token is valid for. * * @var int */ public $expiresIn; /** * The scopes the user authorized. The approved scopes may be a subset of the requested scopes. * * @var array */ public $approvedScopes; /** * Set the token on the user. * * @param string $token * @return $this */ public function setToken($token) { $this->token = $token; return $this; } /** * Set the refresh token required to obtain a new access token. * * @param string $refreshToken * @return $this */ public function setRefreshToken($refreshToken) { $this->refreshToken = $refreshToken; return $this; } /** * Set the number of seconds the access token is valid for. * * @param int $expiresIn * @return $this */ public function setExpiresIn($expiresIn) { $this->expiresIn = $expiresIn; return $this; } /** * Set the scopes that were approved by the user during authentication. * * @param array $approvedScopes * @return $this */ public function setApprovedScopes($approvedScopes) { $this->approvedScopes = $approvedScopes; return $this; } } socialite/src/Two/GoogleProvider.php 0000644 00000004735 15060132306 0013525 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; class GoogleProvider extends AbstractProvider implements ProviderInterface { /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ' '; /** * The scopes being requested. * * @var array */ protected $scopes = [ 'openid', 'profile', 'email', ]; /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://www.googleapis.com/oauth2/v4/token'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $response = $this->getHttpClient()->get('https://www.googleapis.com/oauth2/v3/userinfo', [ RequestOptions::QUERY => [ 'prettyPrint' => 'false', ], RequestOptions::HEADERS => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer '.$token, ], ]); return json_decode((string) $response->getBody(), true); } /** * {@inheritdoc} */ public function refreshToken($refreshToken) { $response = $this->getRefreshTokenResponse($refreshToken); return new Token( Arr::get($response, 'access_token'), Arr::get($response, 'refresh_token', $refreshToken), Arr::get($response, 'expires_in'), explode($this->scopeSeparator, Arr::get($response, 'scope', '')) ); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { // Deprecated: Fields added to keep backwards compatibility in 4.0. These will be removed in 5.0 $user['id'] = Arr::get($user, 'sub'); $user['verified_email'] = Arr::get($user, 'email_verified'); $user['link'] = Arr::get($user, 'profile'); return (new User)->setRaw($user)->map([ 'id' => Arr::get($user, 'sub'), 'nickname' => Arr::get($user, 'nickname'), 'name' => Arr::get($user, 'name'), 'email' => Arr::get($user, 'email'), 'avatar' => $avatarUrl = Arr::get($user, 'picture'), 'avatar_original' => $avatarUrl, ]); } } socialite/src/Two/AbstractProvider.php 0000644 00000032211 15060132306 0014042 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\Client; use GuzzleHttp\RequestOptions; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Laravel\Socialite\Contracts\Provider as ProviderContract; abstract class AbstractProvider implements ProviderContract { /** * The HTTP request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * The HTTP Client instance. * * @var \GuzzleHttp\Client */ protected $httpClient; /** * The client ID. * * @var string */ protected $clientId; /** * The client secret. * * @var string */ protected $clientSecret; /** * The redirect URL. * * @var string */ protected $redirectUrl; /** * The custom parameters to be sent with the request. * * @var array */ protected $parameters = []; /** * The scopes being requested. * * @var array */ protected $scopes = []; /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ','; /** * The type of the encoding in the query. * * @var int Can be either PHP_QUERY_RFC3986 or PHP_QUERY_RFC1738. */ protected $encodingType = PHP_QUERY_RFC1738; /** * Indicates if the session state should be utilized. * * @var bool */ protected $stateless = false; /** * Indicates if PKCE should be used. * * @var bool */ protected $usesPKCE = false; /** * The custom Guzzle configuration options. * * @var array */ protected $guzzle = []; /** * The cached user instance. * * @var \Laravel\Socialite\Two\User|null */ protected $user; /** * Create a new provider instance. * * @param \Illuminate\Http\Request $request * @param string $clientId * @param string $clientSecret * @param string $redirectUrl * @param array $guzzle * @return void */ public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl, $guzzle = []) { $this->guzzle = $guzzle; $this->request = $request; $this->clientId = $clientId; $this->redirectUrl = $redirectUrl; $this->clientSecret = $clientSecret; } /** * Get the authentication URL for the provider. * * @param string $state * @return string */ abstract protected function getAuthUrl($state); /** * Get the token URL for the provider. * * @return string */ abstract protected function getTokenUrl(); /** * Get the raw user for the given access token. * * @param string $token * @return array */ abstract protected function getUserByToken($token); /** * Map the raw user array to a Socialite User instance. * * @param array $user * @return \Laravel\Socialite\Two\User */ abstract protected function mapUserToObject(array $user); /** * Redirect the user of the application to the provider's authentication screen. * * @return \Illuminate\Http\RedirectResponse */ public function redirect() { $state = null; if ($this->usesState()) { $this->request->session()->put('state', $state = $this->getState()); } if ($this->usesPKCE()) { $this->request->session()->put('code_verifier', $this->getCodeVerifier()); } return new RedirectResponse($this->getAuthUrl($state)); } /** * Build the authentication URL for the provider from the given base URL. * * @param string $url * @param string $state * @return string */ protected function buildAuthUrlFromBase($url, $state) { return $url.'?'.http_build_query($this->getCodeFields($state), '', '&', $this->encodingType); } /** * Get the GET parameters for the code request. * * @param string|null $state * @return array */ protected function getCodeFields($state = null) { $fields = [ 'client_id' => $this->clientId, 'redirect_uri' => $this->redirectUrl, 'scope' => $this->formatScopes($this->getScopes(), $this->scopeSeparator), 'response_type' => 'code', ]; if ($this->usesState()) { $fields['state'] = $state; } if ($this->usesPKCE()) { $fields['code_challenge'] = $this->getCodeChallenge(); $fields['code_challenge_method'] = $this->getCodeChallengeMethod(); } return array_merge($fields, $this->parameters); } /** * Format the given scopes. * * @param array $scopes * @param string $scopeSeparator * @return string */ protected function formatScopes(array $scopes, $scopeSeparator) { return implode($scopeSeparator, $scopes); } /** * {@inheritdoc} */ public function user() { if ($this->user) { return $this->user; } if ($this->hasInvalidState()) { throw new InvalidStateException; } $response = $this->getAccessTokenResponse($this->getCode()); $user = $this->getUserByToken(Arr::get($response, 'access_token')); return $this->userInstance($response, $user); } /** * Create a user instance from the given data. * * @param array $response * @param array $user * @return \Laravel\Socialite\Two\User */ protected function userInstance(array $response, array $user) { $this->user = $this->mapUserToObject($user); return $this->user->setToken(Arr::get($response, 'access_token')) ->setRefreshToken(Arr::get($response, 'refresh_token')) ->setExpiresIn(Arr::get($response, 'expires_in')) ->setApprovedScopes(explode($this->scopeSeparator, Arr::get($response, 'scope', ''))); } /** * Get a Social User instance from a known access token. * * @param string $token * @return \Laravel\Socialite\Two\User */ public function userFromToken($token) { $user = $this->mapUserToObject($this->getUserByToken($token)); return $user->setToken($token); } /** * Determine if the current request / session has a mismatching "state". * * @return bool */ protected function hasInvalidState() { if ($this->isStateless()) { return false; } $state = $this->request->session()->pull('state'); return empty($state) || $this->request->input('state') !== $state; } /** * Get the access token response for the given code. * * @param string $code * @return array */ public function getAccessTokenResponse($code) { $response = $this->getHttpClient()->post($this->getTokenUrl(), [ RequestOptions::HEADERS => $this->getTokenHeaders($code), RequestOptions::FORM_PARAMS => $this->getTokenFields($code), ]); return json_decode($response->getBody(), true); } /** * Get the headers for the access token request. * * @param string $code * @return array */ protected function getTokenHeaders($code) { return ['Accept' => 'application/json']; } /** * Get the POST fields for the token request. * * @param string $code * @return array */ protected function getTokenFields($code) { $fields = [ 'grant_type' => 'authorization_code', 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'code' => $code, 'redirect_uri' => $this->redirectUrl, ]; if ($this->usesPKCE()) { $fields['code_verifier'] = $this->request->session()->pull('code_verifier'); } return $fields; } /** * Refresh a user's access token with a refresh token. * * @param string $refreshToken * @return \Laravel\Socialite\Two\Token */ public function refreshToken($refreshToken) { $response = $this->getRefreshTokenResponse($refreshToken); return new Token( Arr::get($response, 'access_token'), Arr::get($response, 'refresh_token'), Arr::get($response, 'expires_in'), explode($this->scopeSeparator, Arr::get($response, 'scope', '')) ); } /** * Get the refresh token response for the given refresh token. * * @param string $refreshToken * @return array */ protected function getRefreshTokenResponse($refreshToken) { return json_decode($this->getHttpClient()->post($this->getTokenUrl(), [ RequestOptions::HEADERS => ['Accept' => 'application/json'], RequestOptions::FORM_PARAMS => [ 'grant_type' => 'refresh_token', 'refresh_token' => $refreshToken, 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, ], ])->getBody(), true); } /** * Get the code from the request. * * @return string */ protected function getCode() { return $this->request->input('code'); } /** * Merge the scopes of the requested access. * * @param array|string $scopes * @return $this */ public function scopes($scopes) { $this->scopes = array_unique(array_merge($this->scopes, (array) $scopes)); return $this; } /** * Set the scopes of the requested access. * * @param array|string $scopes * @return $this */ public function setScopes($scopes) { $this->scopes = array_unique((array) $scopes); return $this; } /** * Get the current scopes. * * @return array */ public function getScopes() { return $this->scopes; } /** * Set the redirect URL. * * @param string $url * @return $this */ public function redirectUrl($url) { $this->redirectUrl = $url; return $this; } /** * Get a instance of the Guzzle HTTP client. * * @return \GuzzleHttp\Client */ protected function getHttpClient() { if (is_null($this->httpClient)) { $this->httpClient = new Client($this->guzzle); } return $this->httpClient; } /** * Set the Guzzle HTTP client instance. * * @param \GuzzleHttp\Client $client * @return $this */ public function setHttpClient(Client $client) { $this->httpClient = $client; return $this; } /** * Set the request instance. * * @param \Illuminate\Http\Request $request * @return $this */ public function setRequest(Request $request) { $this->request = $request; return $this; } /** * Determine if the provider is operating with state. * * @return bool */ protected function usesState() { return ! $this->stateless; } /** * Determine if the provider is operating as stateless. * * @return bool */ protected function isStateless() { return $this->stateless; } /** * Indicates that the provider should operate as stateless. * * @return $this */ public function stateless() { $this->stateless = true; return $this; } /** * Get the string used for session state. * * @return string */ protected function getState() { return Str::random(40); } /** * Determine if the provider uses PKCE. * * @return bool */ protected function usesPKCE() { return $this->usesPKCE; } /** * Enables PKCE for the provider. * * @return $this */ public function enablePKCE() { $this->usesPKCE = true; return $this; } /** * Generates a random string of the right length for the PKCE code verifier. * * @return string */ protected function getCodeVerifier() { return Str::random(96); } /** * Generates the PKCE code challenge based on the PKCE code verifier in the session. * * @return string */ protected function getCodeChallenge() { $hashed = hash('sha256', $this->request->session()->get('code_verifier'), true); return rtrim(strtr(base64_encode($hashed), '+/', '-_'), '='); } /** * Returns the hash method used to calculate the PKCE code challenge. * * @return string */ protected function getCodeChallengeMethod() { return 'S256'; } /** * Set the custom parameters of the request. * * @param array $parameters * @return $this */ public function with(array $parameters) { $this->parameters = $parameters; return $this; } } socialite/src/Two/LinkedInProvider.php 0000644 00000010225 15060132306 0013775 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; class LinkedInProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['r_liteprofile', 'r_emailaddress']; /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ' '; /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://www.linkedin.com/oauth/v2/authorization', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://www.linkedin.com/oauth/v2/accessToken'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $basicProfile = $this->getBasicProfile($token); $emailAddress = $this->getEmailAddress($token); return array_merge($basicProfile, $emailAddress); } /** * Get the basic profile fields for the user. * * @param string $token * @return array */ protected function getBasicProfile($token) { $fields = ['id', 'firstName', 'lastName', 'profilePicture(displayImage~:playableStreams)']; if (in_array('r_liteprofile', $this->getScopes())) { array_push($fields, 'vanityName'); } $response = $this->getHttpClient()->get('https://api.linkedin.com/v2/me', [ RequestOptions::HEADERS => [ 'Authorization' => 'Bearer '.$token, 'X-RestLi-Protocol-Version' => '2.0.0', ], RequestOptions::QUERY => [ 'projection' => '('.implode(',', $fields).')', ], ]); return (array) json_decode($response->getBody(), true); } /** * Get the email address for the user. * * @param string $token * @return array */ protected function getEmailAddress($token) { $response = $this->getHttpClient()->get('https://api.linkedin.com/v2/emailAddress', [ RequestOptions::HEADERS => [ 'Authorization' => 'Bearer '.$token, 'X-RestLi-Protocol-Version' => '2.0.0', ], RequestOptions::QUERY => [ 'q' => 'members', 'projection' => '(elements*(handle~))', ], ]); return (array) Arr::get((array) json_decode($response->getBody(), true), 'elements.0.handle~'); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { $preferredLocale = Arr::get($user, 'firstName.preferredLocale.language').'_'.Arr::get($user, 'firstName.preferredLocale.country'); $firstName = Arr::get($user, 'firstName.localized.'.$preferredLocale); $lastName = Arr::get($user, 'lastName.localized.'.$preferredLocale); $images = (array) Arr::get($user, 'profilePicture.displayImage~.elements', []); $avatar = Arr::first($images, function ($image) { return ( $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] ?? $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['displaySize']['width'] ) === 100; }); $originalAvatar = Arr::first($images, function ($image) { return ( $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] ?? $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['displaySize']['width'] ) === 800; }); return (new User)->setRaw($user)->map([ 'id' => $user['id'], 'nickname' => null, 'name' => $firstName.' '.$lastName, 'first_name' => $firstName, 'last_name' => $lastName, 'email' => Arr::get($user, 'emailAddress'), 'avatar' => Arr::get($avatar, 'identifiers.0.identifier'), 'avatar_original' => Arr::get($originalAvatar, 'identifiers.0.identifier'), ]); } } socialite/src/Two/Token.php 0000644 00000002047 15060132306 0011650 0 ustar 00 <?php namespace Laravel\Socialite\Two; class Token { /** * The user's access token. * * @var string */ public $token; /** * The refresh token that can be exchanged for a new access token. * * @var string */ public $refreshToken; /** * The number of seconds the access token is valid for. * * @var int */ public $expiresIn; /** * The scopes the user authorized. The approved scopes may be a subset of the requested scopes. * * @var array */ public $approvedScopes; /** * Create a new token instance. * * @param string $token * @param string $refreshToken * @param int $expiresIn * @param array $approvedScopes */ public function __construct(string $token, string $refreshToken, int $expiresIn, array $approvedScopes) { $this->token = $token; $this->refreshToken = $refreshToken; $this->expiresIn = $expiresIn; $this->approvedScopes = $approvedScopes; } } socialite/src/Two/GitlabProvider.php 0000644 00000003353 15060132306 0013506 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\RequestOptions; class GitlabProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['read_user']; /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ' '; /** * The Gitlab instance host. * * @var string */ protected $host = 'https://gitlab.com'; /** * Set the Gitlab instance host. * * @param string|null $host * @return $this */ public function setHost($host) { if (! empty($host)) { $this->host = rtrim($host, '/'); } return $this; } /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase($this->host.'/oauth/authorize', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return $this->host.'/oauth/token'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $response = $this->getHttpClient()->get($this->host.'/api/v3/user', [ RequestOptions::QUERY => ['access_token' => $token], ]); return json_decode($response->getBody(), true); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { return (new User)->setRaw($user)->map([ 'id' => $user['id'], 'nickname' => $user['username'], 'name' => $user['name'], 'email' => $user['email'], 'avatar' => $user['avatar_url'], ]); } } socialite/src/Two/FacebookProvider.php 0000644 00000014645 15060132306 0014023 0 ustar 00 <?php namespace Laravel\Socialite\Two; use Exception; use Firebase\JWT\JWT; use Firebase\JWT\Key; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; use phpseclib3\Crypt\RSA; use phpseclib3\Math\BigInteger; class FacebookProvider extends AbstractProvider implements ProviderInterface { /** * The base Facebook Graph URL. * * @var string */ protected $graphUrl = 'https://graph.facebook.com'; /** * The Graph API version for the request. * * @var string */ protected $version = 'v3.3'; /** * The user fields being requested. * * @var array */ protected $fields = ['name', 'email', 'gender', 'verified', 'link']; /** * The scopes being requested. * * @var array */ protected $scopes = ['email']; /** * Display the dialog in a popup view. * * @var bool */ protected $popup = false; /** * Re-request a declined permission. * * @var bool */ protected $reRequest = false; /** * The access token that was last used to retrieve a user. * * @var string|null */ protected $lastToken; /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://www.facebook.com/'.$this->version.'/dialog/oauth', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return $this->graphUrl.'/'.$this->version.'/oauth/access_token'; } /** * {@inheritdoc} */ public function getAccessTokenResponse($code) { $response = $this->getHttpClient()->post($this->getTokenUrl(), [ RequestOptions::FORM_PARAMS => $this->getTokenFields($code), ]); $data = json_decode($response->getBody(), true); return Arr::add($data, 'expires_in', Arr::pull($data, 'expires')); } /** * {@inheritdoc} */ protected function getUserByToken($token) { $this->lastToken = $token; return $this->getUserByOIDCToken($token) ?? $this->getUserFromAccessToken($token); } /** * Get user based on the OIDC token. * * @param string $token * @return array */ protected function getUserByOIDCToken($token) { $kid = json_decode(base64_decode(explode('.', $token)[0]), true)['kid'] ?? null; if ($kid === null) { return null; } $data = (array) JWT::decode($token, $this->getPublicKeyOfOIDCToken($kid)); throw_if($data['aud'] !== $this->clientId, new Exception('Token has incorrect audience.')); throw_if($data['iss'] !== 'https://www.facebook.com', new Exception('Token has incorrect issuer.')); $data['id'] = $data['sub']; $data['first_name'] = $data['given_name']; $data['last_name'] = $data['family_name']; return $data; } /** * Get the public key to verify the signature of OIDC token. * * @param string $id * @return \Firebase\JWT\Key */ protected function getPublicKeyOfOIDCToken(string $kid) { $response = $this->getHttpClient()->get('https://limited.facebook.com/.well-known/oauth/openid/jwks/'); $key = Arr::first(json_decode($response->getBody()->getContents(), true)['keys'], function ($key) use ($kid) { return $key['kid'] === $kid; }); $key['n'] = new BigInteger(JWT::urlsafeB64Decode($key['n']), 256); $key['e'] = new BigInteger(JWT::urlsafeB64Decode($key['e']), 256); return new Key((string) RSA::load($key), 'RS256'); } /** * Get user based on the access token. * * @param string $token * @return array */ protected function getUserFromAccessToken($token) { $params = [ 'access_token' => $token, 'fields' => implode(',', $this->fields), ]; if (! empty($this->clientSecret)) { $params['appsecret_proof'] = hash_hmac('sha256', $token, $this->clientSecret); } $response = $this->getHttpClient()->get($this->graphUrl.'/'.$this->version.'/me', [ RequestOptions::HEADERS => [ 'Accept' => 'application/json', ], RequestOptions::QUERY => $params, ]); return json_decode($response->getBody(), true); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { if (! isset($user['sub'])) { $avatarUrl = $this->graphUrl.'/'.$this->version.'/'.$user['id'].'/picture'; $avatarOriginalUrl = $avatarUrl.'?width=1920'; } return (new User)->setRaw($user)->map([ 'id' => $user['id'], 'nickname' => null, 'name' => $user['name'] ?? null, 'email' => $user['email'] ?? null, 'avatar' => $avatarUrl ?? $user['picture'] ?? null, 'avatar_original' => $avatarOriginalUrl ?? $user['picture'] ?? null, 'profileUrl' => $user['link'] ?? null, ]); } /** * {@inheritdoc} */ protected function getCodeFields($state = null) { $fields = parent::getCodeFields($state); if ($this->popup) { $fields['display'] = 'popup'; } if ($this->reRequest) { $fields['auth_type'] = 'rerequest'; } return $fields; } /** * Set the user fields to request from Facebook. * * @param array $fields * @return $this */ public function fields(array $fields) { $this->fields = $fields; return $this; } /** * Set the dialog to be displayed as a popup. * * @return $this */ public function asPopup() { $this->popup = true; return $this; } /** * Re-request permissions which were previously declined. * * @return $this */ public function reRequest() { $this->reRequest = true; return $this; } /** * Get the last access token used. * * @return string|null */ public function lastToken() { return $this->lastToken; } /** * Specify which graph version should be used. * * @param string $version * @return $this */ public function usingGraphVersion(string $version) { $this->version = $version; return $this; } } socialite/src/Two/ProviderInterface.php 0000644 00000000640 15060132306 0014200 0 ustar 00 <?php namespace Laravel\Socialite\Two; interface ProviderInterface { /** * Redirect the user to the authentication page for the provider. * * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function redirect(); /** * Get the User instance for the authenticated user. * * @return \Laravel\Socialite\Two\User */ public function user(); } socialite/src/Two/BitbucketProvider.php 0000644 00000005537 15060132306 0014226 0 ustar 00 <?php namespace Laravel\Socialite\Two; use Exception; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; class BitbucketProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['email']; /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ' '; /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://bitbucket.org/site/oauth2/authorize', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://bitbucket.org/site/oauth2/access_token'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $response = $this->getHttpClient()->get('https://api.bitbucket.org/2.0/user', [ RequestOptions::QUERY => ['access_token' => $token], ]); $user = json_decode($response->getBody(), true); if (in_array('email', $this->scopes, true)) { $user['email'] = $this->getEmailByToken($token); } return $user; } /** * Get the email for the given access token. * * @param string $token * @return string|null */ protected function getEmailByToken($token) { $emailsUrl = 'https://api.bitbucket.org/2.0/user/emails?access_token='.$token; try { $response = $this->getHttpClient()->get($emailsUrl); } catch (Exception $e) { return; } $emails = json_decode($response->getBody(), true); foreach ($emails['values'] as $email) { if ($email['type'] === 'email' && $email['is_primary'] && $email['is_confirmed']) { return $email['email']; } } } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { return (new User)->setRaw($user)->map([ 'id' => $user['uuid'], 'nickname' => $user['username'], 'name' => Arr::get($user, 'display_name'), 'email' => Arr::get($user, 'email'), 'avatar' => Arr::get($user, 'links.avatar.href'), ]); } /** * Get the access token for the given code. * * @param string $code * @return string */ public function getAccessToken($code) { $response = $this->getHttpClient()->post($this->getTokenUrl(), [ RequestOptions::AUTH => [$this->clientId, $this->clientSecret], RequestOptions::HEADERS => ['Accept' => 'application/json'], RequestOptions::FORM_PARAMS => $this->getTokenFields($code), ]); return json_decode($response->getBody(), true)['access_token']; } } socialite/src/Two/GithubProvider.php 0000644 00000005021 15060132306 0013520 0 ustar 00 <?php namespace Laravel\Socialite\Two; use Exception; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; class GithubProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['user:email']; /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://github.com/login/oauth/authorize', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://github.com/login/oauth/access_token'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { $userUrl = 'https://api.github.com/user'; $response = $this->getHttpClient()->get( $userUrl, $this->getRequestOptions($token) ); $user = json_decode($response->getBody(), true); if (in_array('user:email', $this->scopes, true)) { $user['email'] = $this->getEmailByToken($token); } return $user; } /** * Get the email for the given access token. * * @param string $token * @return string|null */ protected function getEmailByToken($token) { $emailsUrl = 'https://api.github.com/user/emails'; try { $response = $this->getHttpClient()->get( $emailsUrl, $this->getRequestOptions($token) ); } catch (Exception $e) { return; } foreach (json_decode($response->getBody(), true) as $email) { if ($email['primary'] && $email['verified']) { return $email['email']; } } } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { return (new User)->setRaw($user)->map([ 'id' => $user['id'], 'nodeId' => $user['node_id'], 'nickname' => $user['login'], 'name' => Arr::get($user, 'name'), 'email' => Arr::get($user, 'email'), 'avatar' => $user['avatar_url'], ]); } /** * Get the default options for an HTTP request. * * @param string $token * @return array */ protected function getRequestOptions($token) { return [ RequestOptions::HEADERS => [ 'Accept' => 'application/vnd.github.v3+json', 'Authorization' => 'token '.$token, ], ]; } } socialite/src/Two/LinkedInOpenIdProvider.php 0000644 00000004053 15060132306 0015076 0 ustar 00 <?php namespace Laravel\Socialite\Two; use GuzzleHttp\RequestOptions; class LinkedInOpenIdProvider extends AbstractProvider implements ProviderInterface { /** * The scopes being requested. * * @var array */ protected $scopes = ['openid', 'profile', 'email']; /** * The separating character for the requested scopes. * * @var string */ protected $scopeSeparator = ' '; /** * {@inheritdoc} */ protected function getAuthUrl($state) { return $this->buildAuthUrlFromBase('https://www.linkedin.com/oauth/v2/authorization', $state); } /** * {@inheritdoc} */ protected function getTokenUrl() { return 'https://www.linkedin.com/oauth/v2/accessToken'; } /** * {@inheritdoc} */ protected function getUserByToken($token) { return $this->getBasicProfile($token); } /** * Get the basic profile fields for the user. * * @param string $token * @return array */ protected function getBasicProfile($token) { $response = $this->getHttpClient()->get('https://api.linkedin.com/v2/userinfo', [ RequestOptions::HEADERS => [ 'Authorization' => 'Bearer '.$token, 'X-RestLi-Protocol-Version' => '2.0.0', ], RequestOptions::QUERY => [ 'projection' => '(sub,email,name,given_name,family_name,picture)', ], ]); return (array) json_decode($response->getBody(), true); } /** * {@inheritdoc} */ protected function mapUserToObject(array $user) { return (new User)->setRaw($user)->map([ 'id' => $user['sub'], 'nickname' => null, 'name' => $user['name'], 'first_name' => $user['given_name'], 'last_name' => $user['family_name'], 'email' => $user['email'] ?? null, 'avatar' => $user['picture'] ?? null, 'avatar_original' => $user['picture'] ?? null, ]); } } socialite/src/Facades/Socialite.php 0000644 00000001513 15060132306 0013256 0 ustar 00 <?php namespace Laravel\Socialite\Facades; use Illuminate\Support\Facades\Facade; use Laravel\Socialite\Contracts\Factory; /** * @method static \Laravel\Socialite\Contracts\Provider driver(string $driver = null) * @method static \Laravel\Socialite\Two\AbstractProvider buildProvider($provider, $config) * @method array getScopes() * @method \Laravel\Socialite\Contracts\Provider scopes(array|string $scopes) * @method \Laravel\Socialite\Contracts\Provider setScopes(array|string $scopes) * @method \Laravel\Socialite\Contracts\Provider redirectUrl(string $url) * * @see \Laravel\Socialite\SocialiteManager */ class Socialite extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return Factory::class; } } socialite/src/One/TwitterProvider.php 0000644 00000002466 15060132306 0013722 0 ustar 00 <?php namespace Laravel\Socialite\One; class TwitterProvider extends AbstractProvider { /** * {@inheritdoc} */ public function user() { if (! $this->hasNecessaryVerifier()) { throw new MissingVerifierException('Invalid request. Missing OAuth verifier.'); } $user = $this->server->getUserDetails($token = $this->getToken(), $this->shouldBypassCache($token->getIdentifier(), $token->getSecret())); $extraDetails = [ 'location' => $user->location, 'description' => $user->description, ]; $instance = (new User)->setRaw(array_merge($user->extra, $user->urls, $extraDetails)) ->setToken($token->getIdentifier(), $token->getSecret()); return $instance->map([ 'id' => $user->uid, 'nickname' => $user->nickname, 'name' => $user->name, 'email' => $user->email, 'avatar' => $user->imageUrl, 'avatar_original' => str_replace('_normal', '', $user->imageUrl), ]); } /** * Set the access level the application should request to the user account. * * @param string $scope * @return void */ public function scope(string $scope) { $this->server->setApplicationScope($scope); } } socialite/src/One/User.php 0000644 00000001153 15060132306 0011453 0 ustar 00 <?php namespace Laravel\Socialite\One; use Laravel\Socialite\AbstractUser; class User extends AbstractUser { /** * The user's access token. * * @var string */ public $token; /** * The user's access token secret. * * @var string */ public $tokenSecret; /** * Set the token on the user. * * @param string $token * @param string $tokenSecret * @return $this */ public function setToken($token, $tokenSecret) { $this->token = $token; $this->tokenSecret = $tokenSecret; return $this; } } socialite/src/One/AbstractProvider.php 0000644 00000011345 15060132306 0014017 0 ustar 00 <?php namespace Laravel\Socialite\One; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Laravel\Socialite\Contracts\Provider as ProviderContract; use League\OAuth1\Client\Credentials\TokenCredentials; use League\OAuth1\Client\Server\Server; abstract class AbstractProvider implements ProviderContract { /** * The HTTP request instance. * * @var \Illuminate\Http\Request */ protected $request; /** * The OAuth server implementation. * * @var \League\OAuth1\Client\Server\Server */ protected $server; /** * A hash representing the last requested user. * * @var string */ protected $userHash; /** * Create a new provider instance. * * @param \Illuminate\Http\Request $request * @param \League\OAuth1\Client\Server\Server $server * @return void */ public function __construct(Request $request, Server $server) { $this->server = $server; $this->request = $request; } /** * Redirect the user to the authentication page for the provider. * * @return \Illuminate\Http\RedirectResponse */ public function redirect() { $this->request->session()->put( 'oauth.temp', $temp = $this->server->getTemporaryCredentials() ); return new RedirectResponse($this->server->getAuthorizationUrl($temp)); } /** * Get the User instance for the authenticated user. * * @return \Laravel\Socialite\One\User * * @throws \Laravel\Socialite\One\MissingVerifierException */ public function user() { if (! $this->hasNecessaryVerifier()) { throw new MissingVerifierException('Invalid request. Missing OAuth verifier.'); } $token = $this->getToken(); $user = $this->server->getUserDetails( $token, $this->shouldBypassCache($token->getIdentifier(), $token->getSecret()) ); $instance = (new User)->setRaw($user->extra) ->setToken($token->getIdentifier(), $token->getSecret()); return $instance->map([ 'id' => $user->uid, 'nickname' => $user->nickname, 'name' => $user->name, 'email' => $user->email, 'avatar' => $user->imageUrl, ]); } /** * Get a Social User instance from a known access token and secret. * * @param string $token * @param string $secret * @return \Laravel\Socialite\One\User */ public function userFromTokenAndSecret($token, $secret) { $tokenCredentials = new TokenCredentials(); $tokenCredentials->setIdentifier($token); $tokenCredentials->setSecret($secret); $user = $this->server->getUserDetails( $tokenCredentials, $this->shouldBypassCache($token, $secret) ); $instance = (new User)->setRaw($user->extra) ->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret()); return $instance->map([ 'id' => $user->uid, 'nickname' => $user->nickname, 'name' => $user->name, 'email' => $user->email, 'avatar' => $user->imageUrl, ]); } /** * Get the token credentials for the request. * * @return \League\OAuth1\Client\Credentials\TokenCredentials */ protected function getToken() { $temp = $this->request->session()->get('oauth.temp'); if (! $temp) { throw new MissingTemporaryCredentialsException('Missing temporary OAuth credentials.'); } return $this->server->getTokenCredentials( $temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier') ); } /** * Determine if the request has the necessary OAuth verifier. * * @return bool */ protected function hasNecessaryVerifier() { return $this->request->has(['oauth_token', 'oauth_verifier']); } /** * Determine if the user information cache should be bypassed. * * @param string $token * @param string $secret * @return bool */ protected function shouldBypassCache($token, $secret) { $newHash = sha1($token.'_'.$secret); if (! empty($this->userHash) && $newHash !== $this->userHash) { $this->userHash = $newHash; return true; } $this->userHash = $this->userHash ?: $newHash; return false; } /** * Set the request instance. * * @param \Illuminate\Http\Request $request * @return $this */ public function setRequest(Request $request) { $this->request = $request; return $this; } } socialite/src/One/MissingTemporaryCredentialsException.php 0000644 00000000237 15060132306 0020110 0 ustar 00 <?php namespace Laravel\Socialite\One; use InvalidArgumentException; class MissingTemporaryCredentialsException extends InvalidArgumentException { // } socialite/src/One/MissingVerifierException.php 0000644 00000000223 15060132306 0015516 0 ustar 00 <?php namespace Laravel\Socialite\One; use InvalidArgumentException; class MissingVerifierException extends InvalidArgumentException { // } socialite/src/Contracts/User.php 0000644 00000001274 15060132306 0012676 0 ustar 00 <?php namespace Laravel\Socialite\Contracts; interface User { /** * Get the unique identifier for the user. * * @return string */ public function getId(); /** * Get the nickname / username for the user. * * @return string|null */ public function getNickname(); /** * Get the full name of the user. * * @return string|null */ public function getName(); /** * Get the e-mail address of the user. * * @return string|null */ public function getEmail(); /** * Get the avatar / image URL for the user. * * @return string|null */ public function getAvatar(); } socialite/src/Contracts/Provider.php 0000644 00000000705 15060132306 0013550 0 ustar 00 <?php namespace Laravel\Socialite\Contracts; interface Provider { /** * Redirect the user to the authentication page for the provider. * * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Illuminate\Http\RedirectResponse */ public function redirect(); /** * Get the User instance for the authenticated user. * * @return \Laravel\Socialite\Contracts\User */ public function user(); } socialite/src/Contracts/Factory.php 0000644 00000000411 15060132306 0013357 0 ustar 00 <?php namespace Laravel\Socialite\Contracts; interface Factory { /** * Get an OAuth provider implementation. * * @param string $driver * @return \Laravel\Socialite\Contracts\Provider */ public function driver($driver = null); } socialite/src/AbstractUser.php 0000644 00000007205 15060132306 0012422 0 ustar 00 <?php namespace Laravel\Socialite; use ArrayAccess; use Laravel\Socialite\Contracts\User; abstract class AbstractUser implements ArrayAccess, User { /** * The unique identifier for the user. * * @var mixed */ public $id; /** * The user's nickname / username. * * @var string */ public $nickname; /** * The user's full name. * * @var string */ public $name; /** * The user's e-mail address. * * @var string */ public $email; /** * The user's avatar image URL. * * @var string */ public $avatar; /** * The user's raw attributes. * * @var array */ public $user; /** * The user's other attributes. * * @var array */ public $attributes = []; /** * Get the unique identifier for the user. * * @return string */ public function getId() { return $this->id; } /** * Get the nickname / username for the user. * * @return string|null */ public function getNickname() { return $this->nickname; } /** * Get the full name of the user. * * @return string|null */ public function getName() { return $this->name; } /** * Get the e-mail address of the user. * * @return string|null */ public function getEmail() { return $this->email; } /** * Get the avatar / image URL for the user. * * @return string|null */ public function getAvatar() { return $this->avatar; } /** * Get the raw user array. * * @return array */ public function getRaw() { return $this->user; } /** * Set the raw user array from the provider. * * @param array $user * @return $this */ public function setRaw(array $user) { $this->user = $user; return $this; } /** * Map the given array onto the user's properties. * * @param array $attributes * @return $this */ public function map(array $attributes) { $this->attributes = $attributes; foreach ($attributes as $key => $value) { if (property_exists($this, $key)) { $this->{$key} = $value; } } return $this; } /** * Determine if the given raw user attribute exists. * * @param string $offset * @return bool */ #[\ReturnTypeWillChange] public function offsetExists($offset) { return array_key_exists($offset, $this->user); } /** * Get the given key from the raw user. * * @param string $offset * @return mixed */ #[\ReturnTypeWillChange] public function offsetGet($offset) { return $this->user[$offset]; } /** * Set the given attribute on the raw user array. * * @param string $offset * @param mixed $value * @return void */ #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { $this->user[$offset] = $value; } /** * Unset the given value from the raw user array. * * @param string $offset * @return void */ #[\ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->user[$offset]); } /** * Get a user attribute value dynamically. * * @param string $key * @return void */ public function __get($key) { return $this->attributes[$key] ?? null; } } socialite/LICENSE.md 0000644 00000002063 15060132306 0010121 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. socialite/composer.json 0000644 00000003357 15060132306 0011246 0 ustar 00 { "name": "laravel/socialite", "description": "Laravel wrapper around OAuth 1 & OAuth 2 libraries.", "keywords": ["oauth", "laravel"], "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/socialite/issues", "source": "https://github.com/laravel/socialite" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^7.2|^8.0", "ext-json": "*", "firebase/php-jwt": "^6.4", "guzzlehttp/guzzle": "^6.0|^7.0", "illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", "illuminate/http": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", "league/oauth1-client": "^1.10.1", "phpseclib/phpseclib": "^3.0" }, "require-dev": { "mockery/mockery": "^1.0", "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0|^9.0", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^8.0|^9.3|^10.4" }, "autoload": { "psr-4": { "Laravel\\Socialite\\": "src/" } }, "autoload-dev": { "psr-4": { "Laravel\\Socialite\\Tests\\": "tests/" } }, "extra": { "branch-alias": { "dev-master": "5.x-dev" }, "laravel": { "providers": [ "Laravel\\Socialite\\SocialiteServiceProvider" ], "aliases": { "Socialite": "Laravel\\Socialite\\Facades\\Socialite" } } }, "config": { "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true } socialite/README.md 0000644 00000003565 15060132306 0010004 0 ustar 00 <p align="center"><img src="/art/logo.svg" alt="Logo Laravel Socialite"></p> <p align="center"> <a href="https://github.com/laravel/socialite/actions"><img src="https://github.com/laravel/socialite/workflows/tests/badge.svg" alt="Build Status"></a> <a href="https://packagist.org/packages/laravel/socialite"><img src="https://img.shields.io/packagist/dt/laravel/socialite" alt="Total Downloads"></a> <a href="https://packagist.org/packages/laravel/socialite"><img src="https://img.shields.io/packagist/v/laravel/socialite" alt="Latest Stable Version"></a> <a href="https://packagist.org/packages/laravel/socialite"><img src="https://img.shields.io/packagist/l/laravel/socialite" alt="License"></a> </p> ## Introduction Laravel Socialite provides an expressive, fluent interface to OAuth authentication with Facebook, Twitter, Google, LinkedIn, GitHub, GitLab and Bitbucket. It handles almost all of the boilerplate social authentication code you are dreading writing. **We are not accepting new adapters.** Adapters for other platforms are listed at the community driven [Socialite Providers](https://socialiteproviders.com/) website. ## Official Documentation Documentation for Socialite can be found on the [Laravel website](https://laravel.com/docs/socialite). ## Contributing Thank you for considering contributing to Socialite! 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/socialite/security/policy) on how to report security vulnerabilities. ## License Laravel Socialite is open-sourced software licensed under the [MIT license](LICENSE.md).
| ver. 1.4 |
Github
|
.
| PHP 8.2.29 | Ð“ÐµÐ½ÐµÑ€Ð°Ñ†Ð¸Ñ Ñтраницы: 0.52 |
proxy
|
phpinfo
|
ÐаÑтройка