diff --git a/README.md b/README.md index e8d47b71..be3c4480 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,12 @@ In the component's `composer.json`, specify: * `type`, type of Magento 2 component. * `extra/map`, list of files to move and their paths relative to the Magento root directory. +* `extra/chmod`, list of permissions that should be set for files. + + **Note**: + * `extra/map` is required only if your component needs to be moved to a location other than `/vendor`. Otherwise, omit this section. + * `extra/chmod` is required only if you need to set specific permissions for files. - **Note**: `extra/map` is required only if your component needs to be moved to a location other than `/vendor`. Otherwise, omit this section. ## Supported Components The following list explains the use of `type` in `composer.json`. @@ -165,5 +169,38 @@ The Magneto Composer Installer uses the `copy` deployment strategy. It copies ea There are [other deployment strategies](https://github.com/magento/magento-composer-installer/blob/master/doc/Deploy.md) that could be used; however, we don't guarantee that any of them will work. +## Usage `extra/chmod` + +The following example shows how you can set specific permissions for files. + +Example: + +```json +{ + "name": "magento/module-sample", + "description": "N/A", + "require": { + ... + }, + "type": "magento2-module", + "extra": { + "chmod": [ + { + "mask": "0755", + "path": "bin/magento" + }, + { + "mask": "0644", + "path": "some_dir/file.jpg" + } + ] + } +} +``` + +`mask` is a bit mask for chmod command + +`path` is a path to file relative to the Magento root folder + # Notes - The extra->magento-root-dir option is no longer supported. It displays only to preseve backward compatibility. \ No newline at end of file diff --git a/src/MagentoHackathon/Composer/Magento/Plugin.php b/src/MagentoHackathon/Composer/Magento/Plugin.php index ec845f72..234bdb50 100644 --- a/src/MagentoHackathon/Composer/Magento/Plugin.php +++ b/src/MagentoHackathon/Composer/Magento/Plugin.php @@ -157,8 +157,51 @@ public function onNewCodeEvent(\Composer\Script\Event $event) $this->deployLibraries(); $this->saveVendorDirPath($event->getComposer()); $this->requestRegeneration(); + $this->setFilePermissions(); } + /** + * Set permissions for files using extra->chmod from composer.json + * + * @return void + */ + private function setFilePermissions() + { + $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(); + $message = 'Check "chmod" section in composer.json of %s package.'; + + foreach ($packages as $package) { + $extra = $package->getExtra(); + if (!isset($extra['chmod']) || !is_array($extra['chmod'])) { + continue; + } + + $error = false; + foreach ($extra['chmod'] as $chmod) { + if (!isset($chmod['mask']) || !isset($chmod['path']) || strpos($chmod['path'], '..') !== false) { + $error = true; + continue; + } + + $file = $this->installer->getTargetDir() . '/' . $chmod['path']; + if (file_exists($file)) { + chmod($file, octdec($chmod['mask'])); + } else { + $this->io->writeError([ + 'File doesn\'t exist: ' . $chmod['path'], + sprintf($message, $package->getName()) + ]); + } + } + + if ($error) { + $this->io->writeError([ + 'Incorrect mask or file path.', + sprintf($message, $package->getName()) + ]); + } + } + } protected function deployLibraries() {