FileFactory.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\App\Response\Http;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class FileFactory extends \Magento\Framework\App\Response\Http\FileFactory
  13. {
  14. /**
  15. * @var \Magento\Backend\Model\Auth
  16. */
  17. protected $_auth;
  18. /**
  19. * @var \Magento\Backend\Model\UrlInterface
  20. */
  21. protected $_backendUrl;
  22. /**
  23. * @var \Magento\Framework\App\ResponseInterface
  24. */
  25. protected $_response;
  26. /**
  27. * @var \Magento\Backend\Model\Session
  28. */
  29. protected $_session;
  30. /**
  31. * @var \Magento\Framework\App\ActionFlag
  32. */
  33. protected $_flag;
  34. /**
  35. * @var \Magento\Backend\Helper\Data
  36. */
  37. protected $_helper;
  38. /**
  39. * @param \Magento\Framework\App\ResponseInterface $response
  40. * @param \Magento\Framework\Filesystem $filesystem
  41. * @param \Magento\Backend\Model\Auth $auth
  42. * @param \Magento\Backend\Model\UrlInterface $backendUrl
  43. * @param \Magento\Backend\Model\Session $session
  44. * @param \Magento\Framework\App\ActionFlag $flag
  45. * @param \Magento\Backend\Helper\Data $helper
  46. */
  47. public function __construct(
  48. \Magento\Framework\App\ResponseInterface $response,
  49. \Magento\Framework\Filesystem $filesystem,
  50. \Magento\Backend\Model\Auth $auth,
  51. \Magento\Backend\Model\UrlInterface $backendUrl,
  52. \Magento\Backend\Model\Session $session,
  53. \Magento\Framework\App\ActionFlag $flag,
  54. \Magento\Backend\Helper\Data $helper
  55. ) {
  56. $this->_auth = $auth;
  57. $this->_backendUrl = $backendUrl;
  58. $this->_session = $session;
  59. $this->_flag = $flag;
  60. $this->_helper = $helper;
  61. parent::__construct($response, $filesystem);
  62. }
  63. /**
  64. * Set redirect into response
  65. *
  66. * @param string $path
  67. * @param array $arguments
  68. * @return \Magento\Framework\App\ResponseInterface
  69. * @TODO move method
  70. */
  71. protected function _redirect($path, $arguments = [])
  72. {
  73. $this->_session->setIsUrlNotice(
  74. $this->_flag->get('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED)
  75. );
  76. $this->_response->setRedirect($this->_helper->getUrl($path, $arguments));
  77. return $this->_response;
  78. }
  79. /**
  80. * Declare headers and content file in response for file download
  81. *
  82. * @param string $fileName
  83. * @param string|array $content set to null to avoid starting output, $contentLength should be set explicitly in
  84. * that case
  85. * @param string $baseDir
  86. * @param string $contentType
  87. * @param int $contentLength explicit content length, if strlen($content) isn't applicable
  88. * @return \Magento\Framework\App\ResponseInterface
  89. */
  90. public function create(
  91. $fileName,
  92. $content,
  93. $baseDir = DirectoryList::ROOT,
  94. $contentType = 'application/octet-stream',
  95. $contentLength = null
  96. ) {
  97. if ($this->_auth->getAuthStorage()->isFirstPageAfterLogin()) {
  98. return $this->_redirect($this->_backendUrl->getStartupPageUrl());
  99. }
  100. return parent::create($fileName, $content, $baseDir, $contentType, $contentLength);
  101. }
  102. }