<commit_before><?php declare(strict_types=1);
/**
 * Hummingbird Anime List Client
 *
 * An API client for Kitsu to manage anime and manga watch lists
 *
 * PHP version 7.4
 *
 * @package     HummingbirdAnimeClient
 * @author      <NAME> <<EMAIL>>
 * @copyright   2015 - 2020  <NAME>
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
 * @version     5.1
 * @link        https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
 */

namespace Aviat\AnimeClient\Command;

use Monolog\Formatter\JsonFormatter;
use function Aviat\AnimeClient\loadToml;
use function Aviat\AnimeClient\loadTomlFile;

use Aura\Router\RouterContainer;
use Aura\Session\SessionFactory;
use Aviat\AnimeClient\{Model, UrlGenerator, Util};
use Aviat\AnimeClient\API\{Anilist, CacheTrait, Kitsu};
use Aviat\Banker\Teller;
use Aviat\Ion\Config;
use Aviat\Ion\Di\{Container, ContainerInterface, ContainerAware};
use ConsoleKit\{Colors, Command, ConsoleException};
use ConsoleKit\Widgets\Box;
use Laminas\Diactoros\{Response, ServerRequestFactory};
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

/**
 * Base class for console command setup
 */
abstract class BaseCommand extends Command {
	use CacheTrait;
	use ContainerAware;

	/**
	 * Echo text in a box
	 *
	 * @param string|array $message
	 * @param string|int|null $fgColor
	 * @param string|int|null $bgColor
	 * @return void
	 */
	public function echoBox($message, $fgColor = NULL, $bgColor = NULL): void
	{
		if (is_array($message))
		{
			$message = implode("\n", $message);
		}

		// color message
		$message = Colors::colorize($message, $fgColor, $bgColor);

		// create the box
		$box = new Box($this->getConsole(), $message);

		$box->write();

		echo "\n";
	}

	public function echo(string $message): void
	{
		$this->_line($message);
	}

	public function echoSuccess(string $message): void
	{
		$this->_line($message, Colors::GREEN | Colors::BOLD, Colors::BLACK);
	}

	public function echoWarning(string $message): void
	{
		$this->_line($message, Colors::YELLOW | Colors::BOLD, Colors::BLACK);
	}

	public function echoWarningBox(string $message): void
	{
		$this->echoBox($message, Colors::YELLOW | Colors::BOLD, Colors::BLACK);
	}

	public function echoError(string $message): void
	{
		$this->_line($message, Colors::RED | Colors::BOLD, Colors::BLACK);
	}

	public function echoErrorBox(string $message): void
	{
		$this->echoBox($message, Colors::RED | Colors::BOLD, Colors::BLACK);
	}

	public function clearLine(): void
	{
		$this->getConsole()->write("\r\e[2K");
	}

	/**
	 * Setup the Di container
	 *
	 * @return Containerinterface
	 */
	public function setupContainer(): ContainerInterface
	{
		$APP_DIR = realpath(__DIR__ . '/../../../app');
		$APPCONF_DIR = realpath("{$APP_DIR}/appConf/");
		$CONF_DIR = realpath("{$APP_DIR}/config/");
		$baseConfig = require $APPCONF_DIR . '/base_config.php';

		$config = loadToml($CONF_DIR);

		$overrideFile = $CONF_DIR . '/admin-override.toml';
		$overrideConfig = file_exists($overrideFile)
			? loadTomlFile($overrideFile)
			: [];

		$configArray = array_replace_recursive($baseConfig, $config, $overrideConfig);

		return $this->_di($configArray, $APP_DIR);
	}

	private function _line(string $message, $fgColor = NULL, $bgColor = NULL): void
	{
		$message = Colors::colorize($message, $fgColor, $bgColor);
		$this->getConsole()->writeln($message);
	}

