Skip to content

Commit

Permalink
[AdminBundle] Finish version checker deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
acrobat committed Jul 27, 2021
1 parent 59e187d commit cd9d86f
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -17,3 +17,4 @@ src/Kunstmaan/GeneratorBundle/Resources/SensioGeneratorBundle/skeleton/layout/gr
.php_cs
.php_cs.cache
.phpunit.result.cache
!src/Kunstmaan/AdminBundle/Tests/Helper/VersionCheck/testdata/composer_*/composer.lock
2 changes: 1 addition & 1 deletion UPGRADE-5.9.md
Expand Up @@ -67,7 +67,7 @@ security:
- { path: ^/([^/]*)/admin/reset.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
...
```

* The constructor arguments of `Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker` have changed, inject the correct/required services/parameters.

AdminlistBundle
------------
Expand Down
101 changes: 71 additions & 30 deletions src/Kunstmaan/AdminBundle/Helper/VersionCheck/VersionChecker.php
Expand Up @@ -10,18 +10,14 @@
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class VersionChecker
{
public const CACHE_KEY = 'version_check';

/**
* @var ContainerInterface
*/
private $container;

/**
* @var AdapterInterface
*/
Expand Down Expand Up @@ -52,35 +48,86 @@ class VersionChecker
*/
private $translator;

/** @var RequestStack */
private $requestStack;
/** @var string */
private $websiteTitle;
/** @var string */
private $projectDir;

/**
* @param CacheProvider|AdapterInterface $cache
* @param CacheProvider|AdapterInterface|ContainerInterface $cache
* @param TranslatorInterface|LegacyTranslatorInterface $translator
* @param RequestStack $requestStack
*/
public function __construct(ContainerInterface $container, /* AdapterInterface */ $cache, $translator)
{
$this->container = $container;
public function __construct(
/* ContainerInterface $container, */
/* AdapterInterface */ $cache,
$translator,
/* RequestStack */ $requestStack = null,
string $webserviceUrl = null,
int $cacheTimeframe = null,
bool $enabled = null,
string $projectDir = null,
string $websiteTitle = null
) {
if (func_num_args() === 3 && $cache instanceof ContainerInterface) {
@trigger_error(sprintf('Passing an instance of "%s" as the first argument in "%s" is deprecated since KunstmaanAdminBundle 5.9 and the service parameter types will change in KunstmaanAdminBundle 6.0. Check the constructor arguments and inject the required services and parameters instead.', ContainerInterface::class, __METHOD__), E_USER_DEPRECATED);
}

if (!$cache instanceof CacheProvider && !$cache instanceof AdapterInterface) {
if ((func_num_args() >= 2 && func_num_args() < 4) && !$cache instanceof ContainerInterface) {
// NEXT_MAJOR Remove check
throw new \InvalidArgumentException(sprintf('The first parameter of "%s" is not of the correct type, inject the correct services and parameters instead.', __METHOD__));
}

if (!$cache instanceof ContainerInterface && (!$cache instanceof CacheProvider && !$cache instanceof AdapterInterface)) {
// NEXT_MAJOR Add AdapterInterface typehint for the $cache parameter
throw new \InvalidArgumentException(sprintf('The "$cache" parameter should extend from "%s" or implement "%s"', CacheProvider::class, AdapterInterface::class));
}

$this->cache = $cache;
if ($cache instanceof CacheProvider) {
if ($cache instanceof ContainerInterface && (!$translator instanceof CacheProvider && !$translator instanceof AdapterInterface)) {
// NEXT_MAJOR Add AdapterInterface typehint for the $cache parameter
throw new \InvalidArgumentException(sprintf('The "$cache" parameter should extend from "%s" or implement "%s"', CacheProvider::class, AdapterInterface::class));
}

$cacheParam = $cache instanceof ContainerInterface ? $translator : $cache;
$this->cache = $cacheParam;
if ($this->cache instanceof CacheProvider) {
@trigger_error(sprintf('Passing an instance of "%s" as the second argument in "%s" is deprecated since KunstmaanAdminBundle 5.7 and an instance of "%s" will be required in KunstmaanAdminBundle 6.0.', CacheProvider::class, __METHOD__, AdapterInterface::class), E_USER_DEPRECATED);

$this->cache = new DoctrineAdapter($cache);
$this->cache = new DoctrineAdapter($cacheParam);
}

// NEXT_MAJOR Add "Symfony\Contracts\Translation\TranslatorInterface" typehint when sf <4.4 support is removed.
if (!$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) {
if (!$cache instanceof ContainerInterface && (!$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface)) {
throw new \InvalidArgumentException(sprintf('The "$translator" parameter should be instance of "%s" or "%s"', TranslatorInterface::class, LegacyTranslatorInterface::class));
}

$this->translator = $translator;
if ($cache instanceof ContainerInterface && (!$requestStack instanceof TranslatorInterface && !$requestStack instanceof LegacyTranslatorInterface)) {
throw new \InvalidArgumentException(sprintf('The "$translator" parameter should be instance of "%s" or "%s"', TranslatorInterface::class, LegacyTranslatorInterface::class));
}

$translatorParam = $cache instanceof ContainerInterface ? $requestStack : $translator;
$this->translator = $translatorParam;

if (!$cache instanceof ContainerInterface) {
$this->requestStack = $requestStack;
$this->webserviceUrl = $webserviceUrl;
$this->cacheTimeframe = $cacheTimeframe;
$this->enabled = $enabled;
$this->projectDir = $projectDir;
$this->websiteTitle = $websiteTitle;

$this->webserviceUrl = $this->container->getParameter('version_checker.url');
$this->cacheTimeframe = $this->container->getParameter('version_checker.timeframe');
$this->enabled = $this->container->getParameter('version_checker.enabled');
return;
}

$container = $cache;
$this->requestStack = $container->get('request_stack');
$this->webserviceUrl = $container->getParameter('version_checker.url');
$this->cacheTimeframe = $container->getParameter('version_checker.timeframe');
$this->enabled = $container->getParameter('version_checker.enabled');
$this->projectDir = $container->getParameter('kernel.project_dir');
$this->websiteTitle = $container->getParameter('kunstmaan_admin.website_title');
}

/**
Expand Down Expand Up @@ -123,17 +170,16 @@ public function check()
return;
}

$host = $this->container->get('request_stack')->getCurrentRequest()->getHttpHost();
$console = realpath($this->container->get('kernel')->getProjectDir() . '/bin/console');
$host = $this->requestStack->getCurrentRequest()->getHttpHost();
$console = realpath($this->projectDir . '/bin/console');
$installed = filectime($console);
$bundles = $this->parseComposer();
$title = $this->container->getParameter('kunstmaan_admin.website_title');

$jsonData = json_encode([
'host' => $host,
'installed' => $installed,
'bundles' => $bundles,
'project' => $this->translator->trans($title),
'project' => $this->translator->trans($this->websiteTitle),
]);

try {
Expand Down Expand Up @@ -187,10 +233,7 @@ public function setClient($client)
*/
protected function getLockPath()
{
$kernel = $this->container->get('kernel');
$rootPath = $kernel->getProjectDir();

return $rootPath . '/composer.lock';
return $this->projectDir . '/composer.lock';
}

/**
Expand All @@ -202,17 +245,15 @@ protected function getLockPath()
*/
protected function getPackages()
{
$translator = $this->container->get('translator');
$errorMessage = $translator->trans('settings.version.error_parsing_composer');

$composerPath = $this->getLockPath();
if (!file_exists($composerPath)) {
throw new ParseException($translator->trans('settings.version.composer_lock_not_found'));
throw new ParseException($this->translator->trans('settings.version.composer_lock_not_found'));
}

$json = file_get_contents($composerPath);
$result = json_decode($json, true);

$errorMessage = $this->translator->trans('settings.version.error_parsing_composer');
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ParseException($errorMessage . ' (#' . json_last_error() . ')');
}
Expand Down
10 changes: 9 additions & 1 deletion src/Kunstmaan/AdminBundle/Resources/config/services.yml
Expand Up @@ -171,7 +171,15 @@ services:

kunstmaan_admin.versionchecker:
class: Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker
arguments: ['@service_container', '@cache.kunstmaan_versioncheck', '@translator']
arguments:
- '@cache.kunstmaan_versioncheck'
- '@translator'
- '@request_stack'
- '%version_checker.url%'
- '%version_checker.timeframe%'
- '%version_checker.enabled%'
- '%kernel.project_dir%'
- '%kunstmaan_admin.website_title%'
public: true

kunstmaan_admin.cache:
Expand Down

0 comments on commit cd9d86f

Please sign in to comment.