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();
 }
Exemple #2
0
 private function updateFileCleanly($json, array $base, array $new, $requireKey)
 {
     $contents = file_get_contents($json->getPath());
     $manipulator = new JsonManipulator($contents);
     foreach ($new as $package => $constraint) {
         if (!$manipulator->addLink($requireKey, $package, $constraint)) {
             return false;
         }
     }
     file_put_contents($json->getPath(), $manipulator->getContents());
     return true;
 }
Exemple #3
0
 protected function manipulateJson($method, $args, $fallback)
 {
     $args = func_get_args();
     array_shift($args);
     $fallback = array_pop($args);
     if ($this->file->exists()) {
         $contents = file_get_contents($this->file->getPath());
     } elseif ($this->authConfig) {
         $contents = "{\n}\n";
     } else {
         $contents = "{\n    \"config\": {\n    }\n}\n";
     }
     $manipulator = new JsonManipulator($contents);
     $newFile = !$this->file->exists();
     if ($this->authConfig && $method === 'addConfigSetting') {
         $method = 'addSubNode';
         list($mainNode, $name) = explode('.', $args[0], 2);
         $args = array($mainNode, $name, $args[1]);
     } elseif ($this->authConfig && $method === 'removeConfigSetting') {
         $method = 'removeSubNode';
         list($mainNode, $name) = explode('.', $args[0], 2);
         $args = array($mainNode, $name);
     }
     if (call_user_func_array(array($manipulator, $method), $args)) {
         file_put_contents($this->file->getPath(), $manipulator->getContents());
     } else {
         $config = $this->file->read();
         $this->arrayUnshiftRef($args, $config);
         call_user_func_array($fallback, $args);
         $this->file->write($config);
     }
     if ($newFile) {
         @chmod($this->file->getPath(), 0600);
     }
 }
 protected function manipulateJson($method, $args, $fallback)
 {
     $args = func_get_args();
     array_shift($args);
     $fallback = array_pop($args);
     if ($this->file->exists()) {
         $contents = file_get_contents($this->file->getPath());
     } else {
         $contents = "{\n    \"config\": {\n    }\n}\n";
     }
     $manipulator = new JsonManipulator($contents);
     $newFile = !$this->file->exists();
     if (call_user_func_array(array($manipulator, $method), $args)) {
         file_put_contents($this->file->getPath(), $manipulator->getContents());
     } else {
         $config = $this->file->read();
         array_unshift($args, $config);
         call_user_func_array($fallback, $args);
         $this->file->write($config);
     }
     if ($newFile) {
         @chmod($this->file->getPath(), 0600);
     }
 }
    public function testRemoveConfigSettingCanRemoveSubKeyInHashWithSiblings()
    {
        $manipulator = new JsonManipulator('{
    "config": {
        "foo": "bar",
        "github-oauth": {
            "github.com": "foo",
            "bar": "baz"
        }
    }
}');
        $this->assertTrue($manipulator->removeConfigSetting('github-oauth.bar'));
        $this->assertEquals('{
    "config": {
        "foo": "bar",
        "github-oauth": {
            "github.com": "foo"
        }
    }
}
', $manipulator->getContents());
    }
Exemple #6
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));
     $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}).");
     }
 }
Exemple #7
0
 private function updateFileCleanly($json, array $base, array $new, $rootKey)
 {
     $contents = file_get_contents($json->getPath());
     $manipulator = new JsonManipulator($contents);
     foreach ($new as $childKey => $childValue) {
         if (!$manipulator->addLink($rootKey, $childKey, $childValue)) {
             return false;
         }
     }
     file_put_contents($json->getPath(), $manipulator->getContents());
     return true;
 }
Exemple #8
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.");
 }
Exemple #9
0
 /**
  * Cleanly update a Composer JSON file.
  *
  * @param JsonFile $json
  * @param array    $new
  * @param string   $requireKey
  * @param string   $removeKey
  * @param boolean  $sortPackages
  * @param boolean  $postreset
  *
  * @return boolean
  */
 private function updateFileCleanly(JsonFile $json, array $new, $requireKey, $removeKey, $sortPackages, $postreset)
 {
     $contents = file_get_contents($json->getPath());
     $manipulator = new JsonManipulator($contents);
     foreach ($new as $package => $constraint) {
         if ($postreset) {
             $constraint = $this->findBestVersionForPackage($package);
         }
         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;
 }
    public function testUpdateMainKey2()
    {
        $manipulator = new JsonManipulator('{
    "a": {
        "foo": "bar",
        "baz": "qux"
    },
    "foo": "bar",
    "baz": "bar"
}');
        $this->assertTrue($manipulator->addMainKey('foo', 'baz'));
        $this->assertTrue($manipulator->addMainKey('baz', 'quux'));
        $this->assertEquals('{
    "a": {
        "foo": "bar",
        "baz": "qux"
    },
    "foo": "baz",
    "baz": "quux"
}
', $manipulator->getContents());
    }
Exemple #11
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;
 }
