Commit 5b7c217e authored by Thomas Löffler's avatar Thomas Löffler

Use service to check and update static JSON file

parent 309bfaf7
Pipeline #10533 failed with stages
in 4 minutes and 26 seconds
<?php
namespace T3o\TerFe2\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use T3o\TerFe2\Service\ValidComposerNameFileService;
class UpdateValidComposerNameFileCommand extends Command
{
protected function configure()
{
$this->setDescription('Checks if JSON file needs to be updated');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
if (ValidComposerNameFileService::issetRegistryFlag()) {
ValidComposerNameFileService::updateFile();
}
}
}
......@@ -14,19 +14,18 @@ namespace T3o\TerFe2\Controller\Eid;
* The TYPO3 project - inspiring people to share!
*/
use T3o\TerFe2\Service\ValidComposerNameFileService;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class ExtensionController
*
* @package T3o\TerFe2\Controller\Eid
* @deprecated Use ValidComposerNameFileService::output()
*/
class ExtensionController
{
/**
* @var array
*/
protected $jsonArray = [
'meta' => null,
'data' => null
];
/**
* @param $action
*/
......@@ -41,49 +40,9 @@ class ExtensionController
}
}
/**
*/
protected function findAllWithValidComposerName()
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_terfe2_domain_model_extension');
$expr = $queryBuilder->expr();
$result = $queryBuilder->select(
'ext_key',
'composer_name'
)
->from('tx_terfe2_domain_model_extension')
->where(
$expr->neq(
'composer_name',
$queryBuilder->createNamedParameter('')
)
)
->execute();
while ($extension = $result->fetch()) {
$this->jsonArray['data'][$extension['ext_key']] = [
'composer_name' => $extension['composer_name'],
];
}
$json = json_encode($this->jsonArray, true);
if (JSON_ERROR_NONE !== ($jsonErrorCode = json_last_error())) {
$this->jsonArray['meta'] = [
'error' => [
'type' => 'json encoding error',
'code' => $jsonErrorCode
]
];
$this->jsonArray['data'] = null;
$json = json_encode($this->jsonArray);
}
header('Content-Type: application/json');
echo $json;
exit();
ValidComposerNameFileService::output();
}
}
......
......@@ -19,11 +19,13 @@ use T3o\Ter\Api\ExtensionKey;
use T3o\Ter\Api\ExtensionVersion;
use T3o\TerFe2\Domain\Model\Extension;
use T3o\TerFe2\Provider\FileProvider;
use T3o\TerFe2\Service\ValidComposerNameFileService;
use T3o\TerFe2\Utility\ExtensionUtility;
use T3o\TerFe2\Utility\VersionUtility;
use T3o\TerFe2\Validation\Validator\ComposerNameValidator;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
/**
* Controller for the extension object
......@@ -130,7 +132,7 @@ class ExtensionController extends \T3o\TerFe2\Controller\AbstractController
*
* @param \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager $persistenceManager
*/
public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager $persistenceManager)
public function injectPersistenceManager(PersistenceManager $persistenceManager)
{
$this->persistenceManager = $persistenceManager;
}
......@@ -312,7 +314,13 @@ class ExtensionController extends \T3o\TerFe2\Controller\AbstractController
$this->addFlashMessage('Tag "' . htmlspecialchars($tag) . '" added to extension');
}
}
$this->extensionRepository->update($extension);
if ($extension->_isDirty('composerName')) {
ValidComposerNameFileService::setRegistryFlag(1);
}
if (!empty($save)) {
$this->redirectWithMessage(
$this->translate('msg.extension_updated'),
......
<?php
namespace T3o\TerFe2\Service;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Registry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class ValidComposerNameFileService
{
protected static $fileName = 'currentvalidcomposernames.json';
public static function output(): void
{
$fileName = self::getFullPathOfFile();
if (!file_exists($fileName) || self::issetRegistryFlag()) {
self::updateFile();
}
header('Content-Type: application/json');
echo file_get_contents($fileName);
exit();
}
public static function updateFile(): void
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_terfe2_domain_model_extension');
$expr = $queryBuilder->expr();
$result = $queryBuilder
->select(
'ext_key',
'composer_name'
)
->from('tx_terfe2_domain_model_extension')
->where(
$expr->neq(
'composer_name',
$queryBuilder->createNamedParameter('')
)
)
->execute();
$extensionArray = [
'meta' => null,
'data' => null,
];
while ($extension = $result->fetch()) {
$extensionArray['data'][$extension['ext_key']] = [
'composer_name' => $extension['composer_name'],
];
}
if (JSON_ERROR_NONE !== ($jsonErrorCode = json_last_error())) {
$extensionArray['meta'] = [
'error' => [
'type' => 'json encoding error',
'code' => $jsonErrorCode
]
];
$extensionArray['data'] = null;
}
file_put_contents(
self::getFullPathOfFile(),
json_encode($extensionArray)
);
self::setRegistryFlag(0);
}
public static function setRegistryFlag(int $flag): void
{
$registry = GeneralUtility::makeInstance(Registry::class);
$registry->set('ter_fe2', 'validComposerNameUpdate', $flag);
}
public static function issetRegistryFlag(): bool
{
$registry = GeneralUtility::makeInstance(Registry::class);
return (int)$registry->get('ter_fe2', 'validComposerNameUpdate') === 1;
}
protected static function getFullPathOfFile(): string
{
return Environment::getPublicPath() . '/' . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'] . '/' . self::$fileName;
}
}
......@@ -12,6 +12,7 @@ namespace T3o\TerRest\Repository;
* of the License, or any later version.
*/
use T3o\TerFe2\Service\ValidComposerNameFileService;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
/**
......@@ -43,6 +44,10 @@ class ExtensionRepository extends AbstractRepository
$queryBuilder->set($name, $value);
}
if (isset($fields['composer_name'])) {
ValidComposerNameFileService::setRegistryFlag(1);
}
return (int)$this->byUniqueKey($queryBuilder->update($this->tableName), $key)->execute();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment