Skip to content

Commit ac50d6a

Browse files
committed
🚧 finished adding front matter entities
1 parent b65325a commit ac50d6a

5 files changed

Lines changed: 86 additions & 38 deletions

File tree

src/Entities/File.php

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Doctrine\Common\Collections\ArrayCollection;
66
use Doctrine\Common\Collections\Collection;
7-
use Tapestry\Modules\Content\FrontMatter as TapestryFrontMatter;
87

98
/**
109
* @Entity
@@ -84,7 +83,7 @@ class File
8483

8584
/**
8685
* @var Collection|FrontMatter[]
87-
* @OneToMany(targetEntity="FrontMatter", mappedBy="file_id")
86+
* @OneToMany(targetEntity="FrontMatter", mappedBy="file")
8887
*/
8988
private $frontMatter;
9089

@@ -97,37 +96,6 @@ public function __construct()
9796
$this->frontMatter = new ArrayCollection();
9897
}
9998

100-
/**
101-
* File Hydration.
102-
*
103-
* @param \Tapestry\Entities\File $file
104-
* @param Environment|null $environment
105-
*/
106-
public function hydrate(\Tapestry\Entities\File $file, Environment $environment = null)
107-
{
108-
$this->setUid($file->getUid());
109-
$this->setLastModified($file->getLastModified());
110-
$this->setFilename($file->getFilename());
111-
$this->setExt($file->getExt());
112-
$this->setPath($file->getPath());
113-
$this->setToCopy($file->isToCopy());
114-
115-
if (! $file->isToCopy()) {
116-
$frontMatter = new TapestryFrontMatter($file->getFileContent());
117-
$this->setContent($frontMatter->getContent());
118-
foreach($frontMatter->getData() as $key => $value) {
119-
$fmRecord = new FrontMatter();
120-
$fmRecord->setName($key);
121-
$fmRecord->setValue($value);
122-
$this->addFrontMatter($fmRecord);
123-
}
124-
}
125-
126-
if (!is_null($environment)) {
127-
$this->setEnvironment($environment);
128-
}
129-
}
130-
13199
//
132100
// Relationships
133101
//
@@ -154,6 +122,7 @@ public function addFrontMatter(FrontMatter $frontMatter) {
154122
}
155123

156124
$this->frontMatter->add($frontMatter);
125+
$frontMatter->setFile($this);
157126
}
158127

159128
/**

src/Exporter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use TapestryCloud\Database\Entities\File;
1515
use TapestryCloud\Database\Synchronizes\ContentTypes;
1616
use TapestryCloud\Database\Synchronizes\Files;
17+
use TapestryCloud\Database\Hydrators\File as FileHydrator;
1718

1819
class Exporter {
1920

@@ -71,7 +72,7 @@ public function export(Project $project) {
7172
$this->entityManager->flush();
7273
}
7374

74-
$fileSync = new Files($this->entityManager);
75+
$fileSync = new Files($this->entityManager, new FileHydrator($this->entityManager));
7576
$fileSync->sync($files, $environment);
7677

7778
$contentTypeSync = new ContentTypes($this->entityManager);

src/Hydrators/File.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Hydrators;
4+
5+
use Tapestry\Modules\Content\FrontMatter as TapestryFrontMatter;
6+
use TapestryCloud\Database\Entities\Environment;
7+
use TapestryCloud\Database\Entities\FrontMatter;
8+
use TapestryCloud\Database\Entities\File as Model;
9+
10+
11+
class File extends Hydrator
12+
{
13+
14+
/**
15+
* File Hydration.
16+
*
17+
* @param Model $model
18+
* @param \Tapestry\Entities\File $file
19+
* @param Environment|null $environment
20+
*/
21+
public function hydrate(Model $model, \Tapestry\Entities\File $file, Environment $environment = null)
22+
{
23+
$model->setUid($file->getUid());
24+
$model->setLastModified($file->getLastModified());
25+
$model->setFilename($file->getFilename());
26+
$model->setExt($file->getExt());
27+
$model->setPath($file->getPath());
28+
$model->setToCopy($file->isToCopy());
29+
30+
if (! $file->isToCopy()) {
31+
$frontMatter = new TapestryFrontMatter($file->getFileContent());
32+
$model->setContent($frontMatter->getContent());
33+
foreach($frontMatter->getData() as $key => $value) {
34+
$fmRecord = new FrontMatter();
35+
$fmRecord->setName($key);
36+
$fmRecord->setValue(json_encode($value));
37+
$model->addFrontMatter($fmRecord);
38+
39+
$this->entityManager->persist($fmRecord);
40+
}
41+
}
42+
43+
if (!is_null($environment)) {
44+
$model->setEnvironment($environment);
45+
}
46+
}
47+
48+
}

src/Hydrators/Hydrator.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Hydrators;
4+
5+
use Doctrine\ORM\EntityManager;
6+
use Doctrine\ORM\EntityManagerInterface;
7+
8+
class Hydrator
9+
{
10+
/**
11+
* @var EntityManagerInterface|EntityManager
12+
*/
13+
protected $entityManager;
14+
15+
16+
/**
17+
* File constructor.
18+
* @param EntityManagerInterface $entityManager
19+
*/
20+
public function __construct(EntityManagerInterface $entityManager)
21+
{
22+
$this->entityManager = $entityManager;
23+
}
24+
}

src/Synchronizes/Files.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Tapestry\Entities\File as TapestryFile;
88
use TapestryCloud\Database\Entities\Environment;
99
use TapestryCloud\Database\Entities\File;
10+
use TapestryCloud\Database\Hydrators\File as FileHydrator;
1011

1112
class Files
1213
{
@@ -15,13 +16,20 @@ class Files
1516
*/
1617
private $em;
1718

19+
/**
20+
* @var FileHydrator
21+
*/
22+
private $fileHydrator;
23+
1824
/**
1925
* ContentTypes constructor.
2026
* @param EntityManagerInterface $em
27+
* @param FileHydrator $fileHydrator
2128
*/
22-
public function __construct(EntityManagerInterface $em)
29+
public function __construct(EntityManagerInterface $em, FileHydrator $fileHydrator)
2330
{
2431
$this->em = $em;
32+
$this->fileHydrator = $fileHydrator;
2533
}
2634

2735
public function sync(FlatCollection $files, Environment $environment) {
@@ -30,12 +38,10 @@ public function sync(FlatCollection $files, Environment $environment) {
3038
foreach ($files as $file) {
3139

3240
if (!$record = $this->em->getRepository(File::class)->findOneBy(['uid' => $file->getuid(), 'environment' => $environment->getId()])) {
33-
// INSERT
3441
$record = new File();
35-
$record->hydrate($file, $environment);
3642
}
3743

38-
// UPDATE
44+
$this->fileHydrator->hydrate($record, $file, $environment);
3945
$this->em->persist($record);
4046
}
4147

0 commit comments

Comments
 (0)