Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow renaming a package #379

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Packagist/WebBundle/Controller/PackageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function editAction(Request $req, $name)
$packageRepo = $this->getDoctrine()->getRepository('PackagistWebBundle:Package');
/** @var $package Package */
$package = $packageRepo->findOneByName($name);
$package->setEntityRepository($this->getDoctrine()->getRepository('PackagistWebBundle:Package'));
$package->setRouter($this->get('router'));

if (!$package) {
throw $this->createNotFoundException("The requested package, $name, could not be found.");
Expand All @@ -42,6 +44,7 @@ public function editAction(Request $req, $name)
}

$form = $this->createFormBuilder($package, array("validation_groups" => array("Update")))
->add("name", "text")
->add("repository", "text")
->getForm();

Expand Down
25 changes: 22 additions & 3 deletions src/Packagist/WebBundle/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @ORM\Index(name="dumped_idx",columns={"dumpedAt"})
* }
* )
* @Assert\Callback(methods={"isPackageUnique"})
* @Assert\Callback(methods={"isPackageUnique"}, groups={"Update", "Default"})
* @Assert\Callback(methods={"isRepositoryValid"}, groups={"Update", "Default"})
* @author Jordi Boggiano <[email protected]>
*/
Expand Down Expand Up @@ -219,9 +219,28 @@ public function setRouter($router)

public function isPackageUnique(ExecutionContext $context)
{
$group = $context->getGroup();

$qb = $this->entityRepository->getBaseQueryBuilder();

if ($group === 'Update') {
$qb->where('p.name = :name AND p.id != :id')
->setParameters(array('name' => $this->name, 'id' => $this->id));
} else {
$qb->where('p.name = :name')
->setParameter('name', $this->name);
}

$query = $qb->getQuery();

try {
if ($this->entityRepository->findOneByName($this->name)) {
$context->addViolationAt('repository', 'A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.', array(), null);
if ($query->getSingleResult()) {
if ($group === 'Update') {
$message = sprintf('A package named %s already exists', $this->name);
} else {
$message = 'A package with the name <a href="'.$this->router->generate('view_package', array('name' => $this->name)).'">'.$this->name.'</a> already exists.';
}
$context->addViolationAt('repository', $message, array(), null);
}
} catch (\Doctrine\ORM\NoResultException $e) {}
}
Expand Down