config_packager-8.x-1.x-dev/src/Plugin/ConfigPackagerGeneration/ConfigPackagerGenerationArchive.php
src/Plugin/ConfigPackagerGeneration/ConfigPackagerGenerationArchive.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | <?php /** * @file * Contains \Drupal\config_packager\Plugin\ConfigPackagerGeneration\ConfigPackagerGenerationArchive. */ namespace Drupal\config_packager\Plugin\ConfigPackagerGeneration; use Drupal\Component\Serialization\Yaml; use Drupal\config_packager\ConfigPackagerGenerationMethodBase; use Drupal\Core\Archiver\ArchiveTar; use Drupal\Core\Config\InstallStorage; use Drupal\Core\Form\FormStateInterface; /** * Class for generating a compressed archive of packages. * * @Plugin( * id = \Drupal\config_packager\Plugin\ConfigPackagerGeneration\ConfigPackagerGenerationArchive::METHOD_ID, * weight = -2, * name = @Translation("Archive"), * description = @Translation("Generate packages and optional profile as a compressed archive for download."), * ) */ class ConfigPackagerGenerationArchive extends ConfigPackagerGenerationMethodBase { /** * The package generation method id. */ const METHOD_ID = 'archive' ; /** * Reads and merges in existing files for a given package or profile. */ protected function preparePackage( $add_profile , & $package , $existing_packages ) { if (isset( $existing_packages [ $package [ 'machine_name' ]])) { $existing_directory = $existing_packages [ $package [ 'machine_name' ]]; // Scan for all files. $files = file_scan_directory( $existing_directory , '/.*/' ); foreach ( $files as $file ) { // Skip files in the install directory. if ( strpos ( $existing_directory , InstallStorage::CONFIG_INSTALL_DIRECTORY) !== FALSE) { continue ; } // Merge in the info file. if ( $file ->name == $package [ 'machine_name' ] . '.info' ) { $package [ 'files' ][ 'info' ][ 'string' ] = $this ->mergeInfoFile( $package [ 'files' ][ 'info' ][ 'string' ], $file ->uri); } // Read in remaining files. else { // Determine if the file is within a subdirectory of the // extension's directory. $file_directory = dirname( $file ->uri); if ( $file_directory !== $existing_directory ) { $subdirectory = substr ( $file_directory , strlen ( $existing_directory ) + 1); } else { $subdirectory = NULL; } $package [ 'files' ][] = [ 'filename' => $file ->filename, 'subdirectory' => $subdirectory , 'string' => file_get_contents ( $file ->uri) ]; } } } } /** * {@inheritdoc} */ public function generate( $add_profile = FALSE, array $profile = array (), array $packages = array ()) { // If no packages were specified, get all packages. if ( empty ( $packages )) { $packages = $this ->configPackagerManager->getPackages(); } $return = []; // Remove any previous version of the exported archive. $machine_name = $this ->configFactory->get( 'config_packager.settings' )->get( 'profile.machine_name' ); $archive_name = file_directory_temp() . '/' . $machine_name . '.tar.gz' ; if ( file_exists ( $archive_name )) { file_unmanaged_delete( $archive_name ); } $archiver = new ArchiveTar( $archive_name ); if ( $add_profile ) { // If no profile was passed, load the profile. if ( empty ( $profile )) { $profile = $this ->configPackagerManager->getProfile(); } $this ->generatePackage( $return , $profile , $archiver ); } // Add package files. foreach ( $packages as $package ) { $this ->generatePackage( $return , $package , $archiver ); } return $return ; } /** * Writes a package or profile's files to an archive. * * @param array &$return * The return value, passed by reference. * @param array $package * The package or profile. * @param ArchiveTar $archiver * The archiver. */ protected function generatePackage( array & $return , array $package , ArchiveTar $archiver ) { $success = TRUE; foreach ( $package [ 'files' ] as $file ) { try { $this ->generateFile( $package [ 'directory' ], $file , $archiver ); } catch (\Exception $exception ) { $this ->failure( $return , $package , $exception ); $success = FALSE; break ; } } if ( $success ) { $this ->success( $return , $package ); } } /** * Registers a successful package or profile archive operation. * * @param array &$return * The return value, passed by reference. * @param array $package * The package or profile. */ protected function success( array & $return , array $package ) { $type = $package [ 'type' ] == 'module' ? $this ->t( 'Package' ) : $this ->t( 'Profile' ); $return [] = [ 'success' => TRUE, // Archive writing doesn't merit a message, and if done through the UI // would appear on the subsequent page load. 'display' => FALSE, 'message' => $this ->t( '!type @package written to archive.' ), 'variables' => [ '!type' => $type , '@package' => $package [ 'name' ] ], ]; } /** * Registers a failed package or profile archive operation. * * @param array &$return * The return value, passed by reference. * @param array $package * The package or profile. * @param Exception $exception * The exception object. */ protected function failure(& $return , array $package , Exception $exception ) { $type = $package [ 'type' ] == 'package' ? $this ->t( 'Package' ) : $this ->t( 'Profile' ); $return [] = [ 'success' => FALSE, // Archive writing doesn't merit a message, and if done through the UI // would appear on the subsequent page load. 'display' => FALSE, 'message' => $this ->t( '!type @package not written to archive. Error: @error.' ), 'variables' => [ '!type' => $type , '@package' => $package [ 'name' ], '@error' => $exception ->getMessage() ], ]; } /** * Writes a file to the file system, creating its directory as needed. * * @param directory * The extension's directory. * @param array $file * Array with the following keys: * - 'filename': the name of the file. * - 'subdirectory': any subdirectory of the file within the extension * directory. * - 'string': the contents of the file. * @param ArchiveTar $archiver * The archiver. * * @throws Exception */ protected function generateFile( $directory , array $file , ArchiveTar $archiver ) { $filename = $directory ; if (! empty ( $file [ 'subdirectory' ])) { $filename .= '/' . $file [ 'subdirectory' ]; } $filename .= '/' . $file [ 'filename' ]; if ( $archiver ->addString( $filename , $file [ 'string' ]) === FALSE) { throw new \Exception( $this ->t( 'Failed to archive file @filename.' , [ '@filename' => $file [ 'filename' ]])); } } /** * {@inheritdoc} */ public function exportFormSubmit( array & $form , FormStateInterface $form_state ) { // Redirect to the archive file download. $form_state ->setRedirect( 'config_packager.export_download' ); } } |