Skip to content

Cleanup ObjectManager usage - Magento_Translation #27215

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 1 commit
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
51 changes: 27 additions & 24 deletions app/code/Magento/Translation/Model/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Translation\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Translation\Model\Inline\File as TranslationFile;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\View\Asset\Repository;

/**
* A service for handling Translation config files
Expand All @@ -20,41 +22,33 @@ class FileManager
const TRANSLATION_CONFIG_FILE_NAME = 'Magento_Translation/js/i18n-config.js';

/**
* @var \Magento\Framework\View\Asset\Repository
* @var Repository
*/
private $assetRepo;

/**
* @var \Magento\Framework\App\Filesystem\DirectoryList
* @var DirectoryList
*/
private $directoryList;

/**
* @var \Magento\Framework\Filesystem\Driver\File
* @var File
*/
private $driverFile;

/**
* @var TranslationFile
*/
private $translationFile;

/**
* @param \Magento\Framework\View\Asset\Repository $assetRepo
* @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList
* @param \Magento\Framework\Filesystem\Driver\File $driverFile
* @param TranslationFile $translationFile
* @param Repository $assetRepo
* @param DirectoryList $directoryList
* @param File $driverFile
*/
public function __construct(
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Framework\App\Filesystem\DirectoryList $directoryList,
\Magento\Framework\Filesystem\Driver\File $driverFile,
\Magento\Translation\Model\Inline\File $translationFile = null
Repository $assetRepo,
DirectoryList $directoryList,
File $driverFile
) {
$this->assetRepo = $assetRepo;
$this->directoryList = $directoryList;
$this->driverFile = $driverFile;
$this->translationFile = $translationFile ?: ObjectManager::getInstance()->get(TranslationFile::class);
}

/**
Expand All @@ -71,7 +65,7 @@ public function createTranslateConfigAsset()
}

/**
* gets current js-translation.json timestamp
* Gets current js-translation.json timestamp
*
* @return string|void
*/
Expand All @@ -87,18 +81,22 @@ public function getTranslationFileTimestamp()
}

/**
* Retrieve full path for translation file
*
* @return string
*/
protected function getTranslationFileFullPath()
{
return $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
\DIRECTORY_SEPARATOR .
$this->assetRepo->getStaticViewFileContext()->getPath() .
\DIRECTORY_SEPARATOR .
Js\Config::DICTIONARY_FILE_NAME;
\DIRECTORY_SEPARATOR .
$this->assetRepo->getStaticViewFileContext()->getPath() .
\DIRECTORY_SEPARATOR .
Js\Config::DICTIONARY_FILE_NAME;
}

/**
* Retrieve path for translation file
*
* @return string
*/
public function getTranslationFilePath()
Expand All @@ -107,17 +105,22 @@ public function getTranslationFilePath()
}

/**
* Update translation file with content
*
* @param string $content
*
* @return void
*/
public function updateTranslationFileContent($content)
{
$translationDir = $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
\DIRECTORY_SEPARATOR .
$this->assetRepo->getStaticViewFileContext()->getPath();

if (!$this->driverFile->isExists($this->getTranslationFileFullPath())) {
$this->driverFile->createDirectory($translationDir);
}

$this->driverFile->filePutContents($this->getTranslationFileFullPath(), $content);
}

Expand Down
Loading