addOption('name', 'name', InputOption::VALUE_REQUIRED, 'Plugin name, for example foo/my-admin'); $this->addOption('source', 'source', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Directories to export'); } /** * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln('Export Plugin'); $name = strtolower($input->getOption('name')); if (!strpos($name, '/')) { $output->writeln('Bad name, name must contain character \'/\' , for example foo/MyAdmin'); return self::INVALID; } $namespace = Util::nameToNamespace($name); $path_relations = $input->getOption('source'); if (!in_array("config/plugin/$name", $path_relations)) { if (is_dir("config/plugin/$name")) { $path_relations[] = "config/plugin/$name"; } } $original_dest = $dest = base_path()."/vendor/$name"; $dest .= '/src'; $this->writeInstallFile($namespace, $path_relations, $dest); $output->writeln("Create $dest/Install.php"); foreach ($path_relations as $source) { $base_path = pathinfo("$dest/$source", PATHINFO_DIRNAME); if (!is_dir($base_path)) { mkdir($base_path, 0777, true); } $output->writeln("Copy $source to $dest/$source "); copy_dir($source, "$dest/$source"); } $output->writeln("Saved $name to $original_dest"); return self::SUCCESS; } /** * @param $namespace * @param $path_relations * @param $dest_dir * @return void */ protected function writeInstallFile($namespace, $path_relations, $dest_dir) { if (!is_dir($dest_dir)) { mkdir($dest_dir, 0777, true); } $relations = []; foreach($path_relations as $relation) { $relations[$relation] = $relation; } $relations = var_export($relations, true); $install_php_content = << \$dest) { if (\$pos = strrpos(\$dest, '/')) { \$parent_dir = base_path().'/'.substr(\$dest, 0, \$pos); if (!is_dir(\$parent_dir)) { mkdir(\$parent_dir, 0777, true); } } //symlink(__DIR__ . "/\$source", base_path()."/\$dest"); copy_dir(__DIR__ . "/\$source", base_path()."/\$dest"); echo "Create \$dest\r\n"; } } /** * uninstallByRelation * @return void */ public static function uninstallByRelation() { foreach (static::\$pathRelation as \$source => \$dest) { \$path = base_path()."/\$dest"; if (!is_dir(\$path) && !is_file(\$path)) { continue; } echo "Remove \$dest\r\n"; if (is_file(\$path) || is_link(\$path)) { unlink(\$path); continue; } remove_dir(\$path); } } } EOT; file_put_contents("$dest_dir/Install.php", $install_php_content); } }