Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ jobs:
sudo -u mysql mysqld --datadir=/var/lib/mysql --default-time-zone=SYSTEM --initialize-insecure
sudo systemctl start mysql

- name: Install shopware-cli
uses: shopware/shopware-cli-action@v2

- name: Create new Shopware Project
run: composer create-project shopware/production:6.5.8.8 . --no-interaction
run: |
shopware-cli project create shop 6.5.8.8 --no-audit
mv shop/* .
mv shop/.* . || true
rm -rf shop

- name: Checkout
uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -61,8 +61,8 @@ private function createContainer(): ContainerBuilder
InstalledVersions::reload(include $projectDir . '/vendor/composer/installed.php');
(new DotenvLoader($projectDir))->load();

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config'));
$loader->load('services.xml');
$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/Resources/config'));
$loader->load('services.php');
$container->compile();
$container->set(self::class, $this);

Expand Down
13 changes: 12 additions & 1 deletion src/Config/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Shopware\Deployment\Application;
use Shopware\Deployment\Helper\EnvironmentHelper;
use Shopware\Deployment\Struct\OneTimeTask;
use Shopware\Deployment\Struct\OneTimeTaskWhen;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -76,7 +78,16 @@ private static function fillConfig(ProjectConfiguration $projectConfiguration, a
if (isset($deployment['one-time-tasks']) && \is_array($deployment['one-time-tasks'])) {
foreach ($deployment['one-time-tasks'] as $task) {
if (isset($task['id'], $task['script']) && \is_string($task['id']) && \is_string($task['script'])) {
$projectConfiguration->oneTimeTasks[$task['id']] = $task['script'];
$when = OneTimeTaskWhen::AFTER;
if (isset($task['when']) && \is_string($task['when'])) {
$when = OneTimeTaskWhen::from($task['when']);
}

$projectConfiguration->oneTimeTasks[$task['id']] = new OneTimeTask(
$task['id'],
$task['script'],
$when
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Config/ProjectConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ProjectConfiguration
public ProjectStore $store;

/**
* @var array<string, string>
* @var array<string, \Shopware\Deployment\Struct\OneTimeTask>
*/
public array $oneTimeTasks = [];

Expand Down
39 changes: 39 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use Shopware\Deployment\Application;
use Shopware\Deployment\Config\ConfigFactory;
use Shopware\Deployment\Config\ProjectConfiguration;
use Shopware\Deployment\DependencyInjection\MySQLFactory;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$services = $container->services()
->defaults()
->autowire()
->autoconfigure()
->public();

$services->set('event_dispatcher', Symfony\Component\EventDispatcher\EventDispatcher::class);
$services->alias(EventDispatcherInterface::class, 'event_dispatcher');

$services->set(Doctrine\DBAL\Connection::class)
->factory([MySQLFactory::class, 'createAndRetry']);

$services->load('Shopware\\Deployment\\', '../../')
->exclude('../../{Application.php,ApplicationOutput.php,Struct,Resources}');

$services->set(Application::class)
->synthetic();

$services->set(ProjectConfiguration::class)
->factory([ConfigFactory::class, 'create'])
->args([
'%kernel.project_dir%',
service(Application::class),
]);
};
27 changes: 0 additions & 27 deletions src/Resources/config/services.xml

This file was deleted.

11 changes: 8 additions & 3 deletions src/Services/OneTimeTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Connection;
use Shopware\Deployment\Config\ProjectConfiguration;
use Shopware\Deployment\Helper\ProcessHelper;
use Shopware\Deployment\Struct\OneTimeTaskWhen;
use Symfony\Component\Console\Output\OutputInterface;

class OneTimeTasks
Expand All @@ -18,18 +19,22 @@ public function __construct(
) {
}

public function execute(OutputInterface $output): void
public function execute(OutputInterface $output, OneTimeTaskWhen $when): void
{
$executed = $this->getExecutedTasks();

foreach ($this->configuration->oneTimeTasks as $id => $script) {
foreach ($this->configuration->oneTimeTasks as $id => $task) {
if ($task->when !== $when) {
continue;
}

if (isset($executed[$id])) {
continue;
}

$output->writeln('Running one-time task ' . $id);

$this->processHelper->runAndTail($script);
$this->processHelper->runAndTail($task->script);

$this->markAsRun($id);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Services/UpgradeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Shopware\Deployment\Config\ProjectConfiguration;
use Shopware\Deployment\Helper\EnvironmentHelper;
use Shopware\Deployment\Helper\ProcessHelper;
use Shopware\Deployment\Struct\OneTimeTaskWhen;
use Shopware\Deployment\Struct\RunConfiguration;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -32,6 +33,9 @@ public function run(RunConfiguration $configuration, OutputInterface $output): v

$this->hookExecutor->execute(HookExecutor::HOOK_PRE_UPDATE);

// Execute one-time tasks that should run before the update
$this->oneTimeTasks->execute($output, OneTimeTaskWhen::BEFORE);

if ($this->configuration->maintenance->enabled) {
$this->state->enableMaintenanceMode();

Expand Down Expand Up @@ -89,7 +93,8 @@ public function run(RunConfiguration $configuration, OutputInterface $output): v
$this->processHelper->console(['theme:compile', '--active-only']);
}

$this->oneTimeTasks->execute($output);
// Execute one-time tasks that should run after the update
$this->oneTimeTasks->execute($output, OneTimeTaskWhen::AFTER);

$this->hookExecutor->execute(HookExecutor::HOOK_POST_UPDATE);

Expand Down
15 changes: 15 additions & 0 deletions src/Struct/OneTimeTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Shopware\Deployment\Struct;

readonly class OneTimeTask
{
public function __construct(
public string $id,
public string $script,
public OneTimeTaskWhen $when = OneTimeTaskWhen::AFTER,
) {
}
}
17 changes: 17 additions & 0 deletions src/Struct/OneTimeTaskWhen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Shopware\Deployment\Struct;

/**
* Defines when a one-time task should be executed during deployment.
*
* - BEFORE: Execute before the Shopware update (system:update) is run
* - AFTER: Execute after the Shopware update is complete (default)
*/
enum OneTimeTaskWhen: string
{
case BEFORE = 'before';
case AFTER = 'after';
}
33 changes: 29 additions & 4 deletions tests/Config/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Shopware\Deployment\Application;
use Shopware\Deployment\Config\ConfigFactory;
use Shopware\Deployment\Config\ProjectExtensionManagement;
use Shopware\Deployment\Struct\OneTimeTaskWhen;
use Zalas\PHPUnit\Globals\Attribute\Env;

#[CoversClass(ConfigFactory::class)]
Expand Down Expand Up @@ -45,7 +46,11 @@ public function testExistingConfigTest(string $configDir): void
$config = ConfigFactory::create($configDir, $this->createMockApplication());
static::assertTrue($config->extensionManagement->enabled);
static::assertSame('ignore', $config->extensionManagement->overrides['Name']['state']);
static::assertSame(['foo' => 'test'], $config->oneTimeTasks);
static::assertArrayHasKey('foo', $config->oneTimeTasks);
static::assertInstanceOf(\Shopware\Deployment\Struct\OneTimeTask::class, $config->oneTimeTasks['foo']);
static::assertSame('foo', $config->oneTimeTasks['foo']->id);
static::assertSame('test', $config->oneTimeTasks['foo']->script);
static::assertSame(OneTimeTaskWhen::AFTER, $config->oneTimeTasks['foo']->when);
static::assertNotSame('', $config->hooks->pre);
static::assertNotSame('', $config->hooks->post);
static::assertNotSame('', $config->hooks->preInstall);
Expand Down Expand Up @@ -132,7 +137,9 @@ public function testCreateWithProjectConfigOption(): void
$config = ConfigFactory::create(__DIR__, $this->createMockApplication($customConfigPath));

static::assertTrue($config->extensionManagement->enabled);
static::assertSame(['foo' => 'test'], $config->oneTimeTasks);
static::assertArrayHasKey('foo', $config->oneTimeTasks);
static::assertInstanceOf(\Shopware\Deployment\Struct\OneTimeTask::class, $config->oneTimeTasks['foo']);
static::assertSame('test', $config->oneTimeTasks['foo']->script);
}

public function testCreateWithProjectConfigOptionRelativePath(): void
Expand All @@ -141,7 +148,9 @@ public function testCreateWithProjectConfigOptionRelativePath(): void
$config = ConfigFactory::create(__DIR__ . '/_fixtures', $this->createMockApplication('yml/.shopware-project.yml'));

static::assertTrue($config->extensionManagement->enabled);
static::assertSame(['foo' => 'test'], $config->oneTimeTasks);
static::assertArrayHasKey('foo', $config->oneTimeTasks);
static::assertInstanceOf(\Shopware\Deployment\Struct\OneTimeTask::class, $config->oneTimeTasks['foo']);
static::assertSame('test', $config->oneTimeTasks['foo']->script);
}

#[Env('SHOPWARE_PROJECT_CONFIG_FILE', '_fixtures/yml/.shopware-project.yml')]
Expand All @@ -151,7 +160,9 @@ public function testEnvironmentVariableOverridesProjectConfigOption(): void
$config = ConfigFactory::create(__DIR__, $this->createMockApplication('some-other-config.yml'));

// Should load the config from environment variable, not the CLI option
static::assertSame(['foo' => 'test'], $config->oneTimeTasks);
static::assertArrayHasKey('foo', $config->oneTimeTasks);
static::assertInstanceOf(\Shopware\Deployment\Struct\OneTimeTask::class, $config->oneTimeTasks['foo']);
static::assertSame('test', $config->oneTimeTasks['foo']->script);
}

public function testCreateWithNonExistentProjectConfig(): void
Expand All @@ -164,4 +175,18 @@ public function testCreateWithNonExistentProjectConfig(): void
static::assertSame([], $config->extensionManagement->overrides);
static::assertSame([], $config->oneTimeTasks);
}

public function testOneTimeTasksWithWhenField(): void
{
$config = ConfigFactory::create(__DIR__ . '/_fixtures/maintenance-mode', $this->createMockApplication());

static::assertArrayHasKey('foo', $config->oneTimeTasks);
static::assertSame(OneTimeTaskWhen::AFTER, $config->oneTimeTasks['foo']->when);

static::assertArrayHasKey('early-task', $config->oneTimeTasks);
static::assertSame(OneTimeTaskWhen::BEFORE, $config->oneTimeTasks['early-task']->when);

static::assertArrayHasKey('late-task', $config->oneTimeTasks);
static::assertSame(OneTimeTaskWhen::AFTER, $config->oneTimeTasks['late-task']->when);
}
}
6 changes: 6 additions & 0 deletions tests/Config/_fixtures/maintenance-mode/.shopware-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ deployment:
one-time-tasks:
- id: foo
script: test
- id: early-task
script: echo "Running early"
when: before
- id: late-task
script: echo "Running late"
when: after
Loading