Skip to content

Commit

Permalink
Export ContentTypes in separated yaml files
Browse files Browse the repository at this point in the history
Signed-off-by: daverner <daverner@sqli.com>
  • Loading branch information
daverner committed Jun 2, 2020
1 parent e8045d0 commit cdf9682
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 65 deletions.
19 changes: 11 additions & 8 deletions Command/CreateOrUpdateContentTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ class CreateOrUpdateContentTypeCommand extends ContainerAwareCommand
protected function configure()
{
//Prevision du cas du nom d'un fichier si on decide de traiter fichier par fichier ou si on traite tout d'un coup
$this->setName( 'sqli:contentTypesInstaller:create_or_update' )->setDefinition(
array(
new InputArgument(
'filename',
InputArgument::OPTIONAL,
'name of the file which describe the class'
$this
->setName( 'sqli:contentTypesInstaller:create_or_update' )
->setDescription( "Create or update ContentTypes from yaml files" )
->setDefinition(
array(
new InputArgument(
'filename',
InputArgument::OPTIONAL,
'name of the file which describe the class'
)
)
)
);
);
}

protected function execute( InputInterface $input, OutputInterface $output )
Expand Down
47 changes: 34 additions & 13 deletions Command/ExtractContentTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct( ExtractHelper $extractHelper )
{
$this->extractHelper = $extractHelper;

parent::__construct('sqli:contentTypesInstaller:extract');
parent::__construct( 'sqli:contentTypesInstaller:extract' );
}

/**
Expand All @@ -33,13 +33,13 @@ protected function configure()
array(
new InputArgument(
'filename',
InputArgument::REQUIRED,
'output filename'
InputArgument::OPTIONAL,
'Output filename'
),
new InputArgument(
'identifierContentType',
InputArgument::OPTIONAL,
'identifier contentType to Extract'
'Identifier contentType to extract'
)
)
);
Expand All @@ -52,17 +52,38 @@ protected function execute( InputInterface $input, OutputInterface $output )
{
$output->writeln( "Debut de l'extract des content types" );

$outputFilename = $this->getContainer()->getParameter( 'kernel.root_dir' ) . '/../'.
$input->getArgument( 'filename' );
$identifierContentType = $input->getArgument( 'identifierContentType' );
$content = $this->extractHelper->createContentToExport( $identifierContentType, $output );
$aContent = $this->extractHelper->createContentToExport( $identifierContentType, $output );

// Retrieve parameters for filename
$outputPathname = $this->getContainer()->getParameter( 'sqli_ez_toolbox.contenttype_installer.installation_directory' );
$isAbsolutePath = $this->getContainer()->getParameter( 'sqli_ez_toolbox.contenttype_installer.is_absolute_path' );

if( !$isAbsolutePath )
{
// If not an absolute path, generate path from Symfony root dir
$outputPathname = $this->getContainer()->getParameter( 'kernel.root_dir' ) . '/../' . $outputPathname . '/';
}

if( $input->getArgument( 'filename' ) )
{
// If a filename specified in argument, write in this file
$outputFilename = $outputPathname . $input->getArgument( 'filename' );
// Clear file before first write
file_put_contents( $outputFilename, "" );
}

foreach( $aContent as $sIdentifierContentType => $content )
{
if( !$input->getArgument( 'filename' ) )
{
// No filename specified so write in separated file
$outputFilename = $outputPathname . $sIdentifierContentType . ".yml";
file_put_contents( $outputFilename, "" );
}

//Ecriture du content type dans un fichier
//TODO : Voir pour paramaettrer le nom et chemin du fichier de sortie
//ouverture du fichier en mode écriture, création du fichier s'il n'existe pas.
$fp = fopen( $outputFilename, "w" );
fwrite( $fp, $content );
fclose( $fp );
file_put_contents( $outputFilename, $content, FILE_APPEND );
}

$output->writeln( "Fin de l'extract des content types" );
}
Expand Down
31 changes: 22 additions & 9 deletions Controller/ContentTypeInstallerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use EzSystems\EzPlatformAdminUiBundle\Controller\Controller as BaseController;
use SQLI\EzToolboxBundle\Services\ExtractHelper;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

class ContentTypeInstallerController extends BaseController
{
Expand All @@ -37,17 +38,29 @@ public function listAction( ContentTypeService $contentTypeService, LocaleConver

public function exportAction( ExtractHelper $extractHelper )
{
$postVariables = $_POST;
$aExportedIds = $postVariables['ExportIDArray'];
$content = $extractHelper->createContentToExport( $aExportedIds );
$postVariables = $_POST;
$aExportedIds = $postVariables['ExportIDArray'];
$aExportedContentTypes = $extractHelper->createContentToExport( $aExportedIds );

header( 'Content-Type: force-download' );
header( 'Content-Disposition: attachment; filename="contentType.yml"' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
$sResponseContent = "";
foreach( $aExportedContentTypes as $sExportedContentType )
{
$sResponseContent .= $sExportedContentType;
}

$headers =
[
'Content-Type' => 'force-download',
'Content-Disposition' => 'attachment; filename="contentType.yml"',
'Pragma' => 'no-cache',
'Expires' => '0',
];
$response = new Response();
$response->headers->add( $headers );
$response->setContent( $sResponseContent );
$response->send();

echo $content;
exit( 0 );
return $this->redirectToRoute( "sqli_eztoolbox_contenttype_installer_list" );
}

public function indexAction( $name )
Expand Down
68 changes: 33 additions & 35 deletions Services/ExtractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public function __construct( Repository $repository, ?TagsService $tagService )
/**
* @param $aExportedIdentifiers
* @param OutputInterface $output
* @return string
* @return array
* @throws NotFoundException
*/
public function createContentToExport( $aExportedIdentifiers, OutputInterface $output = null )
{
$content = [];
$aContentType = [];

$this->repository->setCurrentUser( $this->repository->getUserService()->loadUser( 14 ) );
Expand All @@ -62,7 +63,7 @@ public function createContentToExport( $aExportedIdentifiers, OutputInterface $o
}
if( isset( $aExportedIdentifiers ) )
{
foreach( $aExportedIdentifiers as $key => $exportedIdentifier )
foreach( $aExportedIdentifiers as $exportedIdentifier )
{
if( is_numeric( $exportedIdentifier ) )
{
Expand All @@ -86,19 +87,19 @@ public function createContentToExport( $aExportedIdentifiers, OutputInterface $o
$isContainer = $contentType->isContainer;
unset( $contentType );

$aContentType[$key]['group_identifier'] = $groupIdentifier;
$aContentType[$key]['identifier'] = $identifier;
$aContentType[$key]['names'] = $names;
$aContentType[$key]['descriptions'] = $descriptions;
$aContentType[$key]['mainLanguageCode'] = $mainLanguageCode;
$aContentType[$key]['nameSchema'] = $nameSchema;
$aContentType[$key]['fieldDefinitions'] = $fieldDefinitions;
$aContentType[$key]['isContainer'] = $isContainer;
$aContentType['group_identifier'] = $groupIdentifier;
$aContentType['identifier'] = $identifier;
$aContentType['names'] = $names;
$aContentType['descriptions'] = $descriptions;
$aContentType['mainLanguageCode'] = $mainLanguageCode;
$aContentType['nameSchema'] = $nameSchema;
$aContentType['fieldDefinitions'] = $fieldDefinitions;
$aContentType['isContainer'] = $isContainer;

$content[$identifier] = $this->createYMLFile( $aContentType, $output );
}
}

$content = $this->createYMLFile( $aContentType, $output );

return $content;
}

Expand All @@ -108,32 +109,29 @@ public function createContentToExport( $aExportedIdentifiers, OutputInterface $o
* @return string
* @throws Exception
*/
public function createYMLFile( $aContent, OutputInterface $output = null )
public function createYMLFile( $contentType, OutputInterface $output = null )
{
$content = "";
foreach( $aContent as $contentType )
{
//TODO : Mettre les champs à exporter dans un fichier de conf ?
$content .= $this->extractInfosForYML( $contentType['identifier'], '', '', true );
$content .= $this->extractInfosForYML( 'group_identifier', $contentType['group_identifier'], ' ' );
$content .= $this->extractInfosForYML( 'identifier', $contentType['identifier'], ' ' );
$content .= $this->extractInfosForYML( 'names', $contentType['names'], ' ' );
$content .= $this->extractInfosForYML( 'descriptions', $contentType['descriptions'], ' ' );
$content .= $this->extractInfosForYML( 'mainLanguageCode', $contentType['mainLanguageCode'], ' ' );
$content .= $this->extractInfosForYML( 'nameSchema', $contentType['nameSchema'], ' ' );
$content .= $this->extractInfosForYML( 'isContainer', $contentType['isContainer'], ' ' );
$content .= "\r\n";
$content .= " ";
$content .= "datatypes:";
$content .= $this->extractDataTypesFromArrayToYML( $contentType['fieldDefinitions'], $output );
//Ajout de ce saut de ligne pour autre contentTypes
$content .= "\r\n";
//TODO : Mettre les champs à exporter dans un fichier de conf ?
$content .= $this->extractInfosForYML( $contentType['identifier'], '', '', true );
$content .= $this->extractInfosForYML( 'group_identifier', $contentType['group_identifier'], ' ' );
$content .= $this->extractInfosForYML( 'identifier', $contentType['identifier'], ' ' );
$content .= $this->extractInfosForYML( 'names', $contentType['names'], ' ' );
$content .= $this->extractInfosForYML( 'descriptions', $contentType['descriptions'], ' ' );
$content .= $this->extractInfosForYML( 'mainLanguageCode', $contentType['mainLanguageCode'], ' ' );
$content .= $this->extractInfosForYML( 'nameSchema', $contentType['nameSchema'], ' ' );
$content .= $this->extractInfosForYML( 'isContainer', $contentType['isContainer'], ' ' );
$content .= "\r\n";
$content .= " ";
$content .= "datatypes:";
$content .= $this->extractDataTypesFromArrayToYML( $contentType['fieldDefinitions'], $output );
//Ajout de ce saut de ligne pour autre contentTypes
$content .= "\r\n";

$contentTypeIdentifierForLog = $contentType['identifier'];
if( !is_null( $output ) )
{
$output->writeln( "Extract du contentType d'identifier $contentTypeIdentifierForLog" );
}
$contentTypeIdentifierForLog = $contentType['identifier'];
if( !is_null( $output ) )
{
$output->writeln( "Extract du contentType d'identifier $contentTypeIdentifierForLog en ". $contentType['mainLanguageCode'] );
}

return $content;
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
{
"name": "ROUX Yoann",
"email": "yroux@sqli.com"
},
{
"name": "OBIGAND Nicolas",
"email": "nobigand@sqli.com"
}
],
"license": [
Expand Down

0 comments on commit cdf9682

Please sign in to comment.