Skip to content

Cleanup ObjectManager usage - Magento_Sitemap #27384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,76 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sitemap\Block\Adminhtml\Grid\Renderer;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\Block\Context;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Framework\Filesystem;
use Magento\Sitemap\Model\Sitemap;
use Magento\Sitemap\Model\SitemapFactory;

/**
* Sitemap grid link column renderer
*/
class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
class Link extends AbstractRenderer
{
/**
* @var \Magento\Framework\Filesystem $filesystem
* @var Filesystem
*/
protected $_filesystem;
private $filesystem;

/**
* @var \Magento\Sitemap\Model\SitemapFactory
* @var SitemapFactory
*/
protected $_sitemapFactory;
private $sitemapFactory;

/**
* @var DocumentRoot
*/
protected $documentRoot;
private $documentRoot;

/**
* @param \Magento\Backend\Block\Context $context
* @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
* @param \Magento\Framework\Filesystem $filesystem
* @param array $data
* @param Context $context
* @param SitemapFactory $sitemapFactory
* @param Filesystem $filesystem
* @param DocumentRoot $documentRoot
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Context $context,
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory,
\Magento\Framework\Filesystem $filesystem,
array $data = [],
DocumentRoot $documentRoot = null
Context $context,
SitemapFactory $sitemapFactory,
Filesystem $filesystem,
DocumentRoot $documentRoot,
array $data = []
) {
$this->_sitemapFactory = $sitemapFactory;
$this->_filesystem = $filesystem;
$this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
$this->sitemapFactory = $sitemapFactory;
$this->filesystem = $filesystem;
$this->documentRoot = $documentRoot;

parent::__construct($context, $data);
}

/**
* Prepare link to display in grid
*
* @param \Magento\Framework\DataObject $row
* @param DataObject $row
*
* @return string
*/
public function render(\Magento\Framework\DataObject $row)
public function render(DataObject $row)
{
/** @var $sitemap \Magento\Sitemap\Model\Sitemap */
$sitemap = $this->_sitemapFactory->create();
/** @var $sitemap Sitemap */
$sitemap = $this->sitemapFactory->create();
$sitemap->setStoreId($row->getStoreId());
$url = $this->escapeHtml($sitemap->getSitemapUrl($row->getSitemapPath(), $row->getSitemapFilename()));
$url = $this->_escaper->escapeHtml($sitemap->getSitemapUrl($row->getSitemapPath(), $row->getSitemapFilename()));

$fileName = preg_replace('/^\//', '', $row->getSitemapPath() . $row->getSitemapFilename());
$documentRootPath = $this->documentRoot->getPath();
$directory = $this->_filesystem->getDirectoryRead($documentRootPath);
$directory = $this->filesystem->getDirectoryRead($documentRootPath);
if ($directory->isFile($fileName)) {
return sprintf('<a href="%1$s">%1$s</a>', $url);
}
Expand Down
51 changes: 20 additions & 31 deletions app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,45 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;

use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Filesystem;
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
use Magento\Sitemap\Model\SitemapFactory;

/**
* Controller class Delete. Represents adminhtml request flow for a sitemap deletion
*/
class Delete extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements HttpPostActionInterface
class Delete extends Sitemap implements HttpPostActionInterface
{
/**
* @var \Magento\Framework\Filesystem
* @var SitemapFactory
*/
private $filesystem;
private $sitemapFactory;

/**
* @var \Magento\Sitemap\Model\SitemapFactory
* @var Filesystem
*/
private $sitemapFactory;
private $filesystem;

/**
* Constructor
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Sitemap\Model\SitemapFactory|null $sitemapFactory
* @param Context $context
* @param SitemapFactory $sitemapFactory
* @param Filesystem $filesystem
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory = null
Context $context,
SitemapFactory $sitemapFactory,
Filesystem $filesystem
) {
parent::__construct($context);
$this->sitemapFactory = $sitemapFactory ?: ObjectManager::getInstance()
->get(\Magento\Sitemap\Model\SitemapFactory::class);
$this->sitemapFactory = $sitemapFactory;
$this->filesystem = $filesystem;
}

/**
Expand All @@ -46,7 +51,7 @@ public function __construct(
*/
public function execute()
{
$directory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::ROOT);
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
// check if we know what should be deleted
$id = $this->getRequest()->getParam('sitemap_id');
if ($id) {
Expand Down Expand Up @@ -86,20 +91,4 @@ public function execute()
// go to grid
$this->_redirect('adminhtml/*/');
}

/**
* The getter function to get Filesystem object for real application code
*
* @return \Magento\Framework\Filesystem
* @deprecated 100.2.0
*/
private function getFilesystem()
{
if (null === $this->filesystem) {
$this->filesystem = \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Filesystem::class
);
}
return $this->filesystem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;

use Magento\Backend\App\Action;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Area;
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
use Magento\Store\Model\App\Emulation;
use Magento\Framework\App\ObjectManager;

/**
* Generate sitemap file
*/
class Generate extends Sitemap implements HttpGetActionInterface
{
/** @var \Magento\Store\Model\App\Emulation $appEmulation */
/**
* @var Emulation
*/
private $appEmulation;

/**
* Generate constructor.
* @param Action\Context $context
* @param \Magento\Store\Model\App\Emulation|null $appEmulation
* @param Emulation $appEmulation
*/
public function __construct(
Action\Context $context,
Emulation $appEmulation = null
Emulation $appEmulation
) {
parent::__construct($context);
$this->appEmulation = $appEmulation ?: ObjectManager::getInstance()
->get(\Magento\Store\Model\App\Emulation::class);
$this->appEmulation = $appEmulation;
}

/**
Expand All @@ -51,7 +52,7 @@ public function execute()
try {
$this->appEmulation->startEnvironmentEmulation(
$sitemap->getStoreId(),
\Magento\Framework\App\Area::AREA_FRONTEND,
Area::AREA_FRONTEND,
true
);
$sitemap->generateXml();
Expand Down
50 changes: 30 additions & 20 deletions app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;

use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Validator\StringLength;
use Magento\MediaStorage\Model\File\Validator\AvailablePath;
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
use Magento\Sitemap\Helper\Data;
use Magento\Sitemap\Model\SitemapFactory;

/**
* Save sitemap controller.
*/
class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements HttpPostActionInterface
class Save extends Sitemap implements HttpPostActionInterface
{
/**
* Maximum length of sitemap filename
Expand All @@ -34,12 +41,12 @@ class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements Http
private $pathValidator;

/**
* @var \Magento\Sitemap\Helper\Data
* @var Data
*/
private $sitemapHelper;

/**
* @var \Magento\Framework\Filesystem
* @var Filesystem
*/
private $filesystem;

Expand All @@ -53,24 +60,24 @@ class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements Http
* @param Context $context
* @param StringLength $stringValidator
* @param AvailablePath $pathValidator
* @param \Magento\Sitemap\Helper\Data $sitemapHelper
* @param \Magento\Framework\Filesystem $filesystem
* @param Data $sitemapHelper
* @param Filesystem $filesystem
* @param SitemapFactory $sitemapFactory
*/
public function __construct(
Context $context,
StringLength $stringValidator = null,
AvailablePath $pathValidator = null,
\Magento\Sitemap\Helper\Data $sitemapHelper = null,
\Magento\Framework\Filesystem $filesystem = null,
SitemapFactory $sitemapFactory = null
StringLength $stringValidator,
AvailablePath $pathValidator,
Data $sitemapHelper,
Filesystem $filesystem,
SitemapFactory $sitemapFactory
) {
parent::__construct($context);
$this->stringValidator = $stringValidator ?: $this->_objectManager->get(StringLength::class);
$this->pathValidator = $pathValidator ?: $this->_objectManager->get(AvailablePath::class);
$this->sitemapHelper = $sitemapHelper ?: $this->_objectManager->get(\Magento\Sitemap\Helper\Data::class);
$this->filesystem = $filesystem ?: $this->_objectManager->get(\Magento\Framework\Filesystem::class);
$this->sitemapFactory = $sitemapFactory ?: $this->_objectManager->get(SitemapFactory::class);
$this->stringValidator = $stringValidator;
$this->pathValidator = $pathValidator;
$this->sitemapHelper = $sitemapHelper;
$this->filesystem = $filesystem;
$this->sitemapFactory = $sitemapFactory;
}

/**
Expand Down Expand Up @@ -115,11 +122,12 @@ protected function validatePath(array $data)
* Clear sitemap
*
* @param \Magento\Sitemap\Model\Sitemap $model
*
* @return void
*/
protected function clearSiteMap(\Magento\Sitemap\Model\Sitemap $model)
{
/** @var \Magento\Framework\Filesystem $directory */
/** @var Filesystem $directory */
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);

if ($this->getRequest()->getParam('sitemap_id')) {
Expand Down Expand Up @@ -169,12 +177,13 @@ protected function saveData($data)
* Get result after saving data
*
* @param string|bool $id
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*/
protected function getResult($id)
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT);

if ($id) {
// check if 'Save and Continue'
if ($this->getRequest()->getParam('back')) {
Expand All @@ -194,19 +203,20 @@ protected function getResult($id)
'adminhtml/*/edit',
['sitemap_id' => $this->getRequest()->getParam('sitemap_id')]
);

return $resultRedirect;
}

/**
* Save action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @return Redirect
*/
public function execute()
{
// check if data sent
$data = $this->getRequest()->getPostValue();
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT);
if ($data) {
if (!$this->validatePath($data)) {
Expand Down
Loading