protected static function manipulateJsonContents($contents, $removeContents)
 {
     $manipulator = new Json\JsonManipulator($contents);
     foreach ($removeContents as $node => $remove) {
         foreach ((array) $remove as $name) {
             $manipulator->removeSubNode($node, $name);
         }
     }
     return $manipulator->getContents();
 }
예제 #2
0
 /**
  * Cleanly update a Composer JSON file.
  *
  * @param JsonFile $jsonFile
  * @param array    $new
  * @param string   $requireKey
  * @param string   $removeKey
  * @param boolean  $sortPackages
  *
  * @return boolean
  */
 private function updateFileCleanly(JsonFile $jsonFile, array $new, $requireKey, $removeKey, $sortPackages)
 {
     $composerJson = $jsonFile->read();
     $manipulator = new JsonManipulator($composerJson);
     foreach ($new as $package => $constraint) {
         $constraint = $this->findBestVersionForPackage($package, $constraint);
         if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
             return false;
         }
         if (!$manipulator->removeSubNode($removeKey, $package)) {
             return false;
         }
     }
     $jsonFile->put($manipulator->getContents());
     return true;
 }
예제 #3
0
    public function testRemoveSubNodeFromRequire()
    {
        $manipulator = new JsonManipulator('{
    "repositories": [
        {
            "package": {
                "require": {
                    "this/should-not-end-up-in-root-require": "~2.0"
                },
                "require-dev": {
                    "this/should-not-end-up-in-root-require-dev": "~2.0"
                }
            }
        }
    ],
    "require": {
        "package/a": "*",
        "package/b": "*",
        "package/c": "*"
    },
    "require-dev": {
        "package/d": "*"
    }
}');
        $this->assertTrue($manipulator->removeSubNode('require', 'package/c'));
        $this->assertTrue($manipulator->removeSubNode('require-dev', 'package/d'));
        $this->assertEquals('{
    "repositories": [
        {
            "package": {
                "require": {
                    "this/should-not-end-up-in-root-require": "~2.0"
                },
                "require-dev": {
                    "this/should-not-end-up-in-root-require-dev": "~2.0"
                }
            }
        }
    ],
    "require": {
        "package/a": "*",
        "package/b": "*"
    },
    "require-dev": {
    }
}
', $manipulator->getContents());
    }
예제 #4
0
 private function updateFileCleanly($json, array $base, array $new, $requireKey, $removeKey, $sortPackages)
 {
     $contents = file_get_contents($json->getPath());
     $manipulator = new JsonManipulator($contents);
     foreach ($new as $package => $constraint) {
         if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
             return false;
         }
         if (!$manipulator->removeSubNode($removeKey, $package)) {
             return false;
         }
     }
     file_put_contents($json->getPath(), $manipulator->getContents());
     return true;
 }
 /**
  * @dataProvider removeSubNodeProvider
  */
 public function testRemoveSubNode($json, $name, $expected, $expectedContent = null)
 {
     $manipulator = new JsonManipulator($json);
     $this->assertEquals($expected, $manipulator->removeSubNode('repositories', $name));
     if (null !== $expectedContent) {
         $this->assertEquals($expectedContent, $manipulator->getContents());
     }
 }
예제 #6
0
파일: package.php 프로젝트: wp-cli/wp-cli
 /**
  * Uninstall a WP-CLI package.
  *
  * ## OPTIONS
  *
  * <name>
  * : Name of the package to uninstall.
  *
  * ## EXAMPLES
  *
  *     $ wp package uninstall wp-cli/server-command
  *     Removing require statement from /home/person/.wp-cli/packages/composer.json
  *     Deleting package directory /home/person/.wp-cli/packages/vendor/wp-cli/server-command
  *     Regenerating Composer autoload.
  *     Success: Uninstalled package.
  */
 public function uninstall($args)
 {
     list($package_name) = $args;
     try {
         $composer = $this->get_composer();
     } catch (Exception $e) {
         WP_CLI::error($e->getMessage());
     }
     if (false === ($package = $this->get_installed_package_by_name($package_name))) {
         WP_CLI::error("Package not installed.");
     }
     $composer_json_obj = $this->get_composer_json();
     // Remove the 'require' from composer.json
     $json_path = $composer_json_obj->getPath();
     WP_CLI::log(sprintf('Removing require statement from %s', $json_path));
     $composer_backup = file_get_contents($composer_json_obj->getPath());
     $manipulator = new JsonManipulator($composer_backup);
     $manipulator->removeSubNode('require', $package_name);
     file_put_contents($composer_json_obj->getPath(), $manipulator->getContents());
     try {
         $composer = $this->get_composer();
     } catch (Exception $e) {
         WP_CLI::error($e->getMessage());
     }
     // Set up the installer
     $install = Installer::create(new NullIO(), $composer);
     $install->setUpdate(true);
     // Installer class will only override composer.lock with this flag
     $install->setPreferSource(true);
     // Use VCS when VCS for easier contributions.
     WP_CLI::log('Removing package directories and regenerating autoloader...');
     $res = false;
     try {
         $res = $install->run();
     } catch (Exception $e) {
         WP_CLI::warning($e->getMessage());
     }
     if (0 === $res) {
         WP_CLI::success("Uninstalled package.");
     } else {
         file_put_contents($composer_json_obj->getPath(), $composer_backup);
         WP_CLI::error("Package removal failed (Composer return code {$res}).");
     }
 }
예제 #7
0
 /**
  * Uninstall a WP-CLI package.
  *
  * ## OPTIONS
  *
  * <name>
  * : Name of the package to uninstall.
  *
  * ## EXAMPLES
  *
  *     $ wp package uninstall wp-cli/server-command
  *     Removing require statement from /home/person/.wp-cli/packages/composer.json
  *     Deleting package directory /home/person/.wp-cli/packages/vendor/wp-cli/server-command
  *     Regenerating Composer autoload.
  *     Success: Uninstalled package.
  */
 public function uninstall($args)
 {
     list($package_name) = $args;
     try {
         $composer = $this->get_composer();
     } catch (Exception $e) {
         WP_CLI::error($e->getMessage());
     }
     if (false === ($package = $this->get_installed_package_by_name($package_name))) {
         WP_CLI::error("Package not installed.");
     }
     $composer_json_obj = $this->get_composer_json();
     // Remove the 'require' from composer.json
     $json_path = $composer_json_obj->getPath();
     WP_CLI::log(sprintf('Removing require statement from %s', $json_path));
     $contents = file_get_contents($json_path);
     $manipulator = new JsonManipulator($contents);
     $manipulator->removeSubNode('require', $package_name);
     file_put_contents($composer_json_obj->getPath(), $manipulator->getContents());
     // Delete the directory
     $package_path = $composer->getConfig()->get('vendor-dir') . '/' . $package->getName();
     WP_CLI::log(sprintf('Deleting package directory %s', $package_path));
     $filesystem = new Filesystem();
     $filesystem->removeDirectory($package_path);
     // Reset Composer and regenerate the auto-loader
     WP_CLI::log('Regenerating Composer autoload.');
     try {
         $composer = $this->get_composer();
     } catch (Exception $e) {
         WP_CLI::warning($e->getMessage());
         WP_CLI::error('Composer autoload will need to be manually regenerated.');
     }
     $this->regenerate_autoloader($composer);
     WP_CLI::success("Uninstalled package.");
 }
예제 #8
0
 protected function updateFileCleanly(JsonFile $json, array $base, array $new, $requireKey, $removeKey, $sortPackages)
 {
     $extra = $this->extra;
     $contents = file_get_contents($json->getPath());
     $manipulator = new JsonManipulator($contents);
     foreach ($new as $package => $constraint) {
         if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
             return false;
         }
         if (!$manipulator->removeSubNode($removeKey, $package)) {
             return false;
         }
     }
     if (isset($extra['installer-modules']) && is_array($extra['installer-modules'])) {
         $manipulator->addSubNode('extra', 'installer-modules', $extra['installer-modules']);
     }
     $paths = $this->getComposer()->getPackage()->getExtra();
     $paths = $paths['installer-paths'];
     if (isset($extra['installer-paths']) && is_array($extra['installer-paths'])) {
         foreach ($extra['installer-paths'] as $path => $list) {
             if (!is_array($list)) {
                 continue;
             }
             if (isset($paths[$path])) {
                 $result = array_merge($paths[$path], $list);
                 $result = array_keys(array_flip($result));
                 $paths[$path] = $result;
             } else {
                 $paths[$path] = $list;
             }
         }
     }
     if (isset($extra['system-modules']) && is_array($extra['system-modules'])) {
         $modules = $extra['system-modules'];
         foreach ($modules as &$module) {
             if (strpos($module, '/') === false) {
                 $module = 'opis-colibri/' . $module;
             }
         }
         $path = 'system/modules/{$name}/';
         if (isset($paths[$path])) {
             $result = array_merge($paths[$path], $modules);
             $result = array_keys(array_flip($result));
             $paths[$path] = $result;
         }
     }
     $manipulator->addSubNode('extra', 'installer-paths', $paths);
     file_put_contents($json->getPath(), $manipulator->getContents());
     return true;
 }