浏览代码

refactor the config file creation

Olivier Massot 4 年之前
父节点
当前提交
d476085d7b
共有 1 个文件被更改,包括 40 次插入58 次删除
  1. 40 58
      ot_admin/Classes/Controller/SiteController.php

+ 40 - 58
ot_admin/Classes/Controller/SiteController.php

@@ -517,14 +517,19 @@ class SiteController extends ActionController
                 ])
                 ->execute();
 
-            // Create the site config.yaml file
-            $this->writeConfigFile(
-                $organizationId,
+            // ## Create the site config.yaml file
+            $identifier = $this->writeConfigFile(
                 $rootUid,
-                $organization->getSubDomain() . '.opentalent.fr',
                 true
             );
 
+            // Update the ot_website identifier
+            $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
+            $queryBuilder->update('ot_websites')
+                ->set('config_identifier', $identifier)
+                ->where($queryBuilder->expr()->eq('uid', $websiteUid))
+                ->execute();
+
             // Create the user_upload and form_definitions directories and update the sys_filemounts table
             $uploadRelPath = "/user_upload/" . $organizationId;
             $fileadminDir = $_ENV['TYPO3_PATH_APP'] . "/public/fileadmin";
@@ -631,13 +636,13 @@ class SiteController extends ActionController
      * - Clear the Typo3 cache for the website
      *
      * @param int $organizationId
-     * @param bool $hard Performs an hard update (recreate the site config file, reset the be_users permissions)
      * @return int
-     * @throws NoSuchCacheException
+     * @throws NoSuchOrganizationException
      * @throws NoSuchRecordException
      * @throws NoSuchWebsiteException
      * @throws \Doctrine\DBAL\ConnectionException
      * @throws \Doctrine\DBAL\DBALException
+     * @throws \Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException
      * @throws \Throwable
      */
     public function updateSiteAction(int $organizationId): int
@@ -657,7 +662,6 @@ class SiteController extends ActionController
 
         // keep tracks of the created folders and files to be able to remove them during a rollback
         try {
-
             // ## Update the ot_website table
             $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
             $queryBuilder->update('ot_websites')
@@ -676,10 +680,6 @@ class SiteController extends ActionController
                     ->execute();
             }
 
-            // ## Update the config.yaml file and the ot_websites.config_identifier field
-            $organizationDomain = $this->otWebsiteRepository->resolveWebsiteDomain($website);
-            $this->writeConfigFile($organizationId, $rootUid, $organizationDomain, true);
-
             // ## Update the `sys_template`.`constants` and the `pages`.`TSConfig` fields
             $constants = $this->getTemplateConstants($organizationId, $organizationExtraData);
 
@@ -697,6 +697,17 @@ class SiteController extends ActionController
                 ->where($queryBuilder->expr()->eq('uid', $rootUid))
                 ->execute();
 
+            // ## Update the config.yaml file
+            $identifier = $this->otWebsiteRepository->findConfigIdentifierFor($rootUid);
+            $this->writeConfigFile($rootUid, true, $identifier);
+
+            // ## Update the ot_website identifier
+            $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
+            $queryBuilder->update('ot_websites')
+                ->set('config_identifier', $identifier)
+                ->where($queryBuilder->expr()->eq('uid', $website['uid']))
+                ->execute();
+
             // Try to commit the result
             $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
             if (!$commitSuccess) {
@@ -711,6 +722,7 @@ class SiteController extends ActionController
 
         // ## Clear the Typo3 cache for the website
         OtCacheManager::clearSiteCache($rootUid, true);
+
         return $rootUid;
     }
 
@@ -2036,21 +2048,24 @@ class SiteController extends ActionController
      * Create or update the .../sites/.../config.yaml file of the given site
      * Return the identifier of the created website
      *
-     * @param int $organizationId
      * @param int $rootUid
-     * @param string $domain
      * @param bool $forceRecreate
+     * @param null $identifier
+     * @return string Identifier of the newly created configuration file
+     * @throws NoSuchWebsiteException
+     * @throws \Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException
      */
-    private function writeConfigFile(int $organizationId, int $rootUid, string $domain, bool $forceRecreate = false): void
+    private function writeConfigFile(int $rootUid, bool $forceRecreate = false, $identifier = null): string
     {
-        $existing = $this->otWebsiteRepository->findConfigFileAndContentFor($rootUid);
+        $website = $this->otWebsiteRepository->getWebsiteByPageUid($rootUid);
+        $domain = $this->otWebsiteRepository->resolveWebsiteDomain($website);
+
+        $existing = $this->otWebsiteRepository->findConfigFileAndContentFor($rootUid, $identifier);
         $configFilename = $existing[0];
         $config = $existing[1];
 
-        $subdomain = explode('.', $domain)[0];
-
         if (!$configFilename) {
-            $identifier = $subdomain . '_' . $organizationId;
+            $identifier = $website['subdomain'] . '_' . $website['organization_id'];
             $configDir = $_ENV['TYPO3_PATH_APP'] . "/config/sites/" . $identifier;
             $configFilename = $configDir . "/config.yaml";
             $isNew = true;
@@ -2060,43 +2075,16 @@ class SiteController extends ActionController
             }
         } else {
             $configDir = dirname($configFilename);
-            $identifier = basename($configDir);
+            if ($identifier == null) {
+                $identifier = basename($configDir);
+            }
             $config['base'] = 'https://' . $domain;
             $isNew = false;
         }
 
         if ($isNew || $forceRecreate) {
-            $config = ['base' => 'https://' . $domain,
-                'baseVariants' => [
-                    ['base' => $subdomain . '/',
-                        'condition' => 'applicationContext == "Development"']
-                ],
-                'errorHandling' => [
-                    ['errorCode' => '404',
-                        'errorHandler' => 'PHP',
-                        'errorPhpClassFQCN' => 'Opentalent\OtTemplating\Page\ErrorHandler'],
-                    ['errorCode' => '403',
-                        'errorHandler' => 'PHP',
-                        'errorPhpClassFQCN' => 'Opentalent\OtTemplating\Page\ErrorHandler'],
-                ],
-                'flux_content_types' => '',
-                'flux_page_templates' => '',
-                'languages' => [[
-                    'title' => 'Fr',
-                    'enabled' => True,
-                    'base' => '/',
-                    'typo3Language' => 'fr',
-                    'locale' => 'fr_FR',
-                    'iso-639-1' => 'fr',
-                    'navigationTitle' => 'Fr',
-                    'hreflang' => 'fr-FR',
-                    'direction' => 'ltr',
-                    'flag' => 'fr',
-                    'languageId' => '0',
-                ]],
-                'rootPageId' => $rootUid,
-                'routes' => []
-            ];
+            $siteConfig = $this->otWebsiteRepository->generateWebsiteConfiguration($website, $identifier);
+            $config = $siteConfig->getConfiguration();
         }
 
         $yamlConfig = Yaml::dump($config, 99, 2);
@@ -2106,14 +2094,6 @@ class SiteController extends ActionController
         }
         $this->writeFile($configFilename, $yamlConfig);
 
-        // Update the identifier in the ot_websites table
-        $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
-        $queryBuilder
-            ->update('ot_websites')
-            ->where($queryBuilder->expr()->eq('organization_id', $organizationId))
-            ->set('config_identifier', $identifier)
-            ->execute();
-
         // Set the owner and mods, in case www-data is not the one who run this command
         // @see https://www.php.net/manual/fr/function.stat.php
         try {
@@ -2131,6 +2111,8 @@ class SiteController extends ActionController
             $cacheSystem->remove('pseudo-sites');
         } catch (NoSuchCacheException $e) {
         }
+
+        return $identifier;
     }
 
     /**