PackagistExtension.php 872 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Packagist\WebBundle\Twig;
  3. use Symfony\Bridge\Doctrine\RegistryInterface;
  4. class PackagistExtension extends \Twig_Extension
  5. {
  6. /**
  7. * @var \Symfony\Bridge\Doctrine\RegistryInterface
  8. */
  9. private $doctrine;
  10. public function __construct(RegistryInterface $doctrine)
  11. {
  12. $this->doctrine = $doctrine;
  13. }
  14. public function getTests()
  15. {
  16. return array(
  17. 'existing_package' => new \Twig_Test_Method($this, 'packageExistsTest')
  18. );
  19. }
  20. public function getName()
  21. {
  22. return 'packagist';
  23. }
  24. public function packageExistsTest($package)
  25. {
  26. if (!preg_match('/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/', $package)) {
  27. return false;
  28. }
  29. return $this->doctrine->getRepository('PackagistWebBundle:Package')
  30. ->packageExists($package);
  31. }
  32. }