Skip to content

Commit 2516078

Browse files
author
Silvia Bigler
committed
[FEATURE] Add export cli task
1 parent 4474b20 commit 2516078

File tree

4 files changed

+134
-3
lines changed

4 files changed

+134
-3
lines changed

Classes/Command/ExportCommand.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace IchHabRecht\MaskExport\Command;
6+
7+
use IchHabRecht\MaskExport\Controller\ExportController;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use TYPO3\CMS\Core\Core\Bootstrap;
13+
use TYPO3\CMS\Core\Utility\GeneralUtility;
14+
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
15+
use TYPO3\CMS\Extbase\Mvc\Request;
16+
use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
17+
18+
/*
19+
* This file is part of the TYPO3 extension mask_export.
20+
*
21+
* (c) 2016 Nicole Cordes <[email protected]>, CPS-IT GmbH
22+
*
23+
* It is free software; you can redistribute it and/or modify it under
24+
* the terms of the GNU General Public License, either version 2
25+
* of the License, or any later version.
26+
*
27+
* For the full copyright and license information, please read the
28+
* LICENSE file that was distributed with this source code.
29+
*/
30+
31+
/**
32+
* This class provides a CLI command for exporting the mask elements
33+
*/
34+
class ExportCommand extends Command
35+
{
36+
protected function configure(): void
37+
{
38+
$this
39+
->setHelp('Export all mask elements')
40+
->addArgument(
41+
'vendorName',
42+
InputArgument::REQUIRED,
43+
'Vendor name for the new extension'
44+
)
45+
->addArgument(
46+
'extensionName',
47+
InputArgument::REQUIRED,
48+
'Extension name for the new extension'
49+
)
50+
->addArgument(
51+
'path',
52+
InputArgument::OPTIONAL,
53+
'Path for the extension to be written. Defaults to the usual extension path.'
54+
);
55+
}
56+
57+
/**
58+
* @param InputInterface $input
59+
* @param OutputInterface $output
60+
* @return int
61+
*/
62+
protected function execute(InputInterface $input, OutputInterface $output): int
63+
{
64+
['path' => $path, 'extensionName' => $extensionName, 'vendorName' => $vendorName] = $input->getArguments();
65+
GeneralUtility::setIndpEnv('TYPO3_REQUEST_URL', '');
66+
Bootstrap::initializeBackendUser();
67+
68+
$request = GeneralUtility::makeInstance(Request::class);
69+
$request->setControllerName('Export');
70+
$request->setControllerActionName('install');
71+
$request->setArguments([
72+
'vendorName' => $input->getArgument('vendorName'),
73+
'extensionName' => $input->getArgument('extensionName'),
74+
'elements' => [],
75+
]);
76+
if ($path) {
77+
$request->setArgument('path', $path . '/');
78+
}
79+
80+
$controller = GeneralUtility::makeInstance(ExportController::class);
81+
$elements = $controller->getAvailableElements();
82+
$GLOBALS['BE_USER']->uc['mask_export'] = [
83+
'vendorName' => $vendorName,
84+
'extensionName' => $extensionName,
85+
'elements' => implode(',', array_keys($elements)),
86+
];
87+
try {
88+
$controller->processRequest($request);
89+
} catch (StopActionException $e) {
90+
// Ignore this Exception because it is for web requests only
91+
}
92+
$output->writeln(sprintf(
93+
'Extension written to %s%s',
94+
$path ? realpath($path) . '/' : Extension::returnInstallPaths()['Local'],
95+
$input->getArgument('extensionName')
96+
));
97+
98+
return Command::SUCCESS;
99+
}
100+
}

Classes/Controller/ExportController.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,20 @@ public function downloadAction($vendorName, $extensionName, $elements)
178178
* @param string $vendorName
179179
* @param string $extensionName
180180
* @param array $elements
181+
* @param string $path
181182
*/
182-
public function installAction($vendorName, $extensionName, $elements)
183+
public function installAction($vendorName, $extensionName, $elements, $path = null)
183184
{
184185
$paths = Extension::returnInstallPaths();
185186
if (empty($paths['Local']) || !file_exists($paths['Local'])) {
186187
throw new \RuntimeException('Local extension install path is missing', 1500061028);
187188
}
188189

189-
$extensionPath = $paths['Local'] . $extensionName;
190+
if (!$path) {
191+
$path = $paths['Local'];
192+
}
193+
194+
$extensionPath = $path . $extensionName;
190195
$files = $this->getFiles($vendorName, $extensionName, $elements);
191196
$this->writeExtensionFilesToPath($files, $extensionPath);
192197

@@ -290,6 +295,14 @@ protected function getElements()
290295
return $elements;
291296
}
292297

298+
/**
299+
* @return array
300+
*/
301+
public function getAvailableElements()
302+
{
303+
return $this->maskConfiguration['tt_content']['elements'] ?? [];
304+
}
305+
293306
/**
294307
* @param string $vendorName
295308
* @param string $extensionName

Configuration/Services.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: false
6+
7+
IchHabRecht\MaskExport\:
8+
resource: '../Classes/*'
9+
10+
IchHabRecht\MaskExport\Command\ExportCommand:
11+
tags:
12+
- name: console.command
13+
command: 'mask:export'
14+
description: 'Export all mask elements'

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ Simply install mask and mask_export with Composer or the Extension Manager.
4040

4141
## Usage
4242

43+
### Backend
4344
- use the mask wizard to configure own content elements
4445
- change to tab "Code Export"
4546
- if you like change the extension key, the default one is *my_mask_export*
4647
- either install or download your extension
4748

49+
### CLI
50+
./vendor/bin/typo3 mask:export MyVendor my_extension_name [save_path]
51+
4852
## Best practise
4953

5054
It is recommended to **not touch** the generated export extension.
@@ -57,7 +61,7 @@ You can find some common configuration in the [my_maskexport_sitepackage](https:
5761
example site package.
5862

5963
Furthermore you can refer to the slides [CCE (Custom Content Elements) - Best Practice ](https://de.slideshare.net/cpsitgmbh/cce-custom-content-elements-best-practice)
60-
for additional information.
64+
for additional information.
6165

6266
## Community
6367

0 commit comments

Comments
 (0)