From 4539ab2dcff470917e4c1faec27c0f60f325e5ad Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandro@alejandrocelaya.com>
Date: Sun, 22 Mar 2020 17:42:56 +0100
Subject: [PATCH] Moved hardcoded class alias to a namespaced constant

---
 config/autoload/locks.global.php          | 6 +++---
 config/container.php                      | 7 +++++--
 module/CLI/config/dependencies.config.php | 4 +++-
 module/Core/functions/functions.php       | 1 +
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/config/autoload/locks.global.php b/config/autoload/locks.global.php
index 22a51e38..25c00f22 100644
--- a/config/autoload/locks.global.php
+++ b/config/autoload/locks.global.php
@@ -8,7 +8,7 @@ use Shlinkio\Shlink\Common\Lock\RetryLockStoreDelegatorFactory;
 use Shlinkio\Shlink\Common\Logger\LoggerAwareDelegatorFactory;
 use Symfony\Component\Lock;
 
-$localLockFactory = 'Shlinkio\Shlink\LocalLockFactory';
+use const Shlinkio\Shlink\Core\LOCAL_LOCK_FACTORY;
 
 return [
 
@@ -21,7 +21,7 @@ return [
             Lock\Store\FlockStore::class => ConfigAbstractFactory::class,
             Lock\Store\RedisStore::class => ConfigAbstractFactory::class,
             Lock\LockFactory::class => ConfigAbstractFactory::class,
-            $localLockFactory => ConfigAbstractFactory::class,
+            LOCAL_LOCK_FACTORY => ConfigAbstractFactory::class,
         ],
         'aliases' => [
             // With this config, a user could alias 'lock_store' => 'redis_lock_store' to override the default
@@ -44,7 +44,7 @@ return [
         Lock\Store\FlockStore::class => ['config.locks.locks_dir'],
         Lock\Store\RedisStore::class => [RedisFactory::SERVICE_NAME],
         Lock\LockFactory::class => ['lock_store'],
-        $localLockFactory => ['local_lock_store'],
+        LOCAL_LOCK_FACTORY => ['local_lock_store'],
     ],
 
 ];
diff --git a/config/container.php b/config/container.php
index 3735e14e..7b6f0b08 100644
--- a/config/container.php
+++ b/config/container.php
@@ -5,13 +5,16 @@ declare(strict_types=1);
 use Laminas\ServiceManager\ServiceManager;
 use Symfony\Component\Lock;
 
+use const Shlinkio\Shlink\Core\LOCAL_LOCK_FACTORY;
+
 chdir(dirname(__DIR__));
 
 require 'vendor/autoload.php';
 
 // This class alias tricks the ConfigAbstractFactory to return Lock\Factory instances even with a different service name
-if (! class_exists('Shlinkio\Shlink\LocalLockFactory')) {
-    class_alias(Lock\LockFactory::class, 'Shlinkio\Shlink\LocalLockFactory');
+// It needs to be placed here as individual config files will not be loaded once config is cached
+if (! class_exists(LOCAL_LOCK_FACTORY)) {
+    class_alias(Lock\LockFactory::class, LOCAL_LOCK_FACTORY);
 }
 
 // Build container
diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php
index 1cc67fd9..5edb6499 100644
--- a/module/CLI/config/dependencies.config.php
+++ b/module/CLI/config/dependencies.config.php
@@ -19,6 +19,8 @@ use Symfony\Component\Console as SymfonyCli;
 use Symfony\Component\Lock\LockFactory;
 use Symfony\Component\Process\PhpExecutableFinder;
 
+use const Shlinkio\Shlink\Core\LOCAL_LOCK_FACTORY;
+
 return [
 
     'dependencies' => [
@@ -52,7 +54,7 @@ return [
     ],
 
     ConfigAbstractFactory::class => [
-        GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, 'Shlinkio\Shlink\LocalLockFactory'],
+        GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, LOCAL_LOCK_FACTORY],
 
         Command\ShortUrl\GenerateShortUrlCommand::class => [
             Service\UrlShortener::class,
diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php
index 87399208..3016b18c 100644
--- a/module/Core/functions/functions.php
+++ b/module/Core/functions/functions.php
@@ -12,6 +12,7 @@ use function sprintf;
 
 const DEFAULT_SHORT_CODES_LENGTH = 5;
 const MIN_SHORT_CODES_LENGTH = 4;
+const LOCAL_LOCK_FACTORY = 'Shlinkio\Shlink\LocalLockFactory';
 
 function generateRandomShortCode(int $length): string
 {