TemplateFactory.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\TemplateEngine\Xhtml;
  7. use Magento\Framework\Phrase;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. /**
  11. * Class TemplateFactory
  12. */
  13. class TemplateFactory
  14. {
  15. /**
  16. * Object manager
  17. *
  18. * @var ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. /**
  22. * Instance name
  23. *
  24. * @var string
  25. */
  26. protected $instanceName;
  27. /**
  28. * Constructor
  29. *
  30. * @param ObjectManagerInterface $objectManager
  31. * @param string $instanceName
  32. */
  33. public function __construct(
  34. ObjectManagerInterface $objectManager,
  35. $instanceName = \Magento\Framework\View\TemplateEngine\Xhtml\Template::class
  36. ) {
  37. $this->objectManager = $objectManager;
  38. $this->instanceName = $instanceName;
  39. }
  40. /**
  41. * Create result
  42. *
  43. * @param array $arguments
  44. * @return Template
  45. * @throws LocalizedException
  46. */
  47. public function create(array $arguments = [])
  48. {
  49. $object = $this->objectManager->create($this->instanceName, $arguments);
  50. if (!($object instanceof Template)) {
  51. throw new LocalizedException(
  52. new Phrase('This class needs to inherit from a class "Template". Verify the class and try again.')
  53. );
  54. }
  55. return $object;
  56. }
  57. }