/**
  * {@inheritDoc}
  */
 public function getCredentials(Request $request)
 {
     $inputData = new JsonArray($request->getContent());
     if (!($inputData->has('username') && $inputData->has('password'))) {
         // post data? Return null and no other methods will be called
         return null;
     }
     // What you return here will be passed to getUser() as $credentials
     return (object) ['username' => $inputData->get('username'), 'password' => $inputData->get('password')];
 }
 /**
  * Test that the deletion works.
  *
  * @param string $setter    The setter to call.
  *
  * @param string $getter    The getter to call.
  *
  * @param string $configKey The config key in the json array that will get populated.
  *
  * @param mixed  $value     The value to pass.
  *
  * @param mixed  $default   The default value that shall be returned if nothing has been set.
  *
  * @return void
  *
  * @dataProvider providerTestDelegation
  */
 public function testDelegation($setter, $getter, $configKey, $value, $default)
 {
     $array = new JsonArray();
     $config = new TensideJsonConfig($array);
     $this->assertEquals($default, call_user_func([$config, $getter]));
     $this->assertSame($config, call_user_func([$config, $setter], $value));
     $this->assertEquals($value, $array->get($configKey));
     $this->assertEquals($value, call_user_func([$config, $getter]));
 }
 /**
  * Test that the reason of root requirements get converted.
  *
  * @return void
  */
 public function testRootInstallReasonGetsConverted()
 {
     $json = new JsonArray();
     $manager = new InstallationManager($json);
     $installer = $this->getMockForAbstractClass(InstallerInterface::class);
     $pool = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock();
     $manager->addInstaller($installer);
     $manager->setPool($pool);
     $reason = $this->getMockBuilder(Rule::class)->disableOriginalConstructor()->getMock();
     $reason->method('getPrettyString')->with($pool)->willReturn('The reason is 42');
     $reason->method('getReason')->willReturn(Rule::RULE_JOB_INSTALL);
     $repository = $this->getMockForAbstractClass(InstalledRepositoryInterface::class);
     $package = $this->getMockForAbstractClass(PackageInterface::class);
     $operation = new InstallOperation($package, $reason);
     $package->method('getType')->will($this->returnValue('library'));
     $package->method('getPrettyName')->will($this->returnValue('vendor/package'));
     $installer->expects($this->once())->method('supports')->with('library')->will($this->returnValue(true));
     $installer->expects($this->once())->method('install')->with($repository, $package);
     $manager->install($repository, $operation);
     $this->assertEquals('Required by the root package: The reason is 42', $json->get('vendor\\/package/reason'));
 }
Exemple #4
0
 /**
  * {@inheritDoc}
  */
 public function get($path, $forceArray = false)
 {
     return $this->data->get($path, $forceArray);
 }
 /**
  * Update the information of a package in the composer.json.
  *
  * Note that the payload name of the package must match the vendor and package passed as parameter.
  *
  * @param string  $vendor  The name of the vendor.
  *
  * @param string  $package The name of the package.
  *
  * @param Request $request The request to process.
  *
  * @return JsonResponse
  *
  * @throws NotAcceptableHttpException When the passed payload is invalid.
  * @throws NotFoundHttpException When the package has not been found.
  *
  * @ApiDoc(
  *   section="package",
  *   statusCodes = {
  *     200 = "When everything worked out ok"
  *   },
  *   authentication = true,
  *   authenticationRoles = {
  *     "ROLE_MANIPULATE_REQUIREMENTS"
  *   }
  * )
  *
  * @ApiDescription(
  *   request={
  *     "name" = {
  *       "dataType" = "string",
  *       "description" = "The name of the package",
  *       "required" = true
  *     },
  *     "constraint" = {
  *       "dataType" = "string",
  *       "description" = "The constraint of the package (when package is installed)",
  *       "required" = true
  *     },
  *     "locked" = {
  *       "dataType" = "string",
  *       "description" = "Flag if the package has been locked for updates",
  *       "required" = true
  *     },
  *   },
  *   response={
  *     "name" = {
  *       "dataType" = "string",
  *       "description" = "The name of the package"
  *     },
  *     "version" = {
  *       "dataType" = "string",
  *       "description" = "The version of the package"
  *     },
  *     "constraint" = {
  *       "dataType" = "string",
  *       "description" = "The constraint of the package (when package is installed)"
  *     },
  *     "type" = {
  *       "dataType" = "string",
  *       "description" = "The noted package type"
  *     },
  *     "locked" = {
  *       "dataType" = "string",
  *       "description" = "Flag if the package has been locked for updates"
  *     },
  *     "time" = {
  *       "dataType" = "datetime",
  *       "description" = "The release date"
  *     },
  *     "upgrade_version" = {
  *       "dataType" = "string",
  *       "description" = "The version available for upgrade (optional, if any)"
  *     },
  *     "description" = {
  *       "dataType" = "string",
  *       "description" = "The package description"
  *     },
  *     "license" = {
  *       "actualType" = "collection",
  *       "subType" = "string",
  *       "description" = "The licenses"
  *     },
  *     "keywords" = {
  *       "actualType" = "collection",
  *       "subType" = "string",
  *       "description" = "The keywords"
  *     },
  *     "homepage" = {
  *       "dataType" = "string",
  *       "description" = "The support website (optional, if any)"
  *     },
  *     "authors" = {
  *       "actualType" = "collection",
  *       "subType" = "object",
  *       "description" = "The authors",
  *       "children" = {
  *         "name" = {
  *           "dataType" = "string",
  *           "description" = "Full name of the author (optional, if any)"
  *         },
  *         "homepage" = {
  *           "dataType" = "string",
  *           "description" = "Email address of the author (optional, if any)"
  *         },
  *         "email" = {
  *           "dataType" = "string",
  *           "description" = "Homepage URL for the author (optional, if any)"
  *         },
  *         "role" = {
  *           "dataType" = "string",
  *           "description" = "Author's role in the project (optional, if any)"
  *         }
  *       }
  *     },
  *     "support" = {
  *       "actualType" = "collection",
  *       "subType" = "object",
  *       "description" = "The support options",
  *       "children" = {
  *         "email" = {
  *           "dataType" = "string",
  *           "description" = "Email address for support (optional, if any)"
  *         },
  *         "issues" = {
  *           "dataType" = "string",
  *           "description" = "URL to the issue tracker (optional, if any)"
  *         },
  *         "forum" = {
  *           "dataType" = "string",
  *           "description" = "URL to the forum (optional, if any)"
  *         },
  *         "wiki" = {
  *           "dataType" = "string",
  *           "description" = "URL to the wiki (optional, if any)"
  *         },
  *         "irc" = {
  *           "dataType" = "string",
  *           "description" = "IRC channel for support, as irc://server/channel (optional, if any)"
  *         },
  *         "source" = {
  *           "dataType" = "string",
  *           "description" = "URL to browse or download the sources (optional, if any)"
  *         },
  *         "docs" = {
  *           "dataType" = "string",
  *           "description" = "URL to the documentation (optional, if any)"
  *         },
  *       }
  *     },
  *     "abandoned" = {
  *       "dataType" = "boolean",
  *       "description" = "Flag if this package is abandoned"
  *     },
  *     "replacement" = {
  *       "dataType" = "string",
  *       "description" = "Replacement for this package (optional, if any)"
  *     }
  *   }
  * )
  */
 public function putPackageAction($vendor, $package, Request $request)
 {
     $packageName = $vendor . '/' . $package;
     $info = new JsonArray($request->getContent());
     $name = $info->get('name');
     if (!($info->has('name') && $info->has('locked') && $info->has('constraint'))) {
         throw new NotAcceptableHttpException('Invalid package information.');
     }
     if ($name !== $packageName) {
         throw new NotAcceptableHttpException('Package name mismatch ' . $packageName . ' vs. ' . $name . '.');
     }
     $composer = $this->getComposer();
     $json = $this->get('tenside.composer_json');
     $package = $this->findPackage($name, $composer->getRepositoryManager()->getLocalRepository());
     if (null === $package) {
         throw new NotFoundHttpException('Package ' . $packageName . ' not found.');
     }
     $json->setLock($package, $info->get('locked'));
     return $this->forward('TensideCoreBundle:Package:getPackage');
 }
 /**
  * Determine the life time for the token.
  *
  * This examines the GET parameters if a field "ttl" has been set.
  * If not, it examines the JSON post data for a field named ttl.
  *
  * @param Request $request The request.
  *
  * @return int|null
  */
 private function determineLifeTime(Request $request)
 {
     if ($lifetime = $request->query->getInt('ttl')) {
         return $this->revertToNullOnMinusOne($lifetime);
     }
     try {
         $inputData = new JsonArray($request->getContent());
         if ($inputData->has('ttl')) {
             return $this->revertToNullOnMinusOne(intval($inputData->get('ttl')));
         }
     } catch (\Exception $e) {
         // Swallow exception, we need to return a defined result.
     }
     return 3600;
 }
 /**
  * Search for packages.
  *
  * @param Request $request The search request.
  *
  * @return JsonResponse
  *
  * @ApiDoc(
  *   section="search",
  *   statusCodes = {
  *     200 = "When everything worked out ok"
  *   },
  *   authentication = true,
  *   authenticationRoles = {
  *     "ROLE_MANIPULATE_REQUIREMENTS"
  *   }
  * )
  * @ApiDescription(
  *   request={
  *    "keywords" = {
  *      "dataType" = "string",
  *      "description" = "The name of the project to search or any other keyword.",
  *      "required" = true
  *    },
  *    "type" = {
  *      "dataType" = "choice",
  *      "description" = "The type of package to search (optional, default: all).",
  *      "format" = "['installed', 'contao', 'all']",
  *      "required" = false
  *    },
  *    "threshold" = {
  *      "dataType" = "int",
  *      "description" = "The amount of results after which the search shall be stopped (optional, default: 20).",
  *      "required" = false
  *    }
  *   },
  *   response={
  *     "package name 1...n" = {
  *       "actualType" = "object",
  *       "subType" = "object",
  *       "description" = "The content of the packages",
  *       "children" = {
  *         "name" = {
  *           "dataType" = "string",
  *           "description" = "The name of the package"
  *         },
  *         "version" = {
  *           "dataType" = "string",
  *           "description" = "The version of the package"
  *         },
  *         "constraint" = {
  *           "dataType" = "string",
  *           "description" = "The constraint of the package (when package is installed)"
  *         },
  *         "type" = {
  *           "dataType" = "string",
  *           "description" = "The noted package type"
  *         },
  *         "locked" = {
  *           "dataType" = "string",
  *           "description" = "Flag if the package has been locked for updates"
  *         },
  *         "time" = {
  *           "dataType" = "datetime",
  *           "description" = "The release date"
  *         },
  *         "upgrade_version" = {
  *           "dataType" = "string",
  *           "description" = "The version available for upgrade (optional, if any)"
  *         },
  *         "description" = {
  *           "dataType" = "string",
  *           "description" = "The package description"
  *         },
  *         "license" = {
  *           "actualType" = "collection",
  *           "subType" = "string",
  *           "description" = "The licenses"
  *         },
  *         "keywords" = {
  *           "actualType" = "collection",
  *           "subType" = "string",
  *           "description" = "The keywords"
  *         },
  *         "homepage" = {
  *           "dataType" = "string",
  *           "description" = "The support website (optional, if any)"
  *         },
  *         "authors" = {
  *           "actualType" = "collection",
  *           "subType" = "object",
  *           "description" = "The authors",
  *           "children" = {
  *             "name" = {
  *               "dataType" = "string",
  *               "description" = "Full name of the author (optional, if any)"
  *             },
  *             "homepage" = {
  *               "dataType" = "string",
  *               "description" = "Email address of the author (optional, if any)"
  *             },
  *             "email" = {
  *               "dataType" = "string",
  *               "description" = "Homepage URL for the author (optional, if any)"
  *             },
  *             "role" = {
  *               "dataType" = "string",
  *               "description" = "Author's role in the project (optional, if any)"
  *             }
  *           }
  *         },
  *         "support" = {
  *           "actualType" = "collection",
  *           "subType" = "object",
  *           "description" = "The support options",
  *           "children" = {
  *             "email" = {
  *               "dataType" = "string",
  *               "description" = "Email address for support (optional, if any)"
  *             },
  *             "issues" = {
  *               "dataType" = "string",
  *               "description" = "URL to the issue tracker (optional, if any)"
  *             },
  *             "forum" = {
  *               "dataType" = "string",
  *               "description" = "URL to the forum (optional, if any)"
  *             },
  *             "wiki" = {
  *               "dataType" = "string",
  *               "description" = "URL to the wiki (optional, if any)"
  *             },
  *             "irc" = {
  *               "dataType" = "string",
  *               "description" = "IRC channel for support, as irc://server/channel (optional, if any)"
  *             },
  *             "source" = {
  *               "dataType" = "string",
  *               "description" = "URL to browse or download the sources (optional, if any)"
  *             },
  *             "docs" = {
  *               "dataType" = "string",
  *               "description" = "URL to the documentation (optional, if any)"
  *             },
  *           }
  *         },
  *         "abandoned" = {
  *           "dataType" = "boolean",
  *           "description" = "Flag if this package is abandoned"
  *         },
  *         "replacement" = {
  *           "dataType" = "string",
  *           "description" = "Replacement for this package (optional, if any)"
  *         },
  *         "installed" = {
  *           "dataType" = "int",
  *           "description" = "Amount of installations"
  *         },
  *         "downloads" = {
  *           "dataType" = "int",
  *           "description" = "Amount of downloads"
  *         },
  *         "favers" = {
  *           "dataType" = "int",
  *           "description" = "Amount of favers"
  *         },
  *       }
  *     }
  *   }
  * )
  */
 public function searchAction(Request $request)
 {
     $composer = $this->getComposer();
     $data = new JsonArray($request->getContent());
     $keywords = $data->get('keywords');
     $type = $data->get('type');
     $threshold = $data->has('threshold') ? $data->get('threshold') : 20;
     $localRepository = $composer->getRepositoryManager()->getLocalRepository();
     $searcher = $this->getRepositorySearch($keywords, $type, $composer, $threshold);
     $results = $searcher->searchAndDecorate($keywords, $this->getFilters($type));
     $responseData = [];
     $rootPackage = $composer->getPackage();
     $converter = new PackageConverter($rootPackage);
     foreach ($results as $versionedResult) {
         /** @var VersionedPackage $versionedResult */
         // Might have no version matching the current stability setting.
         if (null === ($latestVersion = $versionedResult->getLatestVersion())) {
             continue;
         }
         $package = $converter->convertPackageToArray($latestVersion);
         $package->set('installed', $this->getInstalledVersion($localRepository, $versionedResult->getName()))->set('downloads', $versionedResult->getMetaData('downloads'))->set('favers', $versionedResult->getMetaData('favers'));
         $responseData[$package->get('name')] = $package->getData();
     }
     return JsonResponse::create($responseData)->setEncodingOptions(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_FORCE_OBJECT);
 }
 /**
  * Test that a RemovePackageTask task is created properly.
  *
  * @return void
  */
 public function testOnCreateTaskRemovePackageTask()
 {
     $factory = new ComposerTaskFactory(new HomePathDeterminator($this->getTempDir()));
     $task = $factory->createInstance('remove-package', $config = new JsonArray([RemovePackageTask::SETTING_TYPE => 'remove-package', RemovePackageTask::SETTING_ID => 'remove-task-id', RemovePackageTask::SETTING_PACKAGE => ['vendor/dependency-name', '1.0.0'], 'status' => RemovePackageTask::STATE_PENDING]));
     $this->assertInstanceOf(RemovePackageTask::class, $task);
     $this->assertEquals($this->getTempDir(), $config->get(RemovePackageTask::SETTING_HOME));
 }
