Skip to content

Commit

Permalink
Merge pull request #14 from DivanteLtd/feature/custom-layout-installer
Browse files Browse the repository at this point in the history
Feature/custom layout installer
  • Loading branch information
kubaplas authored Feb 5, 2019
2 parents be03be5 + 3c9742f commit 0d1a9ed
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/PimcoreDevkitBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ services:
PimcoreDevkitBundle\Service\ThumbnailConfigService: ~
PimcoreDevkitBundle\Service\MigrationService: ~
PimcoreDevkitBundle\Service\DocumentService: ~
PimcoreDebkitBundle\Service\CustomLayoutService:
arguments: ['@serializer']
74 changes: 74 additions & 0 deletions src/PimcoreDevkitBundle/Service/CustomLayoutService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* @date      05/02/2019
* @author Michał Bolka <michal.bolka@gmail.com>
* @copyright Copyright (c) 2019 Divante (http://divante.pl)
*/

namespace PimcoreDevkitBundle\Service;

use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
use Pimcore\Model\DataObject\ClassDefinition\Service;
use Symfony\Component\Serializer\Serializer;

/**
* Class CustomLayoutsService
* @package PimcoreDevkitBundle\Service
*/
class CustomLayoutsService
{
/**
* @var Serializer
*/
private $serializer;

/**
* CustomLayoutsService constructor.
* @param Serializer $serializer
*/
public function __construct(Serializer $serializer)
{
$this->serializer = $serializer;
}

/**
* @param string $name
* @param int $classId
* @param string $filePath
* @throws \Exception
*/
public function importLayout(string $name, int $classId, string $filePath)
{
$json = file_get_contents($filePath);
$importData = $this->decodeJson($json);
$customLayout = new CustomLayout();
$id = $this->suggestId($classId);
$customLayout->setClassId($classId);
$customLayout->setName($name);
$customLayout->setId($id);
$layout = Service::generateLayoutTreeFromArray($importData['layoutDefinitions'], true);
$customLayout->setLayoutDefinitions($layout);
$customLayout->setDescription($importData['description']);
$customLayout->save();
}

/**
* @param string $json
* @return mixed
*/
public function decodeJson(string $json)
{
$context['json_decode_associative'] = true;
return $this->serializer->decode($json, 'json', $context);
}

/**
* @param $classId
* @return int|null
*/
public function suggestId($classId)
{
return CustomLayout::getIdentifier($classId);
}
}

0 comments on commit 0d1a9ed

Please sign in to comment.