diff --git a/composer.json b/composer.json index e808466..4218ad0 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ }, "extra": { "class": "Seacms\\ComposerInstaller\\Plugin", - "plugin-modifies-downloads": true, - "plugin-modifies-install-path": true + "plugin-modifies-downloads": false, + "plugin-modifies-install-path": false } } diff --git a/src/NotFoundFileException.php b/src/NotFoundFileException.php new file mode 100644 index 0000000..cfdebda --- /dev/null +++ b/src/NotFoundFileException.php @@ -0,0 +1,17 @@ +> The event names to listen to + */ + public static function getSubscribedEvents(){ + return [ + PackageEvents::PRE_PACKAGE_UPDATE => 'prePackageUpdate', + ]; + } + + public function prePackageUpdate(PackageEvent $event) + { + $event->getIO()->write("Test : {$event->getOperation()}"); + } /** * Apply plugin modifications to Composer @@ -24,7 +55,13 @@ class Plugin implements PluginInterface * @return void */ public function activate(Composer $composer, IOInterface $io){ - + $this->composer = $composer; + $this->io = $io; + try { + $this->updateConfig($this->getConfigJsonFile()); + } catch (NotFoundFileException $ex){ + // do nothing + } } /** @@ -50,4 +87,100 @@ class Plugin implements PluginInterface public function uninstall(Composer $composer, IOInterface $io){ } + + /** + * get config.json File from root path + * @return array ['config' => $config,'path'=>path] + * @throws NotFoundFileException + */ + protected function getConfigJsonFile(): array + { + $file = new JsonFile('./main-config.json'); + if (!$file->exists()){ + throw new NotFoundFileException("main-config.json file not existing"); + } + $config = $file->read(); + if (!is_array($config)){ + $this->io->write('Error loading main-config.json : it should contain an array'); + throw new NotFoundFileException("main-config.json file not existing"); + } + return ['config'=>$config,'path'=>$file->getPath()]; + } + + /** + * update config with configFile + * @param array $config + */ + protected function updateConfig(array $config) + { + if (isset($config['config']['local-repositories']) && is_array($config['config']['local-repositories'])){ + $composerConfig = $this->composer->getConfig(); + foreach($config['config']['local-repositories'] as $name => $packageName){ + if (is_string($name) && !empty(basename($name)) + && is_string($packageName) && !empty($packageName) && !in_array(basename($name),['.','..'])){ + $projectName = basename($name); + $dirName = "../$projectName"; + if (is_dir($dirName)){ + $this->io->write("Using local path for '$projectName'"); + // $previousRepositories = $composerConfig->all()['repositories']; + // $this->io->write("previous : ".json_encode($previousRepositories).""); + // $newRepositories = $this->replaceFromConfigWithPath($composerConfig,$projectName,$packageName); + // $this->io->write("new : ".json_encode($newRepositories).""); + $composerConfig->merge([ + 'repositories' => [ + $packageName => [ + 'type' => 'path', + 'url' => "../$projectName", + 'options' => [ + 'name' => $packageName, + 'versions' => [ + $packageName => 'dev-master', + 'symlink' => true + ] + ] + ] + ] + ]); + $previousRepositories = $composerConfig->all()['repositories']; + $this->io->write("previous : ".json_encode($previousRepositories).""); + } + } + } + } + } + + /** + * replace from config repositories vcs by path + * @param Config $composerConfig + * @param string $projectName + * @param string $packageName + * @return array $newRepositories + */ + protected function replaceFromConfigWithPath( + Config $composerConfig, + string $projectName, + string $packageName): array + { + $repositories = $composerConfig->all()['repositories']; + foreach($repositories as $idx => $data){ + if (is_array($data) && isset($data['type']) && $data['type'] === 'vcs' && + isset($data['url']) && is_string($data['url']) && + substr($data['url'],0,-strlen($projectName)) == 'https://git.defis.info/SeaCMS/' && + substr($data['url'],-strlen($projectName)) == $projectName + ){ + $repositories[$idx] = [ + 'type' => 'path', + 'url' => "../$projectName", + 'options' => [ + 'name' => $packageName, + 'versions' => [ + $packageName => 'dev-master', + 'symlink' => true + ] + ] + ]; + } + } + return $repositories; + } } \ No newline at end of file