config_packager-8.x-1.x-dev/src/Plugin/ConfigPackagerGeneration/ConfigPackagerGenerationWrite.php
src/Plugin/ConfigPackagerGeneration/ConfigPackagerGenerationWrite.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 | <?php /** * @file * Contains \Drupal\config_packager\Plugin\ConfigPackagerGeneration\ConfigPackagerGenerationWrite. */ namespace Drupal\config_packager\Plugin\ConfigPackagerGeneration; use Drupal\config_packager\ConfigPackagerGenerationMethodBase; use Drupal\Core\Config\InstallStorage; /** * Class for writing packages to the local file system. * * @Plugin( * id = \Drupal\config_packager\Plugin\ConfigPackagerGeneration\ConfigPackagerGenerationWrite::METHOD_ID, * weight = 2, * name = @Translation("Write"), * description = @Translation("Write packages and optional profile to the file system."), * ) */ class ConfigPackagerGenerationWrite extends ConfigPackagerGenerationMethodBase { /** * The package generation method id. */ const METHOD_ID = 'write' ; /** * Reads and merges in existing files for a given package or profile. */ protected function preparePackage( $add_profile , & $package , $existing_packages ) { // If it's a profile, write it to the 'profiles' directory. Otherwise, it // goes in 'modules/custom'. $base_directory = $add_profile ? 'profiles' : 'modules/custom' ; // If this package is already present, prepare files. if (isset( $existing_packages [ $package [ 'machine_name' ]])) { $existing_directory = $existing_packages [ $package [ 'machine_name' ]]; $package [ 'directory' ] = $existing_directory ; // Merge in the info file. $info_file_uri = $existing_directory . '/' . $package [ 'machine_name' ] . '.info.yml' ; if ( file_exists ( $info_file_uri )) { $package [ 'files' ][ 'info' ][ 'string' ] = $this ->mergeInfoFile( $package [ 'files' ][ 'info' ][ 'string' ], $info_file_uri ); } // Remove the config directory, as it will be replaced. $config_directory = $existing_directory . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY; if ( is_dir ( $config_directory )) { file_unmanaged_delete_recursive( $config_directory ); } } // If the package is not present, nest its files in the base directory. else { // Prepend the extension's directory with the base directory. $package [ 'directory' ] = $base_directory . '/' . $package [ 'directory' ]; } } /** * {@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 = []; // Add profile files. if ( $add_profile ) { // If no profile was passed, load the profile. if ( empty ( $profile )) { $profile = $this ->configPackagerManager->getProfile(); } $this ->generatePackage( $return , $profile ); } // Add package files. foreach ( $packages as $package ) { $this ->generatePackage( $return , $package ); } return $return ; } /** * Writes a package or profile's files to the file system. * * @param array &$return * The return value, passed by reference. * @param array $package * The package or profile. */ protected function generatePackage( array & $return , array $package ) { $success = TRUE; foreach ( $package [ 'files' ] as $file ) { try { $this ->generateFile( $package [ 'directory' ], $file ); } catch (Exception $exception ) { $this ->failure( $return , $package , $exception ); $success = FALSE; break ; } } if ( $success ) { $this ->success( $return , $package ); } } /** * Registers a successful package or profile write operation. * * @param array &$return * The return value, passed by reference. * @param array $package * The package or profile. */ protected function success(& $return , $package ) { $type = $package [ 'type' ] == 'module' ? $this ->t( 'Package' ) : $this ->t( 'Profile' ); $return [] = [ 'success' => TRUE, 'display' => TRUE, 'message' => $this ->t( '!type @package written to @directory.' ), 'variables' => [ '!type' => $type , '@package' => $package [ 'name' ], '@directory' => $package [ 'directory' ] ], ]; } /** * Registers a failed package or profile write 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 , $package , Exception $exception ) { $type = $package [ 'type' ] == 'package' ? $this ->t( 'Package' ) : $this ->t( 'Profile' ); $return [] = [ 'success' => FALSE, 'display' => TRUE, 'message' => $this ->t( '!type @package not written to @directory. Error: @error.' ), 'variables' => [ '!type' => $type , '@package' => $package [ 'name' ], '@directory' => $package [ 'directory' ], '@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. * * @throws Exception */ protected function generateFile( $directory , $file ) { if (! empty ( $file [ 'subdirectory' ])) { $directory .= '/' . $file [ 'subdirectory' ]; } if (! is_dir ( $directory )) { if (drupal_mkdir( $directory , NULL, TRUE) === FALSE) { throw new \Exception( $this ->t( 'Failed to create directory @directory.' , [ '@directory' => $directory ])); } } if ( file_put_contents ( $directory . '/' . $file [ 'filename' ], $file [ 'string' ]) === FALSE) { throw new \Exception( $this ->t( 'Failed to write file @filename.' , [ '@filename' => $file [ 'filename' ]])); } } } |