Url.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config\Argument\Parser;
  7. use Magento\Framework\Data\Argument\InterpreterInterface;
  8. use Magento\Framework\UrlInterface;
  9. use Magento\Framework\UrlFactory;
  10. use Magento\Framework\View\Layout\Argument\Interpreter\NamedParams;
  11. /**
  12. * Interpreter that builds URL by input path and optional parameters
  13. *
  14. * Used isolated instance of UrlInterface since the shared instance has global state causing issues.
  15. */
  16. class Url implements InterpreterInterface
  17. {
  18. /**
  19. * @var UrlInterface
  20. */
  21. private $urlResolver;
  22. /**
  23. * @var NamedParams
  24. */
  25. private $paramsInterpreter;
  26. /**
  27. * @param UrlFactory $urlResolverFactory
  28. * @param NamedParams $paramsInterpreter
  29. */
  30. public function __construct(UrlFactory $urlResolverFactory, NamedParams $paramsInterpreter)
  31. {
  32. $this->urlResolver = $urlResolverFactory->create();
  33. $this->paramsInterpreter = $paramsInterpreter;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. * @return string
  38. * @throws \InvalidArgumentException
  39. */
  40. public function evaluate(array $data)
  41. {
  42. if (!isset($data['path'])) {
  43. throw new \InvalidArgumentException('URL path is missing.');
  44. }
  45. $urlPath = $data['path'];
  46. $urlParams = $this->paramsInterpreter->evaluate($data);
  47. return $this->urlResolver->getUrl($urlPath, $urlParams);
  48. }
  49. }