1717 */
1818class TenantBootstrapper
1919{
20+ private static $ landlordValues = [];
21+
2022 private $ encrypter = null ;
2123
2224 private $ pdo = null ;
2325
24- private $ originalValues = null ;
26+ private $ app = null ;
27+
28+ public static $ landlordKeysToSave = [
29+ 'APP_URL ' ,
30+ 'APP_KEY ' ,
31+ 'LOG_PATH ' ,
32+ 'DB_USERNAME ' ,
33+ 'DB_PASSWORD ' ,
34+ 'REDIS_PREFIX ' ,
35+ 'CACHE_SETTING_PREFIX ' ,
36+ 'SCRIPT_MICROSERVICE_CALLBACK ' ,
37+ ];
2538
2639 public function bootstrap (Application $ app )
2740 {
2841 if (!$ this ->env ('MULTITENANCY ' )) {
2942 return ;
3043 }
44+ $ this ->app = $ app ;
3145
32- // We need to save the original values for running horizon
33- $ this ->saveOriginalValues ();
46+ self ::saveLandlordValues ($ app );
3447
3548 $ tenantData = null ;
3649
@@ -49,29 +62,30 @@ public function bootstrap(Application $app)
4962
5063 return ;
5164 }
52- $ this ->setTenantEnvironmentVariables ($ app , $ tenantData );
65+ $ this ->setTenantEnvironmentVariables ($ tenantData );
5366
5467 // Use tenant's translation files. Doing this here so it's available in cached filesystems.php
5568 $ app ->useLangPath (resource_path ('lang/tenant_ ' . $ tenantData ['id ' ]));
5669
57- $ tenantData ['original_values ' ] = $ this -> getOriginalValue () ;
70+ $ tenantData ['original_values ' ] = self :: $ landlordValues ;
5871 Tenant::setBootstrappedTenant ($ app , $ tenantData );
5972 }
6073
61- private function setTenantEnvironmentVariables ($ app , $ tenantData )
74+ private function setTenantEnvironmentVariables ($ tenantData )
6275 {
6376 // Additional configs are set in SwitchTenant.php
6477
6578 $ tenantId = $ tenantData ['id ' ];
6679 $ config = json_decode ($ tenantData ['config ' ], true );
6780
68- $ this ->set ('APP_CONFIG_CACHE ' , $ app ->basePath ('storage/tenant_ ' . $ tenantId . '/config.php ' ));
81+ $ this ->set ('APP_CONFIG_CACHE ' , $ this -> app ->basePath ('storage/tenant_ ' . $ tenantId . '/config.php ' ));
6982 // Do not override packages cache path for now. Wait until the License service is updated.
70- // $this->set('APP_PACKAGES_CACHE', $app->basePath('storage/tenant_' . $tenantId . '/packages.php'));
71- $ this ->set ('LARAVEL_STORAGE_PATH ' , $ app ->basePath ('storage/tenant_ ' . $ tenantId ));
83+ // $this->set('APP_PACKAGES_CACHE', $this-> app->basePath('storage/tenant_' . $tenantId . '/packages.php'));
84+ $ this ->set ('LARAVEL_STORAGE_PATH ' , $ this -> app ->basePath ('storage/tenant_ ' . $ tenantId ));
7285 $ this ->set ('APP_URL ' , $ config ['app.url ' ]);
7386 $ this ->set ('APP_KEY ' , $ this ->decrypt ($ config ['app.key ' ]));
74- $ this ->set ('DB_DATABASE ' , $ tenantData ['database ' ]);
87+ $ value = $ tenantData ['database ' ];
88+ $ this ->set ('DB_DATABASE ' , $ value );
7589 $ this ->set ('DB_USERNAME ' , $ tenantData ['username ' ] ?? $ this ->getOriginalValue ('DB_USERNAME ' ));
7690
7791 $ encryptedPassword = $ tenantData ['password ' ];
@@ -83,51 +97,45 @@ private function setTenantEnvironmentVariables($app, $tenantData)
8397 }
8498
8599 $ this ->set ('DB_PASSWORD ' , $ password );
86- $ this ->set ('REDIS_PREFIX ' , $ this ->getOriginalValue ('REDIS_PREFIX ' ) . 'tenant- ' . $ tenantId . ': ' );
87- $ this ->set ('LOG_PATH ' , $ app ->basePath ('storage/tenant_ ' . $ tenantId . '/logs/processmaker.log ' ));
100+ // Commenting this out fixes the redis queue, but what about cache????
101+ // $this->set('REDIS_PREFIX', $this->getOriginalValue('REDIS_PREFIX') . 'tenant-' . $tenantId . ':');
102+ // $this->debug("Setting log path to " . $this->app->basePath('storage/tenant_' . $tenantId . '/logs/processmaker.log'));
103+ $ this ->set ('LOG_PATH ' , $ this ->app ->basePath ('storage/tenant_ ' . $ tenantId . '/logs/processmaker.log ' ));
88104 }
89105
90- private function saveOriginalValues ( )
106+ public static function saveLandlordValues ( $ app )
91107 {
92- if ($ this ->env ('ORIGINAL_VALUES ' )) {
108+ if ($ app ->has ('landlordValues ' )) {
109+ self ::$ landlordValues = $ app ->make ('landlordValues ' );
110+
93111 return ;
94112 }
95- $ toSave = [
96- 'APP_URL ' ,
97- 'APP_KEY ' ,
98- 'DB_USERNAME ' ,
99- 'DB_PASSWORD ' ,
100- 'REDIS_PREFIX ' ,
101- 'CACHE_SETTING_PREFIX ' ,
102- 'SCRIPT_MICROSERVICE_CALLBACK ' ,
103- ];
104- $ values = [];
105- foreach ($ toSave as $ key ) {
106- $ values [$ key ] = $ this ->env ($ key );
113+
114+ foreach (self ::$ landlordKeysToSave as $ key ) {
115+ self ::$ landlordValues [$ key ] = $ _SERVER [$ key ] ?? '' ;
107116 }
108- $ this ->set ('ORIGINAL_VALUES ' , serialize ($ values ));
109117 }
110118
111- private function getOriginalValue ($ key = null )
119+ private function getOriginalValue ($ key )
112120 {
113- if (!$ this ->originalValues ) {
114- $ this ->originalValues = unserialize ($ this ->env ('ORIGINAL_VALUES ' ));
115- }
116- if (!$ key ) {
117- return $ this ->originalValues ;
121+ if (!isset (self ::$ landlordValues [$ key ])) {
122+ // throw new \Exception('Landlord value not found in `landlordValues`: ' . $key);
123+ return '' ;
118124 }
119125
120- return $ this -> originalValues [$ key ];
126+ return self :: $ landlordValues [$ key ];
121127 }
122128
123129 private function env ($ key , $ default = null )
124130 {
125- return Env:: get ( $ key, $ default) ;
131+ return $ _SERVER [ $ key] ?? $ default ;
126132 }
127133
128134 private function set ($ key , $ value )
129135 {
130- Env::getRepository ()->set ($ key , $ value );
136+ // Env::getRepository() is immutable but will use values from $_SERVER and $_ENV
137+ $ _SERVER [$ key ] = $ value ;
138+ $ _ENV [$ key ] = $ value ;
131139 }
132140
133141 private function decrypt ($ value )
@@ -174,4 +182,9 @@ private function executeQuery($query, $params = [])
174182
175183 return $ stmt ->fetch ();
176184 }
185+
186+ private function debug ($ message )
187+ {
188+ file_put_contents (base_path ('storage/debug.log ' ), $ message . PHP_EOL , FILE_APPEND );
189+ }
177190}
0 commit comments