Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -3,6 +3,7 @@
namespace Collective\Html;
use BadMethodCallException;
use Illuminate\Support\Arr;
use Illuminate\Support\HtmlString;
trait Componentable
@@ -47,7 +48,7 @@ trait Componentable
* @param $name
* @param array $arguments
*
* @return \Illuminate\Contracts\View\View
* @return HtmlString
*/
protected function renderComponent($name, array $arguments)
{
@@ -81,7 +82,7 @@ trait Componentable
$default = null;
}
$data[$variable] = array_get($arguments, $i, $default);
$data[$variable] = Arr::get($arguments, $i, $default);
$i++;
}

View File

@@ -49,11 +49,11 @@ trait FormAccessible
unset($keys[0]);
$key = implode('.', $keys);
if ($this->hasFormMutator($key)) {
if (method_exists($relatedModel, 'hasFormMutator') && $key !== '' && $relatedModel->hasFormMutator($key)) {
return $relatedModel->getFormValue($key);
}
return data_get($relatedModel, $key);
return data_get($relatedModel, empty($key)? null: $key);
}
// No form mutator, let the model resolve this
@@ -69,11 +69,7 @@ trait FormAccessible
*/
public function isNestedModel($key)
{
if (in_array($key, array_keys($this->getRelations()))) {
return true;
}
return false;
return in_array($key, array_keys($this->getRelations()));
}
/**
@@ -81,13 +77,13 @@ trait FormAccessible
*
* @return bool
*/
protected function hasFormMutator($key)
public function hasFormMutator($key)
{
$methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC);
$mutator = collect($methods)
->first(function (ReflectionMethod $method) use ($key) {
return $method->getName() == 'form' . Str::studly($key) . 'Attribute';
return $method->getName() === 'form' . Str::studly($key) . 'Attribute';
});
return (bool) $mutator;

View File

