123 lines
3.6 KiB
PHP
123 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\DevelopmentBundle\View;
|
|
|
|
use KupShop\CatalogBundle\Search\FulltextElastic;
|
|
use KupShop\KupShopBundle\Config;
|
|
use KupShop\KupShopBundle\Util\System\PathFinder;
|
|
use KupShop\KupShopBundle\Views\Traits\RequestTrait;
|
|
use KupShop\KupShopBundle\Views\View;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class TestCleaningView extends View
|
|
{
|
|
use RequestTrait;
|
|
|
|
private array $errors = [
|
|
'stdout' => '',
|
|
'stderr' => '',
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly PathFinder $pathFinder,
|
|
private readonly FulltextElastic $fulltextElastic,
|
|
) {
|
|
}
|
|
|
|
public function clearSession(): void
|
|
{
|
|
$this->request->getSession()->clear();
|
|
}
|
|
|
|
public function runImportDemo(): void
|
|
{
|
|
$cfg = Config::get();
|
|
|
|
$host = $cfg['Connection']['host'];
|
|
$database = $cfg['Connection']['database'];
|
|
$user = $cfg['Connection']['user'];
|
|
$password = $cfg['Connection']['password'];
|
|
$shopPath = $this->pathFinder->getShopDir();
|
|
|
|
$scriptPath = $this->pathFinder->enginePath('bin/import_demo.sh');
|
|
$this->errors['stdout'] .= "Importing database {$database}".PHP_EOL;
|
|
$this->openProcess("bash -c '{$scriptPath} {$user} {$password} {$host} {$database} {$shopPath}'", function ($_, $stdout, $stderr) {
|
|
$this->errors['stdout'] .= stream_get_contents($stdout);
|
|
$this->errors['stderr'] .= stream_get_contents($stderr);
|
|
});
|
|
|
|
$this->importSharedDatabase();
|
|
|
|
$this->runUpgrade();
|
|
|
|
$this->fulltextElastic->updateIndex();
|
|
|
|
\Settings::clearCache(true);
|
|
}
|
|
|
|
private function runUpgrade(): void
|
|
{
|
|
$scriptPath = $this->pathFinder->enginePath('bin/symfony');
|
|
$this->errors['stdout'] .= 'Running kupshop:upgrade';
|
|
$this->openProcess("bash -c '{$scriptPath} kupshop:upgrade'", function ($_, $stdout, $stderr) {
|
|
$this->errors['stdout'] .= stream_get_contents($stdout);
|
|
$this->errors['stderr'] .= stream_get_contents($stderr);
|
|
});
|
|
}
|
|
|
|
private function importSharedDatabase(bool $force = false): void
|
|
{
|
|
if (!$force && $this->sharedDbExists()) {
|
|
return;
|
|
}
|
|
|
|
$cfg = Config::get();
|
|
|
|
$host = $cfg['Connection']['host'];
|
|
$user = $cfg['Connection']['user'];
|
|
$password = $cfg['Connection']['password'];
|
|
|
|
$upgradePath = $this->pathFinder->enginePath('bundles/KupShop/DevelopmentBundle/Resources/upgrade/database_shared.sql');
|
|
|
|
$this->openProcess("mysql -h{$host} -u{$user} -p{$password}", function ($stdin) use ($upgradePath) {
|
|
fwrite($stdin, file_get_contents($upgradePath));
|
|
});
|
|
}
|
|
|
|
public function getResponse(?Request $request = null): Response
|
|
{
|
|
return new JsonResponse($this->errors);
|
|
}
|
|
|
|
private function openProcess(string $command, callable $callback): int
|
|
{
|
|
$descriptorSpec = [
|
|
['pipe', 'r'], // stdin
|
|
['pipe', 'w'], // stdout
|
|
['pipe', 'w'], // stderr
|
|
];
|
|
|
|
$ph = proc_open($command, $descriptorSpec, $pipes);
|
|
if ($ph === false) {
|
|
throw new \Exception("Failed to execute {$command}!");
|
|
}
|
|
|
|
$callback(...$pipes);
|
|
|
|
foreach ($pipes as $pipe) {
|
|
fclose($pipe);
|
|
}
|
|
|
|
return proc_close($ph);
|
|
}
|
|
|
|
private function sharedDbExists(): bool
|
|
{
|
|
return sqlNumRows(sqlQuery("SHOW DATABASES LIKE '%kupshop_shared%';")) >= 1;
|
|
}
|
|
}
|