Skip to content

Commit 003d994

Browse files
authored
Merge branch '11.x' into cache-view-factory-repeated-calls
2 parents 5d6375f + ca54cb6 commit 003d994

File tree

15 files changed

+267
-150
lines changed

15 files changed

+267
-150
lines changed

src/Illuminate/Database/Console/Migrations/FreshCommand.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,18 @@ public function handle()
6262

6363
$database = $this->input->getOption('database');
6464

65-
if (! is_null($database)) {
66-
$this->migrator->setConnection($database);
67-
}
68-
69-
if ($this->migrator->repositoryExists()) {
70-
$this->newLine();
71-
72-
$this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([
73-
'--database' => $database,
74-
'--drop-views' => $this->option('drop-views'),
75-
'--drop-types' => $this->option('drop-types'),
76-
'--force' => true,
77-
])) == 0);
78-
}
65+
$this->migrator->usingConnection($database, function () use ($database) {
66+
if ($this->migrator->repositoryExists()) {
67+
$this->newLine();
68+
69+
$this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([
70+
'--database' => $database,
71+
'--drop-views' => $this->option('drop-views'),
72+
'--drop-types' => $this->option('drop-types'),
73+
'--force' => true,
74+
])) == 0);
75+
}
76+
});
7977

8078
$this->newLine();
8179

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ protected function compileLegacyRenameColumn(Blueprint $blueprint, Fluent $comma
354354
default => $column['type_name'],
355355
},
356356
'nullable' => $column['nullable'],
357-
'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp')
357+
'default' => $column['default'] && (str_starts_with(strtolower($column['default']), 'current_timestamp') || $column['default'] === 'NULL')
358358
? new Expression($column['default'])
359359
: $column['default'],
360360
'autoIncrement' => $column['auto_increment'],

