Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Console CLI #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ All modules can communicate with the other modules using their API endpoints.
* **html_admin** - A folder holding front-end templates (`*.html.twig files`) for the administrator panel.
* **html_client** - A folder holding front-end templates (`*.html.twig files`) for the client / guest area.

### Commands CLI folder (Optional)

* **ClassConsole.php** - A file where you can run CLI console by ```php console.php your:cli```
* Multiple files can be added to the folder as long as they have different names and do not conflict with existing ones.

### Controller folder

* **Admin.php** - Defines the module's routes and navigation items for the administrator panel.
Expand Down
69 changes: 69 additions & 0 deletions src/Commands/ExampleDI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* FOSSBilling.
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license Apache-2.0
*
* Copyright FOSSBilling 2022
* This software may contain code previously used in the BoxBilling project.
* Copyright BoxBilling, Inc 2011-2021
*
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/

namespace Box\Mod\Example\Commands;

use Pimple\Container;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'example:clear',
description: 'Clear your cache for example',
hidden: false
)]
class ExampleDI extends Command implements \FOSSBilling\InjectionAwareInterface
{
protected ?Container $di = null;

/**
* @param Container $di
* @return void
*/
public function setDi(Container $di): void
{
$this->di = $di;
}

/**
* @return Container|null
*/
public function getDi(): ?Container
{
return $this->di;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$service = $this->di['mod_service']('system');
try {
$service->clearCache();
} catch (\Exception $e) {
$output->writeln('<error>An error occurred: ' . $e->getMessage() . '</error>');
return Command::FAILURE;
} finally {
$output->writeln('<info>Successfully cleared the cache.</info>');
return Command::SUCCESS;
}
}
}
61 changes: 61 additions & 0 deletions src/Commands/ExampleError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* FOSSBilling.
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license Apache-2.0
*
* Copyright FOSSBilling 2022
* This software may contain code previously used in the BoxBilling project.
* Copyright BoxBilling, Inc 2011-2021
*
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/

namespace Box\Mod\Example\Commands;

use Pimple\Container;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'example:error',
description: 'Return an error message',
hidden: false
)]
class ExampleError extends Command implements \FOSSBilling\InjectionAwareInterface
{
protected ?Container $di = null;

/**
* @param Container $di
* @return void
*/
public function setDi(Container $di): void
{
$this->di = $di;
}

/**
* @return Container|null
*/
public function getDi(): ?Container
{
return $this->di;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$output->writeln('<error>This is your example error console</error>');
} catch (\Exception $e) {
$output->writeln('<error>This is your example error console triggered by an exception</error>');
return Command::FAILURE;
}
return Command::SUCCESS;
}
}
56 changes: 56 additions & 0 deletions src/Commands/ExampleInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* FOSSBilling.
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license Apache-2.0
*
* Copyright FOSSBilling 2022
* This software may contain code previously used in the BoxBilling project.
* Copyright BoxBilling, Inc 2011-2021
*
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/

namespace Box\Mod\Example\Commands;

use Pimple\Container;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'example:info',
description: 'Return an info message',
hidden: false
)]
class ExampleInfo extends Command implements \FOSSBilling\InjectionAwareInterface
{
protected ?Container $di = null;

/**
* @param Container $di
* @return void
*/
public function setDi(Container $di): void
{
$this->di = $di;
}

/**
* @return Container|null
*/
public function getDi(): ?Container
{
return $this->di;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('<info>This is your example info.</info>');
return Command::SUCCESS;
}
}