Skip to content

Commit af46ef3

Browse files
committed
🚧 working on doctrine entities
1 parent 66fa671 commit af46ef3

7 files changed

Lines changed: 281 additions & 36 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@
66
[![Gitmoji](https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg?style=flat-square)](https://gitmoji.carloscuesta.me)
77

88
This is a work in progress, which once complete will allow you to export the Tapestry project state to a database for manipulation by third party tools. In the case of Tapestry this is a precursor to the API plugin for an in browser admin panel.
9+
10+
### Development
11+
12+
To run migrations use:
13+
14+
```
15+
vendor\bin\doctrine.bat orm:schema-tool:update --force
16+
```

src/Entities/ContentType.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Entities;
4+
5+
/**
6+
* @Entity
7+
* @Table(name="content_types")
8+
*/
9+
class ContentType
10+
{
11+
/** @Id @Column(type="integer") @GeneratedValue */
12+
private $id;
13+
14+
/** @ManyToOne(targetEntity="Environment") */
15+
private $environment;
16+
17+
/** @Column(type="string") */
18+
private $name;
19+
20+
/** @Column(type="string") */
21+
private $path;
22+
23+
/** @Column(type="string") */
24+
private $template;
25+
26+
/** @Column(type="string") */
27+
private $permalink;
28+
29+
/** @Column(type="boolean") */
30+
private $enabled;
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getName()
36+
{
37+
return $this->name;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getPath()
44+
{
45+
return $this->path;
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getTemplate()
52+
{
53+
return $this->template;
54+
}
55+
56+
/**
57+
* @return string
58+
*/
59+
public function getPermalink()
60+
{
61+
return $this->permalink;
62+
}
63+
64+
/**
65+
* @return boolean
66+
*/
67+
public function getEnabled()
68+
{
69+
return $this->enabled;
70+
}
71+
72+
/**
73+
* @return Environment[]
74+
*/
75+
public function getEnvironment()
76+
{
77+
return $this->environment;
78+
}
79+
}

src/Entities/Environment.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Entities;
4+
5+
/**
6+
* @Entity
7+
* @Table(name="environments")
8+
*/
9+
class Environment
10+
{
11+
/** @Id @Column(type="integer") @GeneratedValue */
12+
private $id;
13+
14+
/** @Column(type="string") */
15+
private $name;
16+
17+
/** @OneToMany(targetEntity="ContentType", mappedBy="environment_id") */
18+
private $contentTypes;
19+
20+
/** @OneToMany(targetEntity="File", mappedBy="environment_id") */
21+
private $files;
22+
23+
public function getName()
24+
{
25+
return $this->name;
26+
}
27+
28+
public function setName($name)
29+
{
30+
$this->name = $name;
31+
}
32+
33+
public function getContentTypes()
34+
{
35+
return $this->contentTypes;
36+
}
37+
38+
public function addContentType(ContentType $contentType) {
39+
$this->contentTypes[] = $contentType;
40+
}
41+
42+
public function getFiles()
43+
{
44+
return $this->files;
45+
}
46+
47+
public function addFile(File $file) {
48+
$this->files[] = $file;
49+
}
50+
}

src/Entities/File.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database\Entities;
4+
5+
/**
6+
* @Entity
7+
* @Table(name="files")
8+
*/
9+
class File
10+
{
11+
/** @Id @Column(type="integer") @GeneratedValue */
12+
private $id;
13+
14+
/** @ManyToOne(targetEntity="Environment") */
15+
private $environment;
16+
17+
/** @Column(type="string") */
18+
private $uid;
19+
20+
/**
21+
* @return string
22+
*/
23+
public function getUid()
24+
{
25+
return $this->uid;
26+
}
27+
28+
public function setUid($uid)
29+
{
30+
$this->uid = $uid;
31+
}
32+
}

src/Exporter.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace TapestryCloud\Database;
44

5+
use Doctrine\ORM\EntityManager;
6+
use Doctrine\ORM\EntityManagerInterface;
57
use PDO;
8+
use Tapestry\Entities\Collections\FlatCollection;
69
use Tapestry\Entities\Project;
710
use Doctrine\DBAL\Connection;
11+
use Tapestry\Modules\ContentTypes\ContentTypeFactory;
12+
use TapestryCloud\Database\Entities\Environment;
13+
use TapestryCloud\Database\Entities\File;
814

915
class Exporter {
1016

@@ -13,12 +19,18 @@ class Exporter {
1319
*/
1420
private $connection;
1521

22+
/**
23+
* @var EntityManagerInterface|EntityManager
24+
*/
25+
private $entityManager;
26+
1627
/**
1728
* Exporter constructor.
1829
* @param Connection $connection
30+
* @param EntityManagerInterface $entityManager
1931
* @throws \Exception
2032
*/
21-
public function __construct(Connection $connection)
33+
public function __construct(Connection $connection, EntityManagerInterface $entityManager)
2234
{
2335
$this->connection = $connection;
2436
if (!$this->connection->connect()) {
@@ -27,15 +39,44 @@ public function __construct(Connection $connection)
2739

2840
$tables = $this->connection->getSchemaManager()->listTables();
2941

30-
if (count($tables) < 1) {
31-
$migrator = new Migrator($this->connection);
32-
$migrator->migrate();
42+
$migrator = new Migrator($this->connection);
43+
if (count($tables) < $migrator->tables()) {
44+
//$migrator->migrate();
3345
}
3446

35-
$n = 1;
47+
$this->entityManager = $entityManager;
3648
}
3749

3850
public function export(Project $project) {
39-
$n = 1;
51+
/** @var FlatCollection $files */
52+
$files = $project->get('files');
53+
54+
/** @var ContentTypeFactory $contentTypes */
55+
$contentTypes = $project->get('content_types');
56+
57+
/** @var array $cmdOptions */
58+
$cmdOptions = $project->get('cmd_options');
59+
60+
// 1. Create env record if none found for current environment
61+
// $environment = new Environment();
62+
// $environment->name = $cmdOptions['env'];
63+
64+
if (! $environment = $this->entityManager->getRepository(Environment::class)->findOneBy(['name' => $cmdOptions['env']])) {
65+
$environment = new Environment();
66+
$environment->setName($cmdOptions['env']);
67+
$this->entityManager->persist($environment);
68+
$this->entityManager->flush();
69+
}
70+
71+
/** @var \Tapestry\Entities\File $file */
72+
foreach ($files as $file) {
73+
$fileRecord = new File();
74+
$fileRecord->setUid($file->getUid());
75+
$environment->addFile($fileRecord);
76+
$this->entityManager->persist($fileRecord);
77+
}
78+
79+
$this->entityManager->flush();
80+
4081
}
4182
}

src/Migrator.php

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,58 @@ class Migrator
1616
* @var array
1717
*/
1818
private $migrations = [
19+
'CREATE TABLE IF NOT EXISTS environments ('.
20+
'id INTEGER PRIMARY KEY,' .
21+
'name TEXT NOT NULL'.
22+
')',
23+
1924
'CREATE TABLE IF NOT EXISTS content_types ('.
20-
'id INTEGER PRIMARY KEY,' .
21-
'name TEXT NOT NULL,'.
22-
'path TEXT NOT NULL,'.
23-
'template TEXT NOT NULL,'.
24-
'permalink TEXT NOT NULL,'.
25-
'enabled INTEGER NOT NULL DEFAULT 0'.
25+
'id INTEGER PRIMARY KEY,' .
26+
'environment_id INTEGER NOT NULL,'.
27+
'name TEXT NOT NULL,'.
28+
'path TEXT NOT NULL,'.
29+
'template TEXT NOT NULL,'.
30+
'permalink TEXT NOT NULL,'.
31+
'enabled INTEGER NOT NULL DEFAULT 0'.
2632
')',
33+
2734
'CREATE TABLE IF NOT EXISTS files ('.
28-
'id INTEGER PRIMARY KEY,' .
29-
'uid TEXT NOT NULL'.
35+
'id INTEGER PRIMARY KEY,' .
36+
'environment_id INTEGER NOT NULL,'.
37+
'uid TEXT NOT NULL'.
3038
')',
39+
3140
'CREATE TABLE IF NOT EXISTS taxonomies ('.
32-
'id INTEGER PRIMARY KEY,' .
33-
'name TEXT NOT NULL'.
41+
'id INTEGER PRIMARY KEY,' .
42+
'name TEXT NOT NULL'.
3443
')',
44+
3545
'CREATE TABLE IF NOT EXISTS content_type_taxonomies ('.
36-
'id INTEGER PRIMARY KEY,' .
37-
'content_type_id INTEGER NOT NULL,'.
38-
'taxonomy_id INTEGER NOT NULL,'.
39-
'FOREIGN KEY (content_type_id)'.
40-
'REFERENCES content_types(content_type_id) ON UPDATE CASCADE ON DELETE CASCADE,'.
41-
'FOREIGN KEY (taxonomy_id)'.
42-
'REFERENCES taxonomies(taxonomy_id) ON UPDATE CASCADE ON DELETE CASCADE'.
46+
'id INTEGER PRIMARY KEY,' .
47+
'content_type_id INTEGER NOT NULL,'.
48+
'taxonomy_id INTEGER NOT NULL,'.
49+
'FOREIGN KEY (content_type_id)'.
50+
'REFERENCES content_types(content_type_id) ON UPDATE CASCADE ON DELETE CASCADE,'.
51+
'FOREIGN KEY (taxonomy_id)'.
52+
'REFERENCES taxonomies(taxonomy_id) ON UPDATE CASCADE ON DELETE CASCADE'.
4353
')',
54+
4455
'CREATE TABLE IF NOT EXISTS taxonomy_classifications ('.
45-
'id INTEGER PRIMARY KEY,' .
46-
'taxonomy_id INTEGER NOT NULL,'.
47-
'name TEXT NOT NULL,'.
48-
'FOREIGN KEY (taxonomy_id)'.
49-
'REFERENCES taxonomies(taxonomy_id) ON UPDATE CASCADE ON DELETE CASCADE'.
56+
'id INTEGER PRIMARY KEY,' .
57+
'taxonomy_id INTEGER NOT NULL,'.
58+
'name TEXT NOT NULL,'.
59+
'FOREIGN KEY (taxonomy_id)'.
60+
'REFERENCES taxonomies(taxonomy_id) ON UPDATE CASCADE ON DELETE CASCADE'.
5061
')',
62+
5163
'CREATE TABLE IF NOT EXISTS taxonomy_classification_file ('.
52-
'id INTEGER PRIMARY KEY,' .
53-
'taxonomy_classification_id INTEGER NOT NULL,'.
54-
'file_id INTEGER NOT NULL,'.
55-
'FOREIGN KEY (taxonomy_classification_id)'.
56-
'REFERENCES taxonomy_classifications(taxonomy_classification_id) ON UPDATE CASCADE ON DELETE CASCADE,'.
57-
'FOREIGN KEY (file_id)'.
58-
'REFERENCES files(file_id) ON UPDATE CASCADE ON DELETE CASCADE'.
64+
'id INTEGER PRIMARY KEY,' .
65+
'taxonomy_classification_id INTEGER NOT NULL,'.
66+
'file_id INTEGER NOT NULL,'.
67+
'FOREIGN KEY (taxonomy_classification_id)'.
68+
'REFERENCES taxonomy_classifications(taxonomy_classification_id) ON UPDATE CASCADE ON DELETE CASCADE,'.
69+
'FOREIGN KEY (file_id)'.
70+
'REFERENCES files(file_id) ON UPDATE CASCADE ON DELETE CASCADE'.
5971
')',
6072
];
6173

@@ -69,6 +81,11 @@ public function __construct(Connection $connection)
6981
$this->connection = $connection;
7082
}
7183

84+
public function tables()
85+
{
86+
return count($this->migrations);
87+
}
88+
7289
public function migrate()
7390
{
7491
foreach ($this->migrations as $migration) {

0 commit comments

Comments
 (0)