PackagistExtension.php 997 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. 'packagistPackageName' => new \Twig_Test_Method($this, 'validPackageNameTest'),
  18. 'existingPackagistPackage' => new \Twig_Test_Method($this, 'packageExistsTest')
  19. );
  20. }
  21. public function getName()
  22. {
  23. return 'packagist';
  24. }
  25. public function packageExistsTest($package)
  26. {
  27. return $this->doctrine->getRepository('PackagistWebBundle:Package')
  28. ->packageExists($package);
  29. }
  30. public function validPackageNameTest($package)
  31. {
  32. return preg_match('/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+/', $package);
  33. }
  34. }