Exemple #9
0
 /**
  * Test the merge method.
  *
  * @return void
  */
 public function testMerge()
 {
     $json = new JsonArray(['test' => ['key1' => 'value1', 'key2' => 'value2']]);
     $json->merge(['test' => ['key1' => 'value1', 'key2' => 'foobar', 'key3' => 'three']]);
     $this->assertEquals(['test'], $json->getEntries('/'));
     $this->assertEquals(['test/key1', 'test/key2', 'test/key3'], $json->getEntries('/test'));
     $this->assertEquals('foobar', $json->get('/test/key2'));
 }
 /**
  * Retrieve the available versions of a package.
  *
  * NOTE: This method will become inaccessible as soon as the installation is complete.
  *
  * @param string $vendor  The vendor name of the package.
  *
  * @param string $project The name of the package.
  *
  * @return JsonResponse
  *
  * @ApiDoc(
  *   section="install",
  *   statusCodes = {
  *     201 = "When everything worked out ok",
  *     406 = "When the installation is already complete"
  *   },
  * )
  * @ApiDescription(
  *   response={
  *     "versions" = {
  *       "actualType" = "collection",
  *       "subType" = "object",
  *       "description" = "The list of versions",
  *       "children" = {
  *         "name" = {
  *           "dataType" = "string",
  *           "description" = "The name of the package"
  *         },
  *         "version" = {
  *           "dataType" = "string",
  *           "description" = "The version of the package"
  *         },
  *         "version_normalized" = {
  *           "dataType" = "string",
  *           "description" = "The normalized version of the package"
  *         },
  *         "reference" = {
  *           "dataType" = "string",
  *           "description" = "The optional reference"
  *         }
  *       }
  *     }
  *   }
  * )
  */
 public function getProjectVersionsAction($vendor, $project)
 {
     $this->checkUninstalled();
     $url = sprintf('https://packagist.org/packages/%s/%s.json', $vendor, $project);
     $rfs = new RemoteFilesystem($this->getInputOutput());
     $results = $rfs->getContents($url, $url);
     $data = new JsonArray($results);
     $versions = [];
     foreach ($data->get('package/versions') as $information) {
         $version = ['name' => $information['name'], 'version' => $information['version'], 'version_normalized' => $information['version_normalized']];
         $normalized = $information['version'];
         if ('dev-' === substr($normalized, 0, 4)) {
             if (isset($information['extra']['branch-alias'][$normalized])) {
                 $version['version_normalized'] = $information['extra']['branch-alias'][$normalized];
             }
         }
         if (isset($information['source']['reference'])) {
             $version['reference'] = $information['source']['reference'];
         } elseif (isset($information['dist']['reference'])) {
             $version['reference'] = $information['dist']['reference'];
         }
         $versions[] = $version;
     }
     return new JsonResponse(['status' => 'OK', 'versions' => $versions]);
 }