@@ -9,8 +9,8 @@ use Illuminate\Contracts\Session\Session;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Traits\Macroable;
@@ -49,6 +49,12 @@ class FormBuilder
*/
protected $csrfToken;
/**
* Consider Request variables while auto fill.
* @var bool
*/
protected $considerRequest = false;
/**
* The session store implementation.
*
@@ -108,6 +114,7 @@ class FormBuilder
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
* @param \Illuminate\Contracts\View\Factory $view
* @param string $csrfToken
* @param Request $request
*/
public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Request $request = null)
{
@@ -127,7 +134,7 @@ class FormBuilder
*/
public function open(array $options = [])
{
$method = array_get($options, 'method', 'post');
$method = Arr::get($options, 'method', 'post');
// We need to extract the proper method from the attributes. If the method is
// something other than GET or POST we'll use POST since we will spoof the
@@ -152,7 +159,7 @@ class FormBuilder
// is used to spoof requests for this PUT, PATCH, etc. methods on forms.
$attributes = array_merge(
$attributes, array_except($options, $this->reserved)
$attributes, Arr::except($options, $this->reserved)
);
@@ -191,6 +198,16 @@ class FormBuilder
$this->model = $model;
}
/**
* Get the current model instance on the form builder.
*
* @return mixed $model
*/
public function getModel()
{
return $this->model;
}
/**
* Close the current form.
*
@@ -319,6 +336,20 @@ class FormBuilder
return $this->input('password', $name, '', $options);
}
/**
* Create a range input field.
*
* @param string $name
* @param string $value
* @param array $options
*
* @return \Illuminate\Support\HtmlString
*/
public function range($name, $value = null, $options = [])
{
return $this->input('range', $name, $value, $options);
}
/**
* Create a hidden input field.
*
@@ -454,6 +485,10 @@ class FormBuilder
*/
public function time($name, $value = null, $options = [])
{
if ($value instanceof DateTime) {
$value = $value->format('H:i');
}
return $this->input('time', $name, $value, $options);
}
@@ -471,6 +506,24 @@ class FormBuilder
return $this->input('url', $name, $value, $options);
}
/**
* Create a week input field.
*
* @param string $name
* @param string $value
* @param array $options
*
* @return \Illuminate\Support\HtmlString
*/
public function week($name, $value = null, $options = [])
{
if ($value instanceof DateTime) {
$value = $value->format('Y-\WW');
}
return $this->input('week', $name, $value, $options);
}
/**
* Create a file input field.
*
@@ -517,7 +570,7 @@ class FormBuilder
// the element. Then we'll create the final textarea elements HTML for us.
$options = $this->html->attributes($options);
return $this->toHtmlString('<textarea' . $options . '>' . e($value). '</textarea>');
return $this->toHtmlString('<textarea' . $options . '>' . e($value, false). '</textarea>');
}
/**
@@ -536,9 +589,9 @@ class FormBuilder
// If the "size" attribute was not specified, we will just look for the regular
// columns and rows attributes, using sane defaults if these do not exist on
// the attributes array. We'll then return this entire options array back.
$cols = array_get($options, 'cols', 50);
$cols = Arr::get($options, 'cols', 50);
$rows = array_get($options, 'rows', 10);
$rows = Arr::get($options, 'rows', 10);
return array_merge($options, compact('cols', 'rows'));
}
@@ -562,9 +615,10 @@ class FormBuilder
*
* @param string $name
* @param array $list
* @param string $selected
* @param string|bool $selected
* @param array $selectAttributes
* @param array $optionsAttributes
* @param array $optgroupsAttributes
*
* @return \Illuminate\Support\HtmlString
*/
@@ -573,7 +627,8 @@ class FormBuilder
$list = [],
$selected = null,
array $selectAttributes = [],
array $optionsAttributes = []
array $optionsAttributes = [],
array $optgroupsAttributes = []
) {
$this->type = 'select';
@@ -599,8 +654,9 @@ class FormBuilder
}
foreach ($list as $value => $display) {
$optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes);
$optionAttributes = $optionsAttributes[$value] ?? [];
$optgroupAttributes = $optgroupsAttributes[$value] ?? [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes);
}
// Once we have all of this HTML, we can join this into a single element after
@@ -675,13 +731,14 @@ class FormBuilder
* @param string $value
* @param string $selected
* @param array $attributes
* @param array $optgroupAttributes
*
* @return \Illuminate\Support\HtmlString
*/
public function getSelectOption($display, $value, $selected, array $attributes = [])
public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = [])
{
if (is_array($display)) {
return $this->optionGroup($display, $value, $selected, $attributes);
if (is_iterable($display)) {
return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes);
}
return $this->option($display, $value, $selected, $attributes);
@@ -694,18 +751,24 @@ class FormBuilder
* @param string $label
* @param string $selected
* @param array $attributes
* @param array $optionsAttributes
* @param integer $level
*
* @return \Illuminate\Support\HtmlString
*/
protected function optionGroup($list, $label, $selected, array $attributes = [])
protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = [], $level = 0)
{
$html = [];
$space = str_repeat("&nbsp;", $level);
foreach ($list as $value => $display) {
$html[] = $this->option($display, $value, $selected, $attributes);
$optionAttributes = $optionsAttributes[$value] ?? [];
if (is_iterable($display)) {
$html[] = $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level+5);
} else {
$html[] = $this->option($space.$display, $value, $selected, $optionAttributes);
}
}
return $this->toHtmlString('<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>');
return $this->toHtmlString('<optgroup label="' . e($space.$label, false) . '"' . $this->html->attributes($attributes) . '>' . implode('', $html) . '</optgroup>');
}
/**
@@ -722,9 +785,14 @@ class FormBuilder
{
$selected = $this->getSelectedValue($value, $selected);
$options = ['value' => $value, 'selected' => $selected] + $attributes;
$options = array_merge(['value' => $value, 'selected' => $selected], $attributes);
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
$string = '<option' . $this->html->attributes($options) . '>';
if ($display !== null) {
$string .= e($display, false) . '</option>';
}
return $this->toHtmlString($string);
}
/**
@@ -741,12 +809,10 @@ class FormBuilder
$options = [
'selected' => $selected,
'disabled' => 'disabled',
'hidden' => 'hidden',
'value' => ''
'value' => '',
];
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display, false) . '</option>');
}
/**
@@ -760,12 +826,14 @@ class FormBuilder
protected function getSelectedValue($value, $selected)
{
if (is_array($selected)) {
return in_array($value, $selected, true) ? 'selected' : null;
return in_array($value, $selected, true) || in_array((string) $value, $selected, true) ? 'selected' : null;
} elseif ($selected instanceof Collection) {
return $selected->contains($value) ? 'selected' : null;
}
return ((string) $value == (string) $selected) ? 'selected' : null;
if (is_int($value) && is_bool($selected)) {
return (bool)$value === $selected;
}
return ((string) $value === (string) $selected) ? 'selected' : null;
}
/**
@@ -846,7 +914,7 @@ class FormBuilder
return $this->getRadioCheckedState($name, $value, $checked);
default:
return $this->getValueAttribute($name) == $value;
return $this->compareValues($name, $value);
}
}
@@ -867,7 +935,7 @@ class FormBuilder
return false;
}
if ($this->missingOldAndModel($name) && !$request) {
if ($this->missingOldAndModel($name) && is_null($request)) {
return $checked;
}
@@ -899,6 +967,20 @@ class FormBuilder
return $checked;
}
return $this->compareValues($name, $value);
}
/**
* Determine if the provide value loosely compares to the value assigned to the field.
* Use loose comparison because Laravel model casting may be in affect and therefore
* 1 == true and 0 == false.
*
* @param string $name
* @param string $value
* @return bool
*/
protected function compareValues($name, $value)
{
return $this->getValueAttribute($name) == $value;
}
@@ -943,6 +1025,24 @@ class FormBuilder
return $this->input('image', $name, null, $attributes);
}
/**
* Create a month input field.
*
* @param string $name
* @param string $value
* @param array $options
*
* @return \Illuminate\Support\HtmlString
*/
public function month($name, $value = null, $options = [])
{
if ($value instanceof DateTime) {
$value = $value->format('Y-m');
}
return $this->input('month', $name, $value, $options);
}
/**
* Create a color input field.
*
@@ -987,6 +1087,50 @@ class FormBuilder
return $this->toHtmlString('<button' . $this->html->attributes($options) . '>' . $value . '</button>');
}
/**
* Create a datalist box field.
*
* @param string $id
* @param array $list
*
* @return \Illuminate\Support\HtmlString
*/
public function datalist($id, $list = [])
{
$this->type = 'datalist';
$attributes['id'] = $id;
$html = [];
if ($this->isAssociativeArray($list)) {
foreach ($list as $value => $display) {
$html[] = $this->option($display, $value, null, []);
}
} else {
foreach ($list as $value) {
$html[] = $this->option($value, $value, null, []);
}
}
$attributes = $this->html->attributes($attributes);
$list = implode('', $html);
return $this->toHtmlString("<datalist{$attributes}>{$list}</datalist>");
}
/**
* Determine if an array is associative.
*
* @param array $array
* @return bool
*/
protected function isAssociativeArray($array)
{
return (array_values($array) !== $array);
}
/**
* Parse the form action method.
*
@@ -998,7 +1142,7 @@ class FormBuilder
{
$method = strtoupper($method);
return $method != 'GET' ? 'POST' : $method;
return $method !== 'GET' ? 'POST' : $method;
}
/**
@@ -1057,7 +1201,13 @@ class FormBuilder
protected function getRouteAction($options)
{
if (is_array($options)) {
return $this->url->route($options[0], array_slice($options, 1));
$parameters = array_slice($options, 1);
if (array_keys($options) === [0, 1]) {
$parameters = head($parameters);
}
return $this->url->route($options[0], $parameters);
}
return $this->url->route($options);
@@ -1100,7 +1250,7 @@ class FormBuilder
// If the method is something other than GET we will go ahead and attach the
// CSRF token to the form, as this can't hurt and is convenient to simply
// always have available on every form the developers creates for them.
if ($method != 'GET') {
if ($method !== 'GET') {
$appendage .= $this->token();
}
@@ -1142,7 +1292,7 @@ class FormBuilder
$old = $this->old($name);
if (! is_null($old) && $name != '_method') {
if (! is_null($old) && $name !== '_method') {
return $old;
}
@@ -1154,14 +1304,14 @@ class FormBuilder
&& is_null($old)
&& is_null($value)
&& !is_null($this->view->shared('errors'))
&& count($this->view->shared('errors')) > 0
&& count(is_countable($this->view->shared('errors')) ? $this->view->shared('errors') : []) > 0
) {
return null;
}
}
$request = $this->request($name);
if (!is_null($request)) {
if (! is_null($request) && $name != '_method') {
return $request;
}
@@ -1174,6 +1324,15 @@ class FormBuilder
}
}
/**
* Take Request in fill process
* @param bool $consider
*/
public function considerRequest($consider = true)
{
$this->considerRequest = $consider;
}
/**
* Get value from current Request
* @param $name
@@ -1181,6 +1340,10 @@ class FormBuilder
*/
protected function request($name)
{
if (!$this->considerRequest) {
return null;
}
if (!isset($this->request)) {
return null;
}
@@ -1192,19 +1355,18 @@ class FormBuilder
* Get the model value that should be assigned to the field.
*
* @param string $name
* @param mixed $model
*
* @return mixed
*/
protected function getModelValueAttribute($name, $model = null)
protected function getModelValueAttribute($name)
{
$key = $this->transformKey($name);
if (method_exists($this->model, 'getFormValue')) {
if ((is_string($this->model) || is_object($this->model)) && method_exists($this->model, 'getFormValue')) {
return $this->model->getFormValue($key);
}
return data_get($this->model, $this->transformKey($name));
return data_get($this->model, $key);
}
/**
@@ -1246,7 +1408,7 @@ class FormBuilder
*/
public function oldInputIsEmpty()
{
return (isset($this->session) && count($this->session->getOldInput()) == 0);
return (isset($this->session) && count((array) $this->session->getOldInput()) === 0);
}
/**

View File

@@ -78,7 +78,7 @@ class HtmlBuilder
{
$attributes['src'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<script' . $this->attributes($attributes) . '></script>' . PHP_EOL);
return $this->toHtmlString('<script' . $this->attributes($attributes) . '></script>');
}
/**
@@ -94,11 +94,11 @@ class HtmlBuilder
{
$defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet'];
$attributes = $attributes + $defaults;
$attributes = array_merge($defaults, $attributes);
$attributes['href'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>' . PHP_EOL);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>');
}
/**
@@ -132,11 +132,11 @@ class HtmlBuilder
{
$defaults = ['rel' => 'shortcut icon', 'type' => 'image/x-icon'];
$attributes = $attributes + $defaults;
$attributes = array_merge($defaults, $attributes);
$attributes['href'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>' . PHP_EOL);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>');
}
/**
@@ -162,7 +162,7 @@ class HtmlBuilder
$title = $this->entities($title);
}
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
return $this->toHtmlString('<a href="' . $this->entities($url) . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
}
/**
@@ -171,12 +171,13 @@ class HtmlBuilder
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $escape
*
* @return \Illuminate\Support\HtmlString
*/
public function secureLink($url, $title = null, $attributes = [])
public function secureLink($url, $title = null, $attributes = [], $escape = true)
{
return $this->link($url, $title, $attributes, true);
return $this->link($url, $title, $attributes, true, $escape);
}
/**
@@ -186,14 +187,15 @@ class HtmlBuilder
* @param string $title
* @param array $attributes
* @param bool $secure
* @param bool $escape
*
* @return \Illuminate\Support\HtmlString
*/
public function linkAsset($url, $title = null, $attributes = [], $secure = null)
public function linkAsset($url, $title = null, $attributes = [], $secure = null, $escape = true)
{
$url = $this->url->asset($url, $secure);
return $this->link($url, $title ?: $url, $attributes, $secure);
return $this->link($url, $title ?: $url, $attributes, $secure, $escape);
}
/**
@@ -202,12 +204,13 @@ class HtmlBuilder
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $escape
*
* @return \Illuminate\Support\HtmlString
*/
public function linkSecureAsset($url, $title = null, $attributes = [])
public function linkSecureAsset($url, $title = null, $attributes = [], $escape = true)
{
return $this->linkAsset($url, $title, $attributes, true);
return $this->linkAsset($url, $title, $attributes, true, $escape);
}
/**
@@ -217,12 +220,14 @@ class HtmlBuilder
* @param string $title
* @param array $parameters
* @param array $attributes
* @param bool $secure
* @param bool $escape
*
* @return \Illuminate\Support\HtmlString
*/
public function linkRoute($name, $title = null, $parameters = [], $attributes = [])
public function linkRoute($name, $title = null, $parameters = [], $attributes = [], $secure = null, $escape = true)
{
return $this->link($this->url->route($name, $parameters), $title, $attributes);
return $this->link($this->url->route($name, $parameters), $title, $attributes, $secure, $escape);
}
/**
@@ -232,12 +237,14 @@ class HtmlBuilder
* @param string $title
* @param array $parameters
* @param array $attributes
* @param bool $secure
* @param bool $escape
*
* @return \Illuminate\Support\HtmlString
*/
public function linkAction($action, $title = null, $parameters = [], $attributes = [])
public function linkAction($action, $title = null, $parameters = [], $attributes = [], $secure = null, $escape = true)
{
return $this->link($this->url->action($action, $parameters), $title, $attributes);
return $this->link($this->url->action($action, $parameters), $title, $attributes, $secure, $escape);
}
/**
@@ -357,7 +364,7 @@ class HtmlBuilder
{
$html = '';
if (count($list) == 0) {
if (count($list) === 0) {
return $html;
}
@@ -387,7 +394,7 @@ class HtmlBuilder
if (is_array($value)) {
return $this->nestedListing($key, $type, $value);
} else {
return '<li>' . e($value) . '</li>';
return '<li>' . e($value, false) . '</li>';
}
}
@@ -451,12 +458,16 @@ class HtmlBuilder
}
// Treat boolean attributes as HTML properties
if (is_bool($value) && $key != 'value') {
if (is_bool($value) && $key !== 'value') {
return $value ? $key : '';
}
if (is_array($value) && $key === 'class') {
return 'class="' . implode(' ', $value) . '"';
}
if (! is_null($value)) {
return $key . '="' . e($value) . '"';
return $key . '="' . e($value, false) . '"';
}
}
@@ -511,7 +522,7 @@ class HtmlBuilder
$attributes = array_merge($defaults, $attributes);
return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>' . PHP_EOL);
return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>');
}
/**
@@ -525,8 +536,8 @@ class HtmlBuilder
*/
public function tag($tag, $content, array $attributes = [])
{
$content = is_array($content) ? implode(PHP_EOL, $content) : $content;
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . PHP_EOL . $this->toHtmlString($content) . PHP_EOL . '</' . $tag . '>' . PHP_EOL);
$content = is_array($content) ? implode('', $content) : $content;
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . $this->toHtmlString($content) . '</' . $tag . '>');
}
/**

View File

@@ -2,12 +2,12 @@
namespace Collective\Html;
use Illuminate\Support\Facades\Blade;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;
class HtmlServiceProvider extends ServiceProvider
class HtmlServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Supported Blade Directives
@@ -17,13 +17,6 @@ class HtmlServiceProvider extends ServiceProvider
protected $directives = ['entities','decode','script','style','image','favicon','link','secureLink','linkAsset','linkSecureAsset','linkRoute','linkAction','mailto','email','ol','ul','dl','meta','tag','open','model','close','token','label','input','text','password','hidden','email','tel','number','date','datetime','datetimeLocal','time','url','file','textarea','select','selectRange','selectYear','selectMonth','getSelectOption','checkbox','radio','reset','image','color','submit','button','old'
];
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*

View File

@@ -10,7 +10,7 @@ if (! function_exists('link_to')) {
* @param bool $secure
* @param bool $escape
*
* @return string
* @return \Illuminate\Support\HtmlString
*/
function link_to($url, $title = null, $attributes = [], $secure = null, $escape = true)
{
@@ -27,7 +27,7 @@ if (! function_exists('link_to_asset')) {
* @param array $attributes
* @param bool $secure
*
* @return string
* @return \Illuminate\Support\HtmlString
*/
function link_to_asset($url, $title = null, $attributes = [], $secure = null)
{
@@ -44,7 +44,7 @@ if (! function_exists('link_to_route')) {
* @param array $parameters
* @param array $attributes
*
* @return string
* @return \Illuminate\Support\HtmlString
*/
function link_to_route($name, $title = null, $parameters = [], $attributes = [])
{
@@ -61,7 +61,7 @@ if (! function_exists('link_to_action')) {
* @param array $parameters
* @param array $attributes
*
* @return string
* @return \Illuminate\Support\HtmlString
*/
function link_to_action($action, $title = null, $parameters = [], $attributes = [])
{