StatusExtension.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\CoreBundle\Twig\Extension;
  11. use Sonata\CoreBundle\Component\Status\StatusClassRendererInterface;
  12. /**
  13. * @author Hugo Briand <briand@ekino.com>
  14. */
  15. class StatusExtension extends \Twig_Extension
  16. {
  17. /**
  18. * @var StatusClassRendererInterface[]
  19. */
  20. protected $statusServices = array();
  21. /**
  22. * Adds a renderer to the status services list.
  23. *
  24. * @param StatusClassRendererInterface $renderer
  25. */
  26. public function addStatusService(StatusClassRendererInterface $renderer)
  27. {
  28. $this->statusServices[] = $renderer;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getFilters()
  34. {
  35. return array(
  36. new \Twig_SimpleFilter('sonata_status_class', array($this, 'statusClass')),
  37. );
  38. }
  39. /**
  40. * @param mixed $object
  41. * @param mixed $statusType
  42. * @param string $default
  43. *
  44. * @return string
  45. */
  46. public function statusClass($object, $statusType = null, $default = '')
  47. {
  48. /** @var StatusClassRendererInterface $statusService */
  49. foreach ($this->statusServices as $statusService) {
  50. if ($statusService->handlesObject($object, $statusType)) {
  51. return $statusService->getStatusClass($object, $statusType, $default);
  52. }
  53. }
  54. return $default;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getName()
  60. {
  61. return 'sonata_core_status';
  62. }
  63. }