Exemple #11
0
 /**
  * Retrieve the user submitted payload.
  *
  * @return array
  */
 public function getUserData()
 {
     return $this->file->get(self::SETTING_USER_DATA);
 }
 /**
  * Convert the information of all packages in a repository to an array used by json API.
  *
  * @param RepositoryInterface $repository   The repository holding the packages to convert.
  *
  * @param bool                $requiredOnly If true, return only the packages added to the root package as require.
  *
  * @param null|JsonArray      $upgradeList  The package version to show as upgradable to.
  *
  * @return JsonArray
  */
 public function convertRepositoryToArray(RepositoryInterface $repository, $requiredOnly = false, JsonArray $upgradeList = null)
 {
     $requires = $requiredOnly ? $this->rootPackage->getRequires() : false;
     $packages = new JsonArray();
     /** @var \Composer\Package\PackageInterface $package */
     foreach ($repository->getPackages() as $package) {
         $name = $package->getPrettyName();
         $esc = $packages->escape($name);
         if (false === $requires || isset($requires[$name])) {
             $upgradeVersion = null;
             if ($upgradeList && $upgradeList->has($esc)) {
                 $upgradeVersion = $upgradeList->get($esc);
             }
             $packages->set($esc, $this->convertPackageToArray($package, $upgradeVersion)->getData());
         }
     }
     return $packages;
 }