src/Illuminate/Validation/NestedRules.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public function __construct(callable $callback)
3030
* @param string $attribute
3131
* @param mixed $value
3232
* @param mixed $data
33+
* @param mixed $context
3334
* @return \stdClass
3435
*/
35-
public function compile($attribute, $value, $data = null)
36+
public function compile($attribute, $value, $data = null, $context = null)
3637
{
37-
$rules = call_user_func($this->callback, $value, $attribute, $data);
38+
$rules = call_user_func($this->callback, $value, $attribute, $data, $context);
3839

3940
$parser = new ValidationRuleParser(
4041
Arr::undot(Arr::wrap($data))

src/Illuminate/Validation/ValidationRuleParser.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected function prepareRule($rule, $attribute)
127127

128128
if ($rule instanceof NestedRules) {
129129
return $rule->compile(
130-
$attribute, $this->data[$attribute] ?? null, Arr::dot($this->data)
130+
$attribute, $this->data[$attribute] ?? null, Arr::dot($this->data), $this->data
131131
)->rules[$attribute];
132132
}
133133

@@ -152,7 +152,9 @@ protected function explodeWildcardRules($results, $attribute, $rules)
152152
if (Str::startsWith($key, $attribute) || (bool) preg_match('/^'.$pattern.'\z/', $key)) {
153153
foreach ((array) $rules as $rule) {
154154
if ($rule instanceof NestedRules) {
155-
$compiled = $rule->compile($key, $value, $data);
155+
$context = Arr::get($this->data, Str::beforeLast($key, '.'));
156+
157+
$compiled = $rule->compile($key, $value, $data, $context);
156158

157159
$this->implicitAttributes = array_merge_recursive(
158160
$compiled->implicitAttributes,

src/Illuminate/View/Compilers/ComponentTagCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ protected function componentString(string $component, array $attributes)
259259
}
260260

261261
return "##BEGIN-COMPONENT-CLASS##@component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).'])
262-
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag && $constructor = (new ReflectionClass('.$class.'::class))->getConstructor()): ?>
263-
<?php $attributes = $attributes->except(collect($constructor->getParameters())->map->getName()->all()); ?>
262+
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
263+
<?php $attributes = $attributes->except(\\'.$class.'::ignoredParameterNames()); ?>
264264
<?php endif; ?>
265265
<?php $component->withAttributes(['.$this->attributesToString($attributes->all(), $escapeAttributes = $class !== DynamicComponent::class).']); ?>';
266266
}

src/Illuminate/View/Compilers/Concerns/CompilesComponents.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function compileClassComponentOpening(string $component, string $a
6868
return implode("\n", [
6969
'<?php if (isset($component)) { $__componentOriginal'.$hash.' = $component; } ?>',
7070
'<?php if (isset($attributes)) { $__attributesOriginal'.$hash.' = $attributes; } ?>',
71-
'<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>',
71+
'<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>',
7272
'<?php $component->withName('.$alias.'); ?>',
7373
'<?php if ($component->shouldRender()): ?>',
7474
'<?php $__env->startComponent($component->resolveView(), $component->data()); ?>',
@@ -157,19 +157,35 @@ protected function compileEndComponentFirst()
157157
*/
158158
protected function compileProps($expression)
159159
{
160-
return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag; ?>
161-
<?php foreach(\$attributes->onlyProps{$expression} as \$__key => \$__value) {
162-
\$\$__key = \$\$__key ?? \$__value;
163-
} ?>
164-
<?php \$attributes = \$attributes->exceptProps{$expression}; ?>
165-
<?php foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) {
160+
return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag;
161+
162+
\$__newAttributes = [];
163+
\$__propNames = \Illuminate\View\ComponentAttributeBag::extractPropNames({$expression});
164+
165+
foreach (\$attributes->all() as \$__key => \$__value) {
166+
if (in_array(\$__key, \$__propNames)) {
167+
\$\$__key = \$\$__key ?? \$__value;
168+
} else {
169+
\$__newAttributes[\$__key] = \$__value;
170+
}
171+
}
172+
173+
\$attributes = new \Illuminate\View\ComponentAttributeBag(\$__newAttributes);
174+
175+
unset(\$__propNames);
176+
unset(\$__newAttributes);
177+
178+
foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) {
166179
\$\$__key = \$\$__key ?? \$__value;
167-
} ?>
168-
<?php \$__defined_vars = get_defined_vars(); ?>
169-
<?php foreach (\$attributes as \$__key => \$__value) {
180+
}
181+
182+
\$__defined_vars = get_defined_vars();
183+
184+
foreach (\$attributes->all() as \$__key => \$__value) {
170185
if (array_key_exists(\$__key, \$__defined_vars)) unset(\$\$__key);
171-
} ?>
172-
<?php unset(\$__defined_vars); ?>";
186+
}
187+
188+
unset(\$__defined_vars); ?>";
173189
}
174190

175191
/**

src/Illuminate/View/Component.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ abstract class Component
7575
*/
7676
protected static $constructorParametersCache = [];
7777

78+
/**
79+
* The cache of ignored parameter names.
80+
*
81+
* @var array
82+
*/
83+
protected static $ignoredParameterNames = [];
84+
7885
/**
7986
* Get the view / view contents that represent the component.
8087
*
@@ -417,6 +424,30 @@ protected function factory()
417424
return static::$factory;
418425
}
419426

427+
/**
428+
* Get the cached set of anonymous component constructor parameter names to exclude.
429+
*
430+
* @return array
431+
*/
432+
public static function ignoredParameterNames()
433+
{
434+
if (! isset(static::$ignoredParameterNames[static::class])) {
435+
$constructor = (new ReflectionClass(
436+
static::class
437+
))->getConstructor();
438+
439+
if (! $constructor) {
440+
return static::$ignoredParameterNames[static::class] = [];
441+
}
442+
443+
static::$ignoredParameterNames[static::class] = collect($constructor->getParameters())
444+
->map->getName()
445+
->all();
446+
}
447+
448+
return static::$ignoredParameterNames[static::class];
449+
}
450+
420451
/**
421452
* Flush the component's cached state.
422453
*

src/Illuminate/View/ComponentAttributeBag.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public function __construct(array $attributes = [])
3737
$this->attributes = $attributes;
3838
}
3939

40+
/**
41+
* Get all of the attribute values.
42+
*
43+
* @return array
44+
*/
45+
public function all()
46+
{
47+
return $this->attributes;
48+
}
49+
4050
/**
4151
* Get the first attribute's value.
4252
*
@@ -207,7 +217,7 @@ public function thatStartWith($needles)
207217
*/
208218
public function onlyProps($keys)
209219
{
210-
return $this->only($this->extractPropNames($keys));
220+
return $this->only(static::extractPropNames($keys));
211221
}
212222

213223
/**
@@ -218,27 +228,7 @@ public function onlyProps($keys)
218228
*/
219229
public function exceptProps($keys)
220230
{
221-
return $this->except($this->extractPropNames($keys));
222-
}
223-
224-
/**
225-
* Extract prop names from given keys.
226-
*
227-
* @param mixed|array $keys
228-
* @return array
229-
*/
230-
protected function extractPropNames($keys)
231-
{
232-
$props = [];
233-
234-
foreach ($keys as $key => $defaultValue) {
235-
$key = is_numeric($key) ? $defaultValue : $key;
236-
237-
$props[] = $key;
238-
$props[] = Str::kebab($key);
239-
}
240-
241-
return $props;
231+
return $this->except(static::extractPropNames($keys));
242232
}
243233

244234
/**
@@ -401,6 +391,26 @@ public function setAttributes(array $attributes)
401391
$this->attributes = $attributes;
402392
}
403393

394+
/**
395+
* Extract "prop" names from given keys.
396+
*
397+
* @param array $keys
398+
* @return array
399+
*/
400+
public static function extractPropNames(array $keys)
401+
{
402+
$props = [];
403+
404+
foreach ($keys as $key => $default) {
405+
$key = is_numeric($key) ? $default : $key;
406+
407+
$props[] = $key;
408+
$props[] = Str::kebab($key);
409+
}
410+
411+
return $props;
412+
}
413+
404414
/**
405415
* Get content as a string of HTML.
406416
*

src/Illuminate/View/Concerns/ManagesEvents.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ protected function addEventListener($name, $callback)
174174
*/
175175
public function callComposer(ViewContract $view)
176176
{
177-
$this->events->dispatch('composing: '.$view->name(), [$view]);
177+
if ($this->events->hasListeners($event = 'composing: '.$view->name())) {
178+
$this->events->dispatch($event, [$view]);
179+
}
178180
}
179181

180182
/**
@@ -185,6 +187,8 @@ public function callComposer(ViewContract $view)
185187
*/
186188
public function callCreator(ViewContract $view)
187189
{
188-
$this->events->dispatch('creating: '.$view->name(), [$view]);
190+
if ($this->events->hasListeners($event = 'creating: '.$view->name())) {
191+
$this->events->dispatch($event, [$view]);
192+
}
189193
}
190194
}

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public function testNativeRenameColumnOnLegacyMariaDB()
215215
$table->renameColumn('name', 'title');
216216
$table->renameColumn('id', 'key');
217217
$table->renameColumn('generated', 'new_generated');
218+
$table->renameColumn('foo', 'bar');
218219
});
219220

220221
$connection = m::mock(Connection::class);
@@ -224,12 +225,14 @@ public function testNativeRenameColumnOnLegacyMariaDB()
224225
['name' => 'name', 'type' => 'varchar(255)', 'type_name' => 'varchar', 'nullable' => true, 'collation' => 'utf8mb4_unicode_ci', 'default' => 'foo', 'comment' => null, 'auto_increment' => false],
225226
['name' => 'id', 'type' => 'bigint unsigned', 'type_name' => 'bigint', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => 'lorem ipsum', 'auto_increment' => true],
226227
['name' => 'generated', 'type' => 'int', 'type_name' => 'int', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => null, 'auto_increment' => false, 'generation' => ['type' => 'stored', 'expression' => 'expression']],
228+
['name' => 'foo', 'type' => 'int', 'type_name' => 'int', 'nullable' => true, 'collation' => null, 'default' => 'NULL', 'comment' => null, 'auto_increment' => false, 'generation' => null],
227229
]);
228230

229231
$this->assertEquals([
230232
"alter table `users` change `name` `title` varchar(255) collate 'utf8mb4_unicode_ci' null default 'foo'",
231233
"alter table `users` change `id` `key` bigint unsigned not null auto_increment comment 'lorem ipsum'",
232234
'alter table `users` change `generated` `new_generated` int as (expression) stored not null',
235+
'alter table `users` change `foo` `bar` int null default NULL',
233236
], $blueprint->toSql($connection, new MariaDbGrammar));
234237
}
235238

0 commit comments

Comments
 (0)