Skip to content

Commit

Permalink
clean the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 16, 2023
1 parent 981488c commit 3aac232
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
14 changes: 13 additions & 1 deletion src/Console/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayAccess\TrayDigita\Exceptions\InvalidArgument\UnsupportedArgumentException;
use ArrayAccess\TrayDigita\Traits\Container\ContainerAllocatorTrait;
use ArrayAccess\TrayDigita\Util\Filter\Consolidation;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -50,8 +51,10 @@ abstract class AbstractCommand extends Command implements ContainerAllocatorInte
/**
* @final
*/
final public function __construct()
final public function __construct(Application $application)
{
parent::setApplication($application);

$this->beforeConstruct();
$this->name = trim($this->name??'');
$this->description = trim($this->description??'');
Expand All @@ -70,6 +73,15 @@ final public function __construct()
$this->setDescription($this->description);
}

/**
* @param ?Application $application
* @return void
* no override
*/
final public function setApplication(Application $application = null): void
{
}

final public function setName(string $name): static
{
if (in_array($name, $this->reservedCommands)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Command/ApplicationChecker/ConfigChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ArrayAccess\TrayDigita\Console\Command\Traits\WriterHelperTrait;
use ArrayAccess\TrayDigita\Kernel\Decorator;
use ArrayAccess\TrayDigita\Kernel\Interfaces\KernelInterface;
use ArrayAccess\TrayDigita\PossibleRoot;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -149,9 +150,9 @@ protected function validatePath(InputInterface $input, OutputInterface $output)
&& is_dir(TD_APP_DIRECTORY)
? TD_APP_DIRECTORY
: null;
$root = ContainerHelper::use(KernelInterface::class)
->getRootDirectory();
if (!$path || !$directory) {
$root = ContainerHelper::use(KernelInterface::class)
?->getRootDirectory()??PossibleRoot::getPossibleRootDirectory();
$path = new Config();
$config->set('path', $path);
if (!$directory) {
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Command/BuiltInWebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use ArrayAccess\TrayDigita\Exceptions\Runtime\UnsupportedRuntimeException;
use ArrayAccess\TrayDigita\Kernel\Interfaces\KernelInterface;
use ArrayAccess\TrayDigita\PossibleRoot;
use ArrayAccess\TrayDigita\Traits\Container\ContainerAllocatorTrait;
use ArrayAccess\TrayDigita\Traits\Service\TranslatorTrait;
use ArrayAccess\TrayDigita\Util\Filter\Consolidation;
Expand Down Expand Up @@ -297,7 +298,7 @@ private function getRootDirectory() : string
return $this->rootDirectory;
}
$this->rootDirectory = ContainerHelper::use(KernelInterface::class, $this->getContainer())
?->getRootDirectory();
?->getRootDirectory()??PossibleRoot::getPossibleRootDirectory();
if ($this->rootDirectory && is_dir($this->rootDirectory)) {
return $this->rootDirectory = realpath($this->rootDirectory)?:$this->rootDirectory;
}
Expand Down Expand Up @@ -331,7 +332,6 @@ protected function doProcess(
$publicFile = $this->getPublicFile();
}


if (!$publicFile || !is_file($publicFile)) {
$output->writeln('');
$output->writeln(sprintf(
Expand Down
12 changes: 12 additions & 0 deletions src/Console/Command/MiddlewareGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ function ($name) {
);
}
$className = $definitions['className'];
if (count(explode('\\', $className)) > 1) {
$message = $this->translateContext(
'Middleware [%s] is invalid! Middleware only contain single class name, not namespaced!',
'console'
);
throw new InteractiveArgumentException(
sprintf(
$message,
$className
)
);
}
if (!Consolidation::allowedClassName($className)) {
throw new InteractiveArgumentException(
sprintf(
Expand Down
16 changes: 8 additions & 8 deletions src/View/ErrorRenderer/HtmlTraceAbleErrorRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ArrayAccess\TrayDigita\Exceptions\Runtime\MaximumCallstackExceeded;
use ArrayAccess\TrayDigita\Http\Exceptions\HttpException;
use ArrayAccess\TrayDigita\Kernel\Interfaces\KernelInterface;
use ArrayAccess\TrayDigita\PossibleRoot;
use ArrayAccess\TrayDigita\Util\Filter\Consolidation;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
use ArrayAccess\TrayDigita\View\Interfaces\ViewInterface;
Expand All @@ -19,23 +20,20 @@
use function explode;
use function file_exists;
use function get_class;
use function getcwd;
use function highlight_string;
use function htmlentities;
use function htmlspecialchars;
use function implode;
use function ini_set;
use function is_string;
use function json_encode;
use function nl2br;
use function phpinfo;
use function preg_match;
use function preg_quote;
use function preg_replace;
use function preg_replace_callback;
use function print_r;
use function realpath;
use function sprintf;
use function str_contains;
use function str_replace;
use function str_starts_with;
use function trim;
Expand All @@ -54,7 +52,7 @@ protected function format(
if ($displayErrorDetails) {
try {
$kernel = ContainerHelper::use(KernelInterface::class, $this->getContainer());
$root = $kernel?->getRootDirectory();
$root = $kernel?->getRootDirectory()??PossibleRoot::getPossibleRootDirectory();
if (!$root) {
$ref = new ReflectionClass(ClassLoader::class);
$this->rootDirectory = dirname($ref->getFileName(), 3);
Expand All @@ -64,7 +62,7 @@ protected function format(
} catch (Throwable) {
$serverParams = $request->getServerParams();
$this->rootDirectory = dirname(
$serverParams['DOCUMENT_ROOT'] ?? dirname($serverParams['SCRIPT_FILENAME'])
$serverParams['DOCUMENT_ROOT']??getcwd() // fallback cwd
);
}
return $this->exceptionDetail($exception);
Expand Down Expand Up @@ -272,6 +270,7 @@ private function fromTrace(array $trace, &$traceCount) : array
);
}
}
/** @noinspection HtmlUnknownAttribute */
$theLine .= sprintf(
'<span data-line="%2$d" %1$s>%2$d</span>',
$line === $trace['line'] ? ' class="current"' : '',
Expand All @@ -280,6 +279,7 @@ private function fromTrace(array $trace, &$traceCount) : array
if (trim($lineContent) === '') {
$lineContent = '<br>';
}
/** @noinspection HtmlUnknownAttribute */
$theLineContent .= sprintf(
'<div data-line="%d" %s>%s</div>',
$line,
Expand Down Expand Up @@ -750,8 +750,8 @@ private function exceptionDetail(Throwable $exception): string
}
.traced-content-line span[data-line],
.traced-content-details > div[data-line] {
padding-top: 0rem;
padding-bottom: 0rem;
padding-top: 0;
padding-bottom: 0;
}
.traced-content-line span.current,
.traced-content-details .current,
Expand Down

0 comments on commit 3aac232

Please sign in to comment.