Exemple #13
0
 /**
  * Retrieve when this task got created as ISO 8601 date string.
  *
  * @return \DateTime
  */
 public function getCreatedAt()
 {
     return \DateTime::createFromFormat(DATE_ISO8601, $this->file->get('created-at'));
 }
Exemple #14
0
 /**
  * Create a task instance from the given MetaData.
  *
  * @param JsonArray $config The configuration of the task.
  *
  * @return Task|null
  */
 private function createTaskFromMetaData(JsonArray $config)
 {
     $typeName = $config->get(Task::SETTING_TYPE);
     if ($this->factory->isTypeSupported($typeName)) {
         return $this->factory->createInstance($typeName, $config);
     }
     return null;
 }
 /**
  * Try to validate the version constraint.
  *
  * @param Request $request The request.
  *
  * @return JsonResponse
  *
  * @throws \RuntimeException For invalid user classes.
  *
  * @ApiDoc(
  *   section="misc",
  *   statusCodes = {
  *     200 = "When everything worked out ok",
  *     400 = "When the request payload was invalid."
  *   },
  *   authentication = true,
  *   authenticationRoles = {
  *     "ROLE_NONE"
  *   }
  * )
  * @ApiDescription(
  *   request={
  *     "constraint" = {
  *       "description" = "The constraint to test.",
  *       "dataType" = "string",
  *       "required" = true
  *     }
  *   },
  *   response={
  *    "status" = {
  *      "dataType" = "choice",
  *      "description" = "OK or ERROR",
  *      "format" = "['OK', 'ERROR']",
  *    },
  *    "error" = {
  *      "dataType" = "string",
  *      "description" = "The error message (if any).",
  *    }
  *   }
  * )
  */
 public function checkVersionConstraintAction(Request $request)
 {
     try {
         $inputData = new JsonArray($request->getContent());
     } catch (\Exception $exception) {
         return new JsonResponse(['status' => 'ERROR', 'error' => 'invalid payload'], JsonResponse::HTTP_BAD_REQUEST);
     }
     $versionParser = new VersionParser();
     if (!$inputData->has('constraint')) {
         return new JsonResponse(['status' => 'ERROR', 'error' => 'invalid payload'], JsonResponse::HTTP_BAD_REQUEST);
     }
     try {
         $versionParser->parseConstraints($inputData->get('constraint'));
     } catch (\Exception $exception) {
         return new JsonResponse(['status' => 'ERROR', 'error' => $exception->getMessage()], JsonResponse::HTTP_OK);
     }
     return new JsonResponse(['status' => 'OK'], JsonResponse::HTTP_OK);
 }
Exemple #16
0
 /**
  * Decorate the package with stats from packagist.
  *
  * @param VersionedPackage $package The package version.
  *
  * @return VersionedPackage
  */
 protected function decorateWithPackagistStats(VersionedPackage $package)
 {
     if (null === $this->decorateBaseUrl) {
         return $package;
     }
     $rfs = new RemoteFilesystem(new BufferIO());
     $requestUrl = sprintf($this->decorateBaseUrl, $package->getName());
     if (!($jsonData = $rfs->getContents($requestUrl, $requestUrl))) {
         $this->decorateBaseUrl = null;
         return $package;
     }
     try {
         $data = new JsonArray($jsonData);
     } catch (\RuntimeException $exception) {
         $this->decorateBaseUrl = null;
         return $package;
     }
     $metaPaths = ['downloads' => 'package/downloads/total', 'favers' => 'package/favers'];
     foreach ($metaPaths as $metaKey => $metaPath) {
         $package->addMetaData($metaKey, $data->get($metaPath));
     }
     return $package;
 }