| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Application\Tool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Borut Balažek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public static function prepare() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         self::prepareFolders(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'var', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'var/cache', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             'var/cache/assetic', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'var/cache/file', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'var/cache/http', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'var/cache/profiler', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'var/cache/proxy', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'var/cache/template', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'var/cache/security', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             'var/database', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             'var/logs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'var/sessions', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'var/mailer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             'var/mailer/spool', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         self::prepareLogFiles(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'var/logs/development.log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'var/logs/testing.log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'var/logs/staging.log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'var/logs/production.log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public static function prepareFolders(array $paths = array(), $uploadsPath = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (empty($paths)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $fs = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         foreach ($paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $fs->remove($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $fs->mkdir($paths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $fs->chmod($path, 0777); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         self::prepareUploadsFolder($uploadsPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public static function prepareUploadsFolder($uploadsDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (!$uploadsDirectory) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $fs = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $uploadsDirectory = 'web/assets/uploads'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (!$fs->exists($uploadsDirectory)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $fs->mkdir($uploadsDirectory, 0777); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $user = PHP_OS == 'Darwin' // Fix for OSX | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ? get_current_user() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             : 'www-data' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $fs->chown($uploadsDirectory, $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $fs->chmod($uploadsDirectory, 0777); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             // Not sure If we need to show this errors. Let's think about that... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public static function prepareSharedFolders(array $paths = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (empty($paths)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $fs = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $releaseRoot = dirname(dirname(dirname(dirname(__FILE__)))).'/'; // Current version root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $root = dirname(dirname($releaseRoot)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $sharedDirectory = $root.'/shared/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         // Create the shared directory first (if it does not exists) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if (!$fs->exists($sharedDirectory)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $fs->mkdir($sharedDirectory, 0777); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         foreach ($paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $pathDirectory = $releaseRoot.$path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $sharedPathDirectory = $sharedDirectory.$path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             if (!$fs->exists($sharedPathDirectory)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $fs->mkdir($sharedPathDirectory, 0777); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $pathDirectoryTmp = $pathDirectory.'_tmp'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             // Symlink it per hand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             exec("ln -f -s $sharedPathDirectory $pathDirectoryTmp"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             exec("rm -rf $pathDirectory"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             exec("mv -Tf $pathDirectoryTmp $pathDirectory"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |     public static function prepareLogFiles(array $paths) | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |         $fs = new Filesystem(); | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |         foreach ($paths as $path) { | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |             $fs->remove($path); | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |             $fs->touch($paths); | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |             $fs->chmod($path, 0777); | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |