Skip to content

wip #4145

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

Draft
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
Draft

wip #4145

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
1 change: 1 addition & 0 deletions framework/core/src/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function register(): void
HttpMiddleware\ReferrerPolicyHeader::class,
HttpMiddleware\ContentTypeOptionsHeader::class,
Middleware\DisableBrowserCache::class,
Middleware\GatherDebugInformation::class,
];
});

Expand Down
66 changes: 66 additions & 0 deletions framework/core/src/Admin/Middleware/GatherDebugInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Admin\Middleware;

use Flarum\Settings\SettingsRepositoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;

class GatherDebugInformation implements Middleware
{
public function __construct(protected SettingsRepositoryInterface $settings)
{
}

public function process(Request $request, Handler $handler): Response
{
$this->upsertWebUser();

$this->upsertOpCacheStatus();

return $handler->handle($request);
}

/**
* Read current web user, so we can compare that against CLI executions,
* these often cause file permission issues.
*/
public function upsertWebUser(): void
{
$user = $this->settings->get('core.debug.web_user');
$currentUser = get_current_user();

if ($user !== $currentUser) {
$this->settings->set(
'core.debug.web_user',
$currentUser
);
}
}

/**
* Read the opcache situation, this is only visible in web.
* Cli has opcache disabled by default.
*/
public function upsertOpCacheStatus(): void
{
$opcache = $this->settings->get('core.debug.opcache');
$opcacheStatus = function_exists('opcache_get_configuration') && opcache_get_configuration() !== false ? 'on' : 'off';

if ($opcache !== $opcacheStatus) {
$this->settings->set(
'core.debug.opcache',
$opcacheStatus
);
}
}
}
17 changes: 10 additions & 7 deletions framework/core/src/Foundation/ApplicationInfoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ public function scheduledTasksRegistered(): bool
return count($this->schedule->events()) > 0;
}

public function getSchedulerStatus(): string
public function getSchedulerStatus(bool $translated = true): string
{
$status = $this->settings->get('schedule.last_run');

if (! $status) {
return $this->translator->trans('core.admin.dashboard.status.scheduler.never-run');
$key = match (true) {
! $status => 'never-run',
Carbon::parse($status) > Carbon::now()->subMinutes(5) => 'active',
default => 'inactive'
};

if ($translated) {
return $this->translator->trans("core.admin.dashboard.status.scheduler.$key");
}

// If the schedule has not run in the last 5 minutes, mark it as inactive.
return Carbon::parse($status) > Carbon::now()->subMinutes(5)
? $this->translator->trans('core.admin.dashboard.status.scheduler.active')
: $this->translator->trans('core.admin.dashboard.status.scheduler.inactive');
return $key;
}

public function identifyQueueDriver(): string
Expand Down
97 changes: 9 additions & 88 deletions framework/core/src/Foundation/Console/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,15 @@
namespace Flarum\Foundation\Console;

use Flarum\Console\AbstractCommand;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application;
use Flarum\Foundation\ApplicationInfoProvider;
use Flarum\Foundation\Config;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Database\ConnectionInterface;
use Flarum\Foundation\Info\Renderer\CliRenderer;
use Flarum\Foundation\Info\Report;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableStyle;

class InfoCommand extends AbstractCommand
{
public function __construct(
protected ExtensionManager $extensions,
protected Config $config,
protected SettingsRepositoryInterface $settings,
protected ConnectionInterface $db,
protected ApplicationInfoProvider $appInfo
protected Container $container
) {
parent::__construct();
}
Expand All @@ -41,83 +32,13 @@ protected function configure(): void

protected function fire(): int
{
$coreVersion = $this->findPackageVersion(__DIR__.'/../../../', Application::VERSION);
$this->output->writeln("<info>Flarum core:</info> $coreVersion");
$report = new Report(
new CliRenderer($this->output),
$this->container
);

$this->output->writeln('<info>PHP version:</info> '.$this->appInfo->identifyPHPVersion());
$this->output->writeln('<info>'.$this->appInfo->identifyDatabaseDriver().' version:</info> '.$this->appInfo->identifyDatabaseVersion());

$phpExtensions = implode(', ', get_loaded_extensions());
$this->output->writeln("<info>Loaded extensions:</info> $phpExtensions");

$this->getExtensionTable()->render();

$this->output->writeln('<info>Base URL:</info> '.$this->config->url());
$this->output->writeln('<info>Installation path:</info> '.getcwd());
$this->output->writeln('<info>Queue driver:</info> '.$this->appInfo->identifyQueueDriver());
$this->output->writeln('<info>Session driver:</info> '.$this->appInfo->identifySessionDriver());

if ($this->appInfo->scheduledTasksRegistered()) {
$this->output->writeln('<info>Scheduler status:</info> '.$this->appInfo->getSchedulerStatus());
}

$this->output->writeln('<info>Mail driver:</info> '.$this->settings->get('mail_driver', 'unknown'));
$this->output->writeln('<info>Debug mode:</info> '.($this->config->inDebugMode() ? '<error>ON</error>' : 'off'));

if ($this->config->inDebugMode()) {
$this->output->writeln('');
$this->error(
"Don't forget to turn off debug mode! It should never be turned on in a production system."
);
}
$report->render();

return Command::SUCCESS;
}

private function getExtensionTable(): Table
{
$table = (new Table($this->output))
->setHeaders([
['Flarum Extensions'],
['ID', 'Version', 'Commit']
])->setStyle(
(new TableStyle)->setCellHeaderFormat('<info>%s</info>')
);

foreach ($this->extensions->getEnabledExtensions() as $extension) {
$table->addRow([
$extension->getId(),
$extension->getVersion(),
$this->findPackageVersion($extension->getPath())
]);
}

return $table;
}

/**
* Try to detect a package's exact version.
*
* If the package seems to be a Git version, we extract the currently
* checked out commit using the command line.
*/
private function findPackageVersion(string $path, ?string $fallback = null): ?string
{
if (file_exists("$path/.git")) {
$cwd = getcwd();
chdir($path);

$output = [];
$status = null;
exec('git rev-parse HEAD 2>&1', $output, $status);

chdir($cwd);

if ($status == 0) {
return isset($fallback) ? "$fallback ($output[0])" : $output[0];
}
}

return $fallback;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Foundation\Info\Concerns;

trait PackageVersionFromPath
{
/**
* Try to detect a package's exact version.
*
* If the package seems to be a Git version, we extract the currently
* checked out commit using the command line.
*/
private function findPackageVersion(string $path, ?string $fallback = null): ?string
{
if (file_exists("$path/.git")) {
$cwd = getcwd();
chdir($path);

$output = [];
$status = null;
exec('git rev-parse HEAD 2>&1', $output, $status);

chdir($cwd);

if ($status == 0) {
return isset($fallback) ? "$fallback ($output[0])" : $output[0];
}
}

return $fallback;
}
}
54 changes: 54 additions & 0 deletions framework/core/src/Foundation/Info/Renderer/CliRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Foundation\Info\Renderer;

use Flarum\Foundation\Info\RendererInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableStyle;
use Symfony\Component\Console\Output\OutputInterface;

class CliRenderer implements RendererInterface
{
public function __construct(protected OutputInterface $output)
{
}

public function heading(string $title): void
{
$this->output->writeln("<bg=gray>$title</>");
}

public function keyValue(string $key, mixed $value): void
{
$this->output->writeln("<info>$key:</info> $value");
}

public function table(array $headers, array $rows): void
{
$table = (new Table($this->output))
->setHeaders($headers)->setStyle(
(new TableStyle)->setCellHeaderFormat('<info>%s</info>')
);

foreach ($rows as $row) {
$table->addRow($row);
}

$table->render();
}

public function open(): void
{
}

public function close(): void
{
}
}
23 changes: 23 additions & 0 deletions framework/core/src/Foundation/Info/RendererInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Foundation\Info;

interface RendererInterface
{
public function heading(string $title): void;

public function keyValue(string $key, mixed $value): void;

public function table(array $headers, array $rows): void;

public function open(): void;

public function close(): void;
}
46 changes: 46 additions & 0 deletions framework/core/src/Foundation/Info/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Foundation\Info;

use Illuminate\Contracts\Container\Container;

class Report
{
protected array $sections = [
Section\CoreVersion::class,
Section\PHP::class,
Section\Database::class,
Section\EnabledExtensions::class,
Section\Features::class,
Section\Debug::class,
Section\ServiceUsers::class,
];

public function __construct(
protected RendererInterface $renderer,
protected Container $container
) {
}

public function render(): void
{
$sections = $this->sections;

$this->renderer->open();

foreach ($sections as &$section) {
$section = $this->container->make($section);

$section($this->renderer);
}

$this->renderer->close();
}
}
Loading
Loading