Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Jun 26, 2024
1 parent f72493a commit 1d7b9c1
Show file tree
Hide file tree
Showing 29 changed files with 569 additions and 192 deletions.
23 changes: 23 additions & 0 deletions abstract/ArrayOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace panlatent\craft\actions\abstract;

class ArrayOutput implements OutputInterface
{
public function __construct(private array $arr, private string $template)
{

}

public function canStored(): bool
{
return true;
}

public function render(): string
{
return \Craft::$app->getView()->renderTemplate($this->template, $this->arr);
}


}
4 changes: 4 additions & 0 deletions abstract/ContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

interface ContextInterface
{
public function addError(string $attribute, string $error): void;

public function hasErrors(): bool;

public function getContainer(): ContainerInterface;

public function getErrors(): array;
Expand Down
2 changes: 1 addition & 1 deletion abstract/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ interface OutputInterface
{
public function canStored(): bool;

public function getSettings(): array;
public function render(): string;
}
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"require": {
"php": ">=8.2",
"craftcms/cms": "^5.0",
"craftcms/cms": "^5.0.0",
"dragonmantank/cron-expression": "^3.3",
"guzzlehttp/guzzle": "^7.2",
"nesbot/carbon": "^1.22 || ^2.10 || ^3.0",
Expand All @@ -35,7 +35,8 @@
"psr/log": "^1.0 || ^2.0 || ^3.0",
"react/event-loop": "^1.5",
"symfony/process": "^6.0 || ^7.0",
"alexanderpas/http-enum": "^1.0"
"alexanderpas/http-enum": "^1.0",
"symfony/stopwatch": "^7.1"
},
"replace": {
"panlatent/craft-action-abstract": "self.version"
Expand All @@ -56,7 +57,7 @@
"class": "panlatent\\schedule\\Plugin"
},
"require-dev": {
"codeception/codeception": "^4.1",
"codeception/codeception": "^5.1",
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main"
},
Expand All @@ -69,6 +70,6 @@
"craftcms/plugin-installer": true
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true
}
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private function _registerWebCron(): void

private function _registerUtilities(): void
{
Event::on(Utilities::class, Utilities::EVENT_REGISTER_UTILITIES, function(RegisterComponentTypesEvent $event) {
Event::on(Utilities::class, Utilities::EVENT_REGISTER_UTILITIES, static function(RegisterComponentTypesEvent $event) {
$event->types[] = ActionRunner::class;
});
}
Expand Down
93 changes: 0 additions & 93 deletions src/actions/Command.php

This file was deleted.

143 changes: 109 additions & 34 deletions src/actions/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,138 @@
namespace panlatent\schedule\actions;

use Craft;
use craft\helpers\App;
use panlatent\craft\actions\abstract\Action;
use panlatent\craft\actions\abstract\ContextInterface;
use panlatent\schedule\Plugin;
use Symfony\Component\Process\Process;

class Console extends Command
class Console extends Action
{
public function execute(ContextInterface $context): bool
{
// $process = new Process($this->buildCommand(), dirname(Craft::$app->request->getScriptFile()), null, null, $this->timeout ?: null);
//
// $process->run(function ($type, $buffer) use ($logId) {
// $output = $buffer . "\n";
// Craft::$app->getDb()->createCommand()
// ->update(Table::SCHEDULELOGS, [
// 'status' => self::STATUS_PROCESSING,
// 'output' => new Expression("CONCAT([[output]],:output)", ['output' => $output]),
// ], [
// 'id' => $logId,
// ])
// ->execute();
// });
public ?string $command = null;

public ?string $arguments = null;

public ?string $workDir = null;

public array $variables = [];

public ?string $osUser = null;

public ?int $timeout = null;

public bool $disableOutput = false;

// public bool $isPty = true;
//
// return $process->isSuccessful();
}
// public bool $isTty = false;

public function getSettingsHtml(): ?string
public function execute(ContextInterface $context): bool
{
return Craft::$app->getView()->renderTemplate('schedule/_components/actions/Console/settings', [
'schedule' => $this,
]);
$output = new StdOutput();
$context->setOutput($output);

$process = $this->createProcess();

$output = new StdOutput();
$context->setOutput($output);

$context->getLogger()->debug("Running command: \"{$process->getCommandLine()}\" in {$this->getWorkDir()}");
$process->run(function ($type, $buffer) use ($output) {
$type = Process::ERR === $type ? 'error' : 'info';
$output->writeln("<$type>$buffer</$type>");
});

if (!$process->isSuccessful()) {
$context->addError('exitCode', $process->getExitCodeText());
$context->addError('output', $process->getErrorOutput());
}

return !$context->hasErrors();
}

public function getCommandOptions(): array
public function getSettingsHtml(): ?string
{
$options = [];
$suggestions = [];

$process = new Process([Plugin::getInstance()->getSettings()->getCliPath(), 'craft', 'help'], Craft::getAlias('@root'));
$process = new Process([Plugin::getInstance()->getSettings()->getCliPath(), 'craft', 'help/list'], Craft::getAlias('@root'));
$process->run();

if ($process->isSuccessful()) {
$lines = explode("\n", mb_convert_encoding($process->getOutput(), mb_internal_encoding()));

$data = [];
foreach ($lines as $line) {
if (str_starts_with($line, '-')) {
$options[] = ['optgroup' => substr($line, 2)];
if (($pos = strpos($line, '/')) === false) {
$data[$line] = [];
continue;
}
if (preg_match('#^\s*(\w+/\w+)\s*(?:\(\w+\)|)\s+(.+)\s*$#', $line, $match)) {
$options[] = [
'label' => $match[1] . ' - ' . $match[2], //substr($line, 0, $pos),
'value' => $match[1],
];
}

$data[substr($line, 0, $pos)][] = [
'name' => $line,
'hint' => $line,
];
}

foreach ($data as $label => $commandSuggestions) {
$suggestions[] = [
'label' => $label,
'data' => $commandSuggestions,
];
}
}

return $options;
return Craft::$app->getView()->renderTemplate('schedule/_components/actions/Console/settings', [
'action' => $this,
'suggestions' => $suggestions,
]);
}

protected function createProcess(): Process
{
$command = $this->buildCommand();
if ($this->osUser) {
$command = array_merge(['sudo -u', $this->osUser], $command);
}

$process = new Process($command, $this->getWorkDir(), $this->getEnvironmentVariables(), null, $this->timeout ?: null);
// $process->setTty($this->isTty);
// $process->setPty($this->isPty);

if ($this->disableOutput) {
$process->disableOutput();
}

return $process;
}

protected function buildCommand(): array
{
$command = [$this->command];
if ($this->arguments) {
$command[] = $this->arguments;
}
return $command;
}

protected function defineRules(): array
{
return [
[['command'], 'required'],
];
}

protected function getWorkDir(): string
{
return $this->workDir ?: dirname(Craft::$app->request->getScriptFile());
}

private function getEnvironmentVariables(): array
{
$env = [];
foreach ($this->variables as $key => $value) {
$env[$key] = App::parseEnv($value);
}
return $env;
}
}
Loading

0 comments on commit 1d7b9c1

Please sign in to comment.