Skip to content

Commit

Permalink
install craft in the same process so ENV vars are carried through
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Oct 16, 2024
1 parent 6c98272 commit 18e12a3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
59 changes: 59 additions & 0 deletions src/actions/InstallCraft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace markhuot\craftpest\actions;

use Craft;
use craft\console\Application;
use craft\console\controllers\InstallController;
use craft\helpers\App;
use craft\helpers\ArrayHelper;

class InstallCraft
{
public function __invoke(): void
{
$args = [
'username' => App::env('CRAFT_INSTALL_USERNAME') ?? '[email protected]',
'email' => App::env('CRAFT_INSTALL_EMAIL') ?? '[email protected]',
'password' => App::env('CRAFT_INSTALL_PASSWORD') ?? 'secret',
'interactive' => App::env('CRAFT_INSTALL_INTERACTIVE') ?? '0',
];

if (! file_exists(Craft::getAlias('@config/project/project.yaml'))) {
$args = array_merge($args, [
'siteName' => App::env('CRAFT_INSTALL_SITENAME') ?? '"Craft CMS"',
'siteUrl' => App::env('CRAFT_INSTALL_SITEURL') ?? 'http://localhost:8080',
'language' => App::env('CRAFT_INSTALL_LANGUAGE') ?? 'en-US',
]);
}

$getConfig = function (string $path) {
if (file_exists($path)) {
return require $path;
}

return [];
};

$config = ArrayHelper::merge(
$getConfig(Craft::$app->getPath()->getVendorPath().'/craftcms/cms/src/config/app.php'),
$getConfig(Craft::$app->getPath()->getVendorPath().'/craftcms/cms/src/config/app.console.php'),
$getConfig(Craft::$app->getPath()->getConfigPath().'/app.php'),
$getConfig(Craft::$app->getPath()->getConfigPath().'/app.console.php'),
[
'id' => 'CraftCMSConsole',
'name' => 'Craft CMS Console',
'components' => [
'config' => Craft::$app->getConfig(),
],
],
);
unset($config['class']);

Craft::createObject(InstallController::class, [
'id' => 'foo',
'module' => new Application($config),
'config' => $args,
])->runAction('craft');
}
}
36 changes: 3 additions & 33 deletions src/test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace markhuot\craftpest\test;

use Craft;
use craft\helpers\App;
use Illuminate\Support\Collection;
use markhuot\craftpest\actions\CallSeeders;
use markhuot\craftpest\actions\InstallCraft;
use markhuot\craftpest\interfaces\RenderCompiledClassesInterface;
use Symfony\Component\Process\Process;
use function markhuot\craftpest\helpers\base\service;

class TestCase extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -106,38 +107,7 @@ public function createApplication()

protected function craftInstall()
{
$args = [
'--username='.(App::env('CRAFT_INSTALL_USERNAME') ?? '[email protected]'),
'--email='.(App::env('CRAFT_INSTALL_EMAIL') ?? '[email protected]'),
'--password='.(App::env('CRAFT_INSTALL_PASSWORD') ?? 'secret'),
'--interactive='.(App::env('CRAFT_INSTALL_INTERACTIVE') ?? '0'),
];

if (! file_exists(Craft::getAlias('@config/project/project.yaml'))) {
$args = array_merge($args, [
'--siteName='.(App::env('CRAFT_INSTALL_SITENAME') ?? '"Craft CMS"'),
'--siteUrl='.(App::env('CRAFT_INSTALL_SITEURL') ?? 'http://localhost:8080'),
'--language='.(App::env('CRAFT_INSTALL_LANGUAGE') ?? 'en-US'),
]);
}

$craftExePath = getenv('CRAFT_EXE_PATH') ?: './craft';
$process = new Process([$craftExePath, 'install', ...$args]);
$process->setTty(Process::isTtySupported());
$process->setTimeout(null);
$process->start();

foreach ($process as $type => $data) {
if ($type === $process::OUT) {
echo $data;
} else {
echo $data;
}
}

if (! $process->isSuccessful()) {
throw new \Exception('Failed installing Craft');
}
service(InstallCraft::class)();
}

protected function craftMigrateAll()
Expand Down

0 comments on commit 18e12a3

Please sign in to comment.