mirror of
https://github.com/slawkens/myaac.git
synced 2025-10-16 02:34:54 +02:00
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:
29
system/src/Commands/CacheClearCommand.php
Normal file
29
system/src/Commands/CacheClearCommand.php
Normal 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;
|
||||
}
|
||||
}
|
18
system/src/Commands/Command.php
Normal file
18
system/src/Commands/Command.php
Normal 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();
|
||||
}
|
||||
}
|
29
system/src/Commands/CronjobCommand.php
Normal file
29
system/src/Commands/CronjobCommand.php
Normal 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;
|
||||
}
|
||||
}
|
60
system/src/Commands/CronjobInstallCommand.php
Normal file
60
system/src/Commands/CronjobInstallCommand.php
Normal 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;
|
||||
}
|
||||
}
|
78
system/src/Commands/MailSendCommand.php
Normal file
78
system/src/Commands/MailSendCommand.php
Normal 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;
|
||||
}
|
||||
}
|
50
system/src/Commands/PluginInstallCommand.php
Normal file
50
system/src/Commands/PluginInstallCommand.php
Normal 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;
|
||||
}
|
||||
}
|
38
system/src/Commands/PluginInstallInstallCommand.php
Normal file
38
system/src/Commands/PluginInstallInstallCommand.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user