remove_tests_directory($composer, $io); $this->replace_namespace($composer, $io); $this->replace_textdomain($composer, $io); } /** * Deactivates the Composer plugin. * * This method is called when the plugin is deactivated during the Composer lifecycle. * Although this implementation does nothing, the method must be defined to fulfill * the interface or abstract class requirements. * * @param Composer $composer The Composer instance. * @param IOInterface $io The IO interface for input/output operations. * * @return void */ public function deactivate(Composer $composer, IOInterface $io) { // do nothing, empty body needed to extend the abstract methods. } /** * Uninstalls the Composer plugin. * * This method is called when the plugin is removed from the Composer environment. * It provides an opportunity to clean up any configurations or state. * In this implementation, no action is performed. * * @param Composer $composer The Composer instance. * @param IOInterface $io The IO interface for input/output operations. * * @return void */ public function uninstall(Composer $composer, IOInterface $io) { // do nothing, empty body needed to extend the abstract methods. } /** * This method replaces the php namespace placeholder * present in onboading wizard source with valid php namespace. * * @param Composer $composer The Composer instance. * @param IOInterface $io The IO interface for input/output operations. * @return void */ public function replace_namespace(Composer $composer, IOInterface $io): void { $php_namespace = self::get_php_namespace($composer->getPackage()->getName()); $file_recursive_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(dirname(__DIR__))); foreach ($file_recursive_iterator as $file) { if (!$file->isFile() || $file->getExtension() !== 'php') continue; $path = $file->getRealPath(); $content = file_get_contents($path); if (strpos($content, self::$namespace_placeholder) !== false) { file_put_contents($path, str_replace(self::$namespace_placeholder, $php_namespace, $content)); $io->write(sprintf('Replaced %s namespace placeholder with %s in file: %s', self::$namespace_placeholder, $php_namespace, $path)); } } } /** * This method replaces the textdomain placeholder * present in onboading wizard source with valid textdomain. * * @param Composer $composer The Composer instance. * @param IOInterface $io The IO interface for input/output operations. * @return void */ private function replace_textdomain(Composer $composer, IOInterface $io) { $package_name = $composer->getPackage()->getName(); // Use the root package name, for example if its team-updraft/updraftplus // then the text domain would be replaced with `updraftplus`. $parts = explode('/', $package_name); $replacement = $parts[1]; $file_recursive_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(dirname(__DIR__))); foreach ($file_recursive_iterator as $file) { if (!$file->isFile() || !in_array($file->getExtension(), array('php', 'js'))) continue; $path = $file->getRealPath(); $content = file_get_contents($path); if (strpos($content, self::$textdomain_placeholder) !== false) { file_put_contents($path, str_replace(self::$textdomain_placeholder, $replacement, $content)); $io->write(sprintf('Replaced %s namespace placeholder with %s in file: %s', self::$textdomain_placeholder, $replacement, $path)); } } } /** * Remove the tests directory from the package. * * @param Composer $composer The Composer instance. * @param IOInterface $io The IO interface for input/output operations. * @return void */ private function remove_tests_directory(Composer $composer, IOInterface $io): void { $rootDir = dirname(__DIR__); $testsDir = $rootDir . DIRECTORY_SEPARATOR . 'tests'; if (is_dir($testsDir)) { $this->delete_directory($testsDir); $io->write(sprintf('Removed tests directory: %s', $testsDir)); } } /** * Recursively delete a directory and its contents. * * @param string $dir Path to the directory to delete. * @return void */ private function delete_directory(string $dir): void { $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST ); foreach ($iterator as $file) { if ($file->isDir()) { rmdir($file->getPathname()); } else { unlink($file->getPathname()); } } rmdir($dir); } }