| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Zikula package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright Zikula Foundation - https://ziku.la/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Zikula\Bundle\CoreBundle\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class LocalDotEnvHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $projectDir; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         string $projectDir | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $this->projectDir = $projectDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Add $newVars to the .env.local file. If true === $override, remove existing values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Prepend value with '!' to disable pseudo-urlencoding that value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function writeLocalEnvVars(array $newVars, bool $override = false): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $localEnvDir = $this->projectDir . '/.env.local'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $fileSystem = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $vars = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if (!$override && $fileSystem->exists($localEnvDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $content = explode("\n", file_get_contents($localEnvDir)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             foreach ($content as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 if (empty($line)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 [$key, $value] = explode('=', $line, 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $vars[$key] = $newVars[$key] ?? '!' . $value; // never encode existing values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $vars = $vars + array_diff_assoc($newVars, $vars); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $fileSystem->dumpFile($localEnvDir, $this->varsToString($vars)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function varsToString(array $vars): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $lines = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($vars as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $value = '!' === $value[0] ? mb_substr($value, 1) : str_replace(['#', '@', '(', ')'], ['%23', '%40', '%28', '%29'], $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $lines[] = $key . '=' . $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return trim(implode("\n", $lines)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  |