Revised Commands -> use symfony/console -> php ma (MyAAC)

Usage:
php ma list
php ma cache:clear
php ma plugin:install guild-wars.zip

More sophisticated:
echo "Hello, this is hello world message" | php ma mail:send test@test.com --subject "This is subject"

Also: custom commands can be added via Plugins: just need to return new class instance that extends \MyAAC\Commands\Command in plugins/*/commands folder
This commit is contained in:
slawkens
2024-01-26 23:19:39 +01:00
parent c59bacea93
commit 410d75c882
16 changed files with 342 additions and 222 deletions

View File

@@ -0,0 +1,29 @@
<?php
namespace MyAAC\Commands;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class CacheClearCommand extends Command
{
protected function configure(): void
{
$this->setName('cache:clear')
->setDescription('This command clears the cache');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if (!clearCache()) {
$io->error('Unknown error on clear cache');
return Command::FAILURE;
}
$io->success('Cache cleared');
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace MyAAC\Commands;
use MyAAC\Hooks;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
class Command extends SymfonyCommand
{
protected Hooks $hooks;
public function __construct() {
parent::__construct();
$this->hooks = new Hooks();
$this->hooks->load();
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace MyAAC\Commands;
use GO\Scheduler;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CronjobCommand extends Command
{
protected function configure(): void
{
$this->setName('cronjob')
->setDescription('This command runs cron tasks');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Create a new scheduler
$scheduler = new Scheduler();
$this->hooks->trigger(HOOK_CRONJOB, ['scheduler' => $scheduler]);
// Let the scheduler execute jobs which are due.
$scheduler->run();
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace MyAAC\Commands;
use GO\Scheduler;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class CronjobInstallCommand extends Command
{
protected function configure(): void
{
$this->setName('cronjob:install')
->setDescription('This command automatically registers into your crontab');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if (MYAAC_OS !== 'LINUX') {
$io->error('This script can be run only on linux.');
return 2;
}
$job = '* * * * * /usr/bin/php ' . SYSTEM . 'bin/cronjob.php >> ' . SYSTEM . 'logs/cron.log 2>&1';
if ($this->cronjobExists($job)) {
$io->info('MyAAC cronjob already installed.');
return Command::FAILURE;
}
exec('crontab -l', $content);
$content = implode(' ', $content);
$content .= PHP_EOL . $job;
file_put_contents(CACHE . 'cronjob', $content . PHP_EOL);
exec('crontab ' . CACHE. 'cronjob');
$io->success('Installed crontab successfully.');
return Command::SUCCESS;
}
private function cronjobExists($command): bool
{
exec('crontab -l', $crontab);
if(isset($crontab) && is_array($crontab)) {
$crontab = array_flip($crontab);
if(isset($crontab[$command])){
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace MyAAC\Commands;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class MailSendCommand extends Command
{
protected function configure(): void
{
$this->setName('mail:send')
->setDescription('This command sends E-Mail to single user. Message can be provided as follows: ' . PHP_EOL
. ' echo "Hello World" | php sa email:send --subject="This is the subject" test@test.com')
->addArgument('recipient', InputArgument::REQUIRED, 'Email, Account Name, Account id or Player Name')
->addOption('subject', 's', InputOption::VALUE_REQUIRED, 'Subject');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$email_account_name = $input->getArgument('recipient');
$subject = $input->getOption('subject');
if (!$subject) {
$io->error('Please specify subject via -s or --subject="" option');
return 2;
}
$message = file_get_contents('php://stdin');
if(!str_contains($email_account_name, '@')) {
$account = new \OTS_Account();
if(USE_ACCOUNT_NAME) {
$account->find($email_account_name);
}
else {
$account->load($email_account_name);
}
if($account->isLoaded()) {
$email_account_name = $account->getEMail();
}
else {
$player = new \OTS_Player();
$player->find($email_account_name);
if($player->isLoaded()) {
$email_account_name = $player->getAccount()->getEMail();
}
else {
$io->error('Cannot find player or account with name: ' . $email_account_name);
return 3;
}
}
}
if(!\Validator::email($email_account_name)) {
$io->error('Invalid E-Mail format');
return 4;
}
if(strlen($subject) > 255) {
$io->error('Subject max length is 255 characters');
return 5;
}
if(!_mail($email_account_name, $subject, $message)) {
$io->error('An error occurred while sending email. More info can be found in system/logs/mailer-error.log');
return 6;
}
$io->success('Mail sent to ' . $email_account_name . '.');
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace MyAAC\Commands;
use MyAAC\Plugins;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class PluginInstallCommand extends Command
{
protected function configure(): void
{
$this->setName('plugin:install')
->setDescription('This command installs plugin')
->addArgument('plugin', InputArgument::REQUIRED, 'Path to zip file (plugin) that you want to install');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$pathToFile = $input->getArgument('plugin');
$ext = strtolower(pathinfo($pathToFile, PATHINFO_EXTENSION));
if($ext !== 'zip') {// check if it is zipped/compressed file
$io->error('Please install only .zip files');
return 2;
}
if(!file_exists($pathToFile)) {
$io->error('File ' . $pathToFile . ' does not exist');
return 3;
}
if(!Plugins::install($pathToFile)){
$io->error(Plugins::getError());
return 4;
}
foreach(Plugins::getWarnings() as $warning) {
$io->warning($warning);
}
$info = Plugins::getPluginJson();
$io->success((isset($info['name']) ? $info['name'] . ' p' : 'P') . 'lugin has been successfully installed.');
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace MyAAC\Commands;
use MyAAC\Plugins;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class PluginInstallInstallCommand extends Command
{
protected function configure(): void
{
$this->setName('plugin:install:install')
->setDescription('This command executes the "install" part of the plugin')
->addArgument('plugin', InputArgument::REQUIRED, 'Plugin name');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$pluginName = $input->getArgument('plugin');
if(!Plugins::executeInstall($pluginName)) {
$io->error(Plugins::getError());
return 2;
}
foreach(Plugins::getWarnings() as $warning) {
$io->warning($warning);
}
$info = Plugins::getPluginJson($pluginName);
$io->success('Script for install ' . (isset($info['name']) ? $info['name'] . ' p' : 'P') . 'lugin has been successfully executed.');
return Command::SUCCESS;
}
}