Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 566bab0

Browse files
authored
fixed automatic auto-scripts not to throw a exception if array is a n… (#102)
…umeric array | Q | A | --------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | - | License | MIT | Doc PR | -
1 parent 066bcad commit 566bab0

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

src/Automatic/Automatic.php

+26-20
Original file line numberDiff line numberDiff line change
@@ -656,36 +656,42 @@ public function onPostUpdate(Event $event, array $operations = []): void
656656
*/
657657
public function executeAutoScripts(Event $event): void
658658
{
659-
$event->stopPropagation();
660-
661659
// force reloading scripts as we might have added and removed during this run
662660
$json = new JsonFile(Factory::getComposerFile());
663661
$jsonContents = $json->read();
664662

665-
if (isset($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS])) {
666-
/** @var \Narrowspark\Automatic\ScriptExecutor $scriptExecutor */
667-
$scriptExecutor = $this->container->get(ScriptExecutor::class);
663+
if (! isset($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS])) {
664+
$this->container->get(IOInterface::class)->write('No auto-scripts section was found under scripts', true, IOInterface::VERBOSE);
668665

669-
foreach ((array) $this->container->get(Lock::class)->get(ScriptExecutor::TYPE) as $extenders) {
670-
foreach ($extenders as $class => $path) {
671-
if (! \class_exists($class)) {
672-
require_once $path;
673-
}
666+
return;
667+
}
674668

675-
/** @var \Narrowspark\Automatic\Common\Contract\ScriptExtender $class */
676-
$reflectionClass = new ReflectionClass($class);
669+
if (\in_array(true, \array_map('\is_numeric', \array_keys($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS])), true)) {
670+
return;
671+
}
677672

678-
if ($reflectionClass->isInstantiable() && $reflectionClass->hasMethod('getType')) {
679-
$scriptExecutor->add($class::getType(), $class);
680-
}
673+
$event->stopPropagation();
674+
675+
/** @var \Narrowspark\Automatic\ScriptExecutor $scriptExecutor */
676+
$scriptExecutor = $this->container->get(ScriptExecutor::class);
677+
678+
foreach ((array) $this->container->get(Lock::class)->get(ScriptExecutor::TYPE) as $extenders) {
679+
foreach ($extenders as $class => $path) {
680+
if (! \class_exists($class)) {
681+
require_once $path;
681682
}
682-
}
683683

684-
foreach ($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS] as $cmd => $type) {
685-
$scriptExecutor->execute($type, $cmd);
684+
/** @var \Narrowspark\Automatic\Common\Contract\ScriptExtender $class */
685+
$reflectionClass = new ReflectionClass($class);
686+
687+
if ($reflectionClass->isInstantiable() && $reflectionClass->hasMethod('getType')) {
688+
$scriptExecutor->add($class::getType(), $class);
689+
}
686690
}
687-
} else {
688-
$this->container->get(IOInterface::class)->write('No auto-scripts section was found under scripts', true, IOInterface::VERBOSE);
691+
}
692+
693+
foreach ($jsonContents['scripts'][ScriptEvents::AUTO_SCRIPTS] as $cmd => $type) {
694+
$scriptExecutor->execute($type, $cmd);
689695
}
690696
}
691697

tests/Automatic/AutomaticTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,34 @@ public function testExecuteAutoScripts(): void
424424
\putenv('COMPOSER');
425425
}
426426

427+
public function testExecuteAutoScriptsWithNumericArray(): void
428+
{
429+
\putenv('COMPOSER=' . __DIR__ . '/Fixture/composer-with-numeric-scripts.json');
430+
431+
$eventMock = $this->mock(Event::class);
432+
$eventMock->shouldReceive('stopPropagation')
433+
->never();
434+
435+
$containerMock = $this->mock(ContainerContract::class);
436+
$containerMock->shouldReceive('get')
437+
->never()
438+
->with(ScriptExecutor::class);
439+
$containerMock->shouldReceive('get')
440+
->never()
441+
->with(Lock::class);
442+
443+
$this->automatic->setContainer($containerMock);
444+
$this->automatic->executeAutoScripts($eventMock);
445+
446+
\putenv('COMPOSER=');
447+
\putenv('COMPOSER');
448+
}
449+
427450
public function testExecuteAutoScriptsWithoutScripts(): void
428451
{
429452
$eventMock = $this->mock(Event::class);
430453
$eventMock->shouldReceive('stopPropagation')
431-
->once();
454+
->never();
432455

433456
$this->ioMock->shouldReceive('write')
434457
->once()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "narrowspark/automatic",
3+
"type": "composer-plugin",
4+
"description": "Composer plugin for automatically project configuration and creation.",
5+
"license": "MIT",
6+
"config": {
7+
"optimize-autoloader": true,
8+
"preferred-install": "dist"
9+
},
10+
"scripts": {
11+
"auto-scripts": [
12+
"@php -r 'echo 1;'"
13+
],
14+
"post-install-cmd": [
15+
"@auto-scripts"
16+
],
17+
"post-update-cmd": [
18+
"@auto-scripts"
19+
]
20+
}
21+
}

0 commit comments

Comments
 (0)