Skip to content

Commit

Permalink
Fix WebProfiler when dev mode is active
Browse files Browse the repository at this point in the history
  • Loading branch information
boherm committed Sep 20, 2023
1 parent 2a94cbf commit 38c10d7
Show file tree
Hide file tree
Showing 2 changed files with 321 additions and 2 deletions.
317 changes: 317 additions & 0 deletions src/PrestaShopBundle/DataCollector/ConfigDataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShopBundle\DataCollector;

use PrestaShop\PrestaShop\Core\Foundation\Version;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\VarDumper\Caster\ClassStub;

/**
* Class ConfigDataCollector.
* This class is used to collect some data for the WebProfiler.
*/
class ConfigDataCollector extends DataCollector implements LateDataCollectorInterface
{
public function __construct(
private readonly string $name,
private readonly Version $version
) {
}

/**
* @var KernelInterface
*/
private $kernel;

/**
* Sets the Kernel associated with this Request.
*/
public function setKernel(KernelInterface $kernel = null)
{
$this->kernel = $kernel;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
$eom = \DateTime::createFromFormat('d/m/Y', '01/' . Kernel::END_OF_MAINTENANCE);
$eol = \DateTime::createFromFormat('d/m/Y', '01/' . Kernel::END_OF_LIFE);

$this->data = [
'token' => $response->headers->get('X-Debug-Token'),
'symfony_version' => Kernel::VERSION,
'symfony_minor_version' => sprintf('%s.%s', Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION),
'symfony_lts' => 4 === Kernel::MINOR_VERSION,
'symfony_state' => $this->determineSymfonyState(),
'symfony_eom' => $eom->format('F Y'),
'symfony_eol' => $eol->format('F Y'),
'env' => $this->kernel->getEnvironment(),
'debug' => $this->kernel->isDebug(),
'php_version' => \PHP_VERSION,
'php_architecture' => \PHP_INT_SIZE * 8,
'php_intl_locale' => class_exists(\Locale::class, false) && \Locale::getDefault() ? \Locale::getDefault() : 'n/a',
'php_timezone' => date_default_timezone_get(),
'xdebug_enabled' => \extension_loaded('xdebug'),
'apcu_enabled' => \extension_loaded('apcu') && filter_var(\ini_get('apc.enabled'), \FILTER_VALIDATE_BOOLEAN),
'zend_opcache_enabled' => \extension_loaded('Zend OPcache') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN),
'bundles' => [],
'sapi_name' => \PHP_SAPI,
];

foreach ($this->kernel->getBundles() as $name => $bundle) {
$this->data['bundles'][$name] = new ClassStub(\get_class($bundle));
}

if (preg_match('~^(\d+(?:\.\d+)*)(.+)?$~', $this->data['php_version'], $matches)) {
if (isset($matches[2])) {
$this->data['php_version'] = $matches[1];
$this->data['php_version_extra'] = $matches[2];
}
}

$this->data['app_name'] = $this->name;
$this->data['app_version'] = $this->version->getSemVersion();
}

/**
* {@inheritdoc}
*/
public function reset()
{
$this->data = [];
}

public function lateCollect()
{
$this->data = $this->cloneVar($this->data);
}

/**
* Gets the token.
*/
public function getToken(): ?string
{
return $this->data['token'];
}

/**
* Gets the Symfony version.
*/
public function getSymfonyVersion(): string
{
return $this->data['symfony_version'];
}

/**
* Returns the state of the current Symfony release.
*
* @return string One of: unknown, dev, stable, eom, eol
*/
public function getSymfonyState(): string
{
return $this->data['symfony_state'];
}

/**
* Returns the minor Symfony version used (without patch numbers of extra
* suffix like "RC", "beta", etc.).
*/
public function getSymfonyMinorVersion(): string
{
return $this->data['symfony_minor_version'];
}

/**
* Returns if the current Symfony version is a Long-Term Support one.
*/
public function isSymfonyLts(): bool
{
return $this->data['symfony_lts'];
}

/**
* Returns the human readable date when this Symfony version ends its
* maintenance period.
*/
public function getSymfonyEom(): string
{
return $this->data['symfony_eom'];
}

/**
* Returns the human readable date when this Symfony version reaches its
* "end of life" and won't receive bugs or security fixes.
*/
public function getSymfonyEol(): string
{
return $this->data['symfony_eol'];
}

/**
* Gets the PHP version.
*/
public function getPhpVersion(): string
{
return $this->data['php_version'];
}

/**
* Gets the PHP version extra part.
*/
public function getPhpVersionExtra(): ?string
{
return $this->data['php_version_extra'] ?? null;
}

/**
* @return int The PHP architecture as number of bits (e.g. 32 or 64)
*/
public function getPhpArchitecture(): int
{
return $this->data['php_architecture'];
}

public function getPhpIntlLocale(): string
{
return $this->data['php_intl_locale'];
}

public function getPhpTimezone(): string
{
return $this->data['php_timezone'];
}

/**
* Gets the environment.
*/
public function getEnv(): string
{
return $this->data['env'];
}

/**
* Returns true if the debug is enabled.
*
* @return bool|string true if debug is enabled, false otherwise or a string if no kernel was set
*/
public function isDebug()
{
return $this->data['debug'];
}

/**
* Returns true if the XDebug is enabled.
*/
public function hasXDebug(): bool
{
return $this->data['xdebug_enabled'];
}

/**
* Returns true if APCu is enabled.
*/
public function hasApcu(): bool
{
return $this->data['apcu_enabled'];
}

/**
* Returns true if Zend OPcache is enabled.
*/
public function hasZendOpcache(): bool
{
return $this->data['zend_opcache_enabled'];
}

public function getBundles()
{
return $this->data['bundles'];
}

/**
* Gets the PHP SAPI name.
*/
public function getSapiName(): string
{
return $this->data['sapi_name'];
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'config';
}

/**
* Tries to retrieve information about the current Symfony version.
*
* @return string One of: dev, stable, eom, eol
*/
private function determineSymfonyState(): string
{
$now = new \DateTime();
$eom = \DateTime::createFromFormat('d/m/Y', '01/' . Kernel::END_OF_MAINTENANCE)->modify('last day of this month');
$eol = \DateTime::createFromFormat('d/m/Y', '01/' . Kernel::END_OF_LIFE)->modify('last day of this month');

if ($now > $eol) {
$versionState = 'eol';
} elseif ($now > $eom) {
$versionState = 'eom';
} elseif ('' !== Kernel::EXTRA_VERSION) { /** @phpstan-ignore-line */
$versionState = 'dev';
} else {
$versionState = 'stable';
}

return $versionState;
}

/**
* Get the application name.
*/
public function getApplicationName(): string
{
return $this->data['app_name'];
}

/**
* Get the application version.
*/
public function getApplicationVersion(): string
{
return $this->data['app_version'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ services:

# Web Profiler Bundle
data_collector.config:
class: Symfony\Component\HttpKernel\DataCollector\ConfigDataCollector
arguments: [ "PrestaShop", "@=service('prestashop.core.foundation.version').getSemVersion()" ]
class: PrestaShopBundle\DataCollector\ConfigDataCollector
arguments:
- 'PrestaShop'
- '@PrestaShop\PrestaShop\Core\Foundation\Version'
public: false
calls:
- [ "setKernel", [ "@kernel" ] ]
Expand Down

0 comments on commit 38c10d7

Please sign in to comment.