Explorar o código

create and implements the OpentalentEnvService

Olivier Massot %!s(int64=4) %!d(string=hai) anos
pai
achega
13e3609f22

+ 7 - 11
ot_admin/Classes/Controller/SiteController.php

@@ -13,6 +13,7 @@ use Opentalent\OtCore\Controller\ActionController;
 use Opentalent\OtCore\Domain\Model\Organization;
 use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
 use Opentalent\OtCore\Exception\ApiRequestException;
+use Opentalent\OtCore\Service\OpentalentEnvService;
 use Opentalent\OtCore\Website\OtWebsiteRepository;
 use Opentalent\OtCore\Utility\FileUtility;
 use Opentalent\OtCore\Routing\Indexer;
@@ -154,7 +155,6 @@ class SiteController extends ActionController
 
     public function __construct()
     {
-        parent::__construct();
         $this->createdPagesIndex = [];
         $this->createdDirs = [];
         $this->createdFiles = [];
@@ -532,9 +532,9 @@ class SiteController extends ActionController
                 ->execute();
 
             // Create the user_upload and form_definitions directories and update the sys_filemounts table
-            $uploadRelPath = "/user_upload/" . $organizationId;
+            $uploadRelPath = "user_upload/" . $organizationId;
             $fileadminDir = $_ENV['TYPO3_PATH_APP'] . "/public/fileadmin";
-            $uploadDir = $fileadminDir . "/" . $uploadRelPath;
+            $uploadDir = rtrim($fileadminDir, '/') . "/" . ltrim($uploadRelPath, '/');
             if (file_exists($uploadDir)) {
                 throw new \RuntimeException("A directory or file " . $uploadDir . " already exists. Abort.");
             }
@@ -670,7 +670,7 @@ class SiteController extends ActionController
                 ->set('organization_name', $organization->getName())
                 ->where($queryBuilder->expr()->eq('uid', $website['uid']))
                 ->execute();
-            
+
             // ## Update the subpages of the rootpage
             $sitePages = $this->otPageRepository->getAllSubpagesForPage($rootUid);
             foreach ($sitePages as $page) {
@@ -1993,14 +1993,10 @@ class SiteController extends ActionController
     }
 
     private function fetchOrganizationExtraData(int $organizationId) {
-
-        $db_host = ($_SERVER['HTTP_HOST'] == 'typo3' |
-                    $_SERVER['HTTP_HOST'] == 'local.sub.opentalent.fr') ? 'db' : 'prod-back';
-
         $cnn = new PDO(
-            "mysql:host=" . $db_host . ";dbname=opentalent",
-            'dbcloner',
-            'wWZ4hYcrmHLW2mUK',
+            "mysql:host=" . OpentalentEnvService::get('DB_HOST') . ";dbname=opentalent",
+            OpentalentEnvService::get('DB_USER'),
+            OpentalentEnvService::get('DB_PASSWORD'),
             array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')
         );
         $cnn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

+ 4 - 11
ot_core/Classes/Http/ApiController.php

@@ -3,6 +3,7 @@
 namespace Opentalent\OtCore\Http;
 
 use Opentalent\OtCore\Exception\ApiRequestException;
+use Opentalent\OtCore\Service\OpentalentEnvService;
 use PDO;
 use TYPO3\CMS\Core\Http\JsonResponse;
 use TYPO3\CMS\Core\Http\ServerRequest;
@@ -21,18 +22,10 @@ class ApiController
      * @return PDO
      */
     private function getCnn(ServerRequest $request) {
-        if ($_SERVER['HTTP_HOST'] == 'typo3' | $_SERVER['HTTP_HOST'] == 'local.sub.opentalent.fr') {
-            $db_host = 'db';
-        } elseif ($_SERVER['HTTP_HOST'] == 'preprod.opentalent.fr') {
-            $db_host = 'localhost';
-        } else {
-            $db_host = 'prod-back';
-        }
-
         $cnn = new PDO(
-            "mysql:host=" . $db_host . ";dbname=opentalent",
-            'dbcloner',
-            'wWZ4hYcrmHLW2mUK',
+            "mysql:host=" . OpentalentEnvService::get('DB_HOST') . ";dbname=opentalent",
+            OpentalentEnvService::get('DB_USER'),
+            OpentalentEnvService::get('DB_PASSWORD'),
             array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')
         );
         $cnn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

+ 1 - 15
ot_core/Classes/Service/OpentalentApiService.php

@@ -15,13 +15,6 @@ class OpentalentApiService implements LoggerAwareInterface
 {
     use LoggerAwareTrait;
 
-    const DEFAULT_BASE_URI = 'https://api.opentalent.fr';
-    protected array $variants_uris = [
-          "preprod.opentalent.fr" => "https://api.preprod.opentalent.fr",
-          "local.sub.opentalent.fr" => "http://docker.nginx.opentalent.fr",
-          "typo3" => "http://docker.nginx.opentalent.fr"
-    ];
-
     protected object $client;
     protected object $context;
 
@@ -72,14 +65,7 @@ class OpentalentApiService implements LoggerAwareInterface
      */
     public function getApiUri(string $trailing_part = ""): string
     {
-        $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['VIRTUAL_HOST'];
-
-        if (isset($this->variants_uris[$host])) {
-            $uri = $this->variants_uris[$host];
-        } else {
-            $uri = self::DEFAULT_BASE_URI;
-        }
-
+        $uri = OpentalentEnvService::get('API_BASE_URI');
         return rtrim($uri, '/') . '/' . ltrim($trailing_part, '/');
     }
 

+ 26 - 0
ot_core/Classes/Service/OpentalentEnvService.php

@@ -2,7 +2,33 @@
 
 namespace Opentalent\OtCore\Service;
 
+use http\Exception\RuntimeException;
+
+/**
+ * Return the current opentalent environment variables
+ *
+ * To override the default values for a specific environment, set the new values as
+ * global variables as $GLOBALS['OT']['YOUR_VAR']
+ *
+ * A goot place to do that is in the AdditionalConfiguration.php file of your typo3 installation,
+ * by adding for example:
+ *
+ *     $GLOBALS['OT']['DB_HOST'] = 'db';
+ *
+ */
 class OpentalentEnvService
 {
+    const DEFAULT = [
+        'API_BASE_URI' => 'https://api.opentalent.fr',
+        'DB_HOST' => 'prod-back',
+        'DB_USER' => 'dbcloner',
+        'DB_PASSWORD' => 'wWZ4hYcrmHLW2mUK',
+    ];
 
+    public static function get($varname) {
+        if (!array_key_exists($varname, self::DEFAULT)) {
+            throw new \RuntimeException('Unexisting environment variable requested: ' . $varname);
+        }
+        return $GLOBALS['OT'][$varname] ?? self::DEFAULT[$varname];
+    }
 }