	private function _di(array $configArray, string $APP_DIR): ContainerInterface
	{
		$container = new Container();

		// -------------------------------------------------------------------------
		// Logging
		// -------------------------------------------------------------------------

		$appLogger = new Logger('animeclient');
		$appLogger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/app-cli.log', 2, Logger::WARNING));
		$container->setLogger($appLogger);

		foreach (['anilist-request-cli', 'kitsu-request-cli'] as $channel)
		{
			$logger = new Logger($channel);
			$handler = new RotatingFileHandler( "{$APP_DIR}/logs/{$channel}.log", 2, Logger::WARNING);
			$handler->setFormatter(new JsonFormatter());
			$logger->pushHandler($handler);

			$container->setLogger($logger, $channel);<commit_msg>Fix setup of console commands<commit_after><?php declare(strict_types=1);
/**
 * Hummingbird Anime List Client
 *
 * An API client for Kitsu to manage anime and manga watch lists
 *
 * PHP version 7.4
 *
 * @package     HummingbirdAnimeClient
 * @author      <NAME> <<EMAIL>>
 * @copyright   2015 - 2020  <NAME>
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
 * @version     5.1
 * @link        https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
 */

namespace Aviat\AnimeClient\Command;

use Monolog\Formatter\JsonFormatter;
use function Aviat\AnimeClient\loadToml;
use function Aviat\AnimeClient\loadTomlFile;

use Aura\Router\RouterContainer;
use Aura\Session\SessionFactory;
use Aviat\AnimeClient\{Model, UrlGenerator, Util};
use Aviat\AnimeClient\API\{Anilist, CacheTrait, Kitsu};
use Aviat\Banker\Teller;
use Aviat\Ion\Config;
use Aviat\Ion\Di\{Container, ContainerInterface, ContainerAware};
use ConsoleKit\{Colors, Command, ConsoleException};
use ConsoleKit\Widgets\Box;
use Laminas\Diactoros\{Response, ServerRequestFactory};
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

/**
 * Base class for console command setup
 */
abstract class BaseCommand extends Command {
	use CacheTrait;
	use ContainerAware;

	/**
	 * Echo text in a box
	 *
	 * @param string|array $message
	 * @param string|int|null $fgColor
	 * @param string|int|null $bgColor
	 * @return void
	 */
	public function echoBox($message, $fgColor = NULL, $bgColor = NULL): void
	{
		if (is_array($message))
		{
			$message = implode("\n", $message);
		}

		// color message
		$message = Colors::colorize($message, $fgColor, $bgColor);

		// create the box
		$box = new Box($this->getConsole(), $message);

		$box->write();

		echo "\n";
	}

	public function echo(string $message): void
	{
		$this->_line($message);
	}

	public function echoSuccess(string $message): void
	{
		$this->_line($message, Colors::GREEN | Colors::BOLD, Colors::BLACK);
	}

	public function echoWarning(string $message): void
	{
		$this->_line($message, Colors::YELLOW | Colors::BOLD, Colors::BLACK);
	}

	public function echoWarningBox(string $message): void
	{
		$this->echoBox($message, Colors::YELLOW | Colors::BOLD, Colors::BLACK);
	}

	public function echoError(string $message): void
	{
		$this->_line($message, Colors::RED | Colors::BOLD, Colors::BLACK);
	}

	public function echoErrorBox(string $message): void
	{
		$this->echoBox($message, Colors::RED | Colors::BOLD, Colors::BLACK);
	}

	public function clearLine(): void
	{
		$this->getConsole()->write("\r\e[2K");
	}

	/**
	 * Setup the Di container
	 *
	 * @return Containerinterface
	 */
	public function setupContainer(): ContainerInterface
	{
		$APP_DIR = realpath(__DIR__ . '/../../../app');
		$APPCONF_DIR = realpath("{$APP_DIR}/appConf/");
		$CONF_DIR = realpath("{$APP_DIR}/config/");
		$baseConfig = require $APPCONF_DIR . '/base_config.php';

		$config = loadToml($CONF_DIR);

		$overrideFile = $CONF_DIR . '/admin-override.toml';
		$overrideConfig = file_exists($overrideFile)
			? loadTomlFile($overrideFile)
			: [];

		$configArray = array_replace_recursive($baseConfig, $config, $overrideConfig);

		return $this->_di($configArray, $APP_DIR);
	}

	private function _line(string $message, $fgColor = NULL, $bgColor = NULL): void
	{
		$message = Colors::colorize($message, $fgColor, $bgColor);
		$this->getConsole()->writeln($message);
	}

	private function _di(array $configArray, string $APP_DIR): ContainerInterface
	{
		$container = new Container();

		// -------------------------------------------------------------------------
		// Logging
		// -------------------------------------------------------------------------

		$appLogger = new Logger('animeclient');
		$appLogger->pushHandler(new RotatingFileHandler($APP_DIR . '/logs/app-cli.log', 2, Logger::WARNING));
		$container->setLogger($appLogger);

		foreach (['kitsu-request', 'anilist-request', 'anilist-request-cli', 'kitsu-request-cli'] as $channel)
		{
			$logger = new Logger($channel);
			$handler = new RotatingFileHandler( "{$APP_DIR}/logs/{$channel}.log", 2, Logger::WARNING);
			$handler->setFormatter(new JsonFormatter());
			$logger->pushHandler($handler);

			$container->setLogger($logger, $channel);
{'fraction_non_alphanumeric': 0.1850189035916824, 'fraction_numerical': 0.004017013232514178, 'mean_word_length': 3.8233618233618234, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2479161', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2687, 'n_words': 577}