Exemple #12
0
 protected function manipulateJson($method, $args, $fallback)
 {
     $args = func_get_args();
     // remove method & fallback
     array_shift($args);
     $fallback = array_pop($args);
     if ($this->file->exists()) {
         $contents = file_get_contents($this->file->getPath());
     } elseif ($this->authConfig) {
         $contents = "{\n}\n";
     } else {
         $contents = "{\n    \"config\": {\n    }\n}\n";
     }
     $manipulator = new JsonManipulator($contents);
     $newFile = !$this->file->exists();
     // override manipulator method for auth config files
     if ($this->authConfig && $method === 'addConfigSetting') {
         $method = 'addSubNode';
         list($mainNode, $name) = explode('.', $args[0], 2);
         $args = array($mainNode, $name, $args[1]);
     } elseif ($this->authConfig && $method === 'removeConfigSetting') {
         $method = 'removeSubNode';
         list($mainNode, $name) = explode('.', $args[0], 2);
         $args = array($mainNode, $name);
     }
     // try to update cleanly
     if (call_user_func_array(array($manipulator, $method), $args)) {
         file_put_contents($this->file->getPath(), $manipulator->getContents());
     } else {
         // on failed clean update, call the fallback and rewrite the whole file
         $config = $this->file->read();
         $this->arrayUnshiftRef($args, $config);
         call_user_func_array($fallback, $args);
         $this->file->write($config);
     }
     if ($newFile) {
         Silencer::call('chmod', $this->file->getPath(), 0600);
     }
 }
Exemple #13
0
 protected function updateFileCleanly(JsonFile $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;
 }
    public function testAddConfigSettingCanOverwriteArrays()
    {
        $manipulator = new JsonManipulator('{
    "config": {
        "github-oauth": {
            "github.com": "foo"
        },
        "github-protocols": ["https"]
    }
}');

        $this->assertTrue($manipulator->addConfigSetting('github-protocols', array('https', 'http')));
        $this->assertEquals('{
    "config": {
        "github-oauth": {
            "github.com": "foo"
        },
        "github-protocols": ["https", "http"]
    }
}
', $manipulator->getContents());

        $this->assertTrue($manipulator->addConfigSetting('github-oauth', array('github.com' => 'bar', 'alt.example.org' => 'baz')));
        $this->assertEquals('{
    "config": {
        "github-oauth": {
            "github.com": "bar",
            "alt.example.org": "baz"
        },
        "github-protocols": ["https", "http"]
    }
}
', $manipulator->getContents());
    }
    public function testIndentDetection()
    {
        $manipulator = new JsonManipulator('{

  "require": {
    "php": "5.*"
  }
}');
        $this->assertTrue($manipulator->addMainKey('require-dev', array('foo' => 'qux')));
        $this->assertEquals('{

  "require": {
    "php": "5.*"
  },
  "require-dev": {
    "foo": "qux"
  }
}
', $manipulator->getContents());
    }
 protected function manipulateJson($method, $args, $fallback)
 {
     $args = func_get_args();
     // remove method & fallback
     array_shift($args);
     $fallback = array_pop($args);
     if ($this->file->exists()) {
         $contents = file_get_contents($this->file->getPath());
     } else {
         $contents = "{\n    \"config\": {\n    }\n}\n";
     }
     $manipulator = new JsonManipulator($contents);
     $newFile = !$this->file->exists();
     // try to update cleanly
     if (call_user_func_array(array($manipulator, $method), $args)) {
         file_put_contents($this->file->getPath(), $manipulator->getContents());
     } else {
         // on failed clean update, call the fallback and rewrite the whole file
         $config = $this->file->read();
         array_unshift($args, $config);
         call_user_func_array($fallback, $args);
         $this->file->write($config);
     }
     if ($newFile) {
         chmod($this->file->getPath(), 0600);
     }
 }
Exemple #17
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;
 }
 /**
  * @dataProvider linkProvider
  */
 public function testAddLink($json, $type, $package, $constraint, $expected)
 {
     $manipulator = new JsonManipulator($json);
     $this->assertTrue($manipulator->addLink($type, $package, $constraint));
     $this->assertEquals($expected, $manipulator->getContents());
 }