Skip to content

Commit 80f075e

Browse files
committed
🚧 boilerplating functionality
1 parent 4efda55 commit 80f075e

6 files changed

Lines changed: 142 additions & 5 deletions

File tree

src/Exporter.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,39 @@
22

33
namespace TapestryCloud\Database;
44

5+
use PDO;
56
use Tapestry\Entities\Project;
7+
use Doctrine\DBAL\Connection;
68

79
class Exporter {
10+
11+
/**
12+
* @var Connection
13+
*/
14+
private $connection;
15+
16+
/**
17+
* Exporter constructor.
18+
* @param Connection $connection
19+
* @throws \Exception
20+
*/
21+
public function __construct(Connection $connection)
22+
{
23+
$this->connection = $connection;
24+
if (!$this->connection->connect()) {
25+
throw new \Exception('Unable to connect to database');
26+
}
27+
28+
$tables = $this->connection->getSchemaManager()->listTables();
29+
30+
if (count($tables) < 1) {
31+
$migrator = new Migrator($this->connection);
32+
$migrator->migrate();
33+
}
34+
35+
$n = 1;
36+
}
37+
838
public function export(Project $project) {
939
$n = 1;
1040
}

src/Migrator.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace TapestryCloud\Database;
4+
5+
use Doctrine\DBAL\Connection;
6+
7+
class Migrator
8+
{
9+
10+
/**
11+
* @var Connection
12+
*/
13+
private $connection;
14+
15+
/**
16+
* @var array
17+
*/
18+
private $migrations = [
19+
'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'.
26+
')',
27+
'CREATE TABLE IF NOT EXISTS files ('.
28+
'id INTEGER PRIMARY KEY,' .
29+
'uid TEXT NOT NULL'.
30+
')',
31+
'CREATE TABLE IF NOT EXISTS taxonomies ('.
32+
'id INTEGER PRIMARY KEY,' .
33+
'name TEXT NOT NULL'.
34+
')',
35+
'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'.
43+
')',
44+
'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'.
50+
')',
51+
'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'.
59+
')',
60+
];
61+
62+
/**
63+
* Exporter constructor.
64+
* @param Connection $connection
65+
* @throws \Exception
66+
*/
67+
public function __construct(Connection $connection)
68+
{
69+
$this->connection = $connection;
70+
}
71+
72+
public function migrate()
73+
{
74+
foreach ($this->migrations as $migration) {
75+
$this->connection->exec($migration);
76+
}
77+
}
78+
}

src/ServiceProvider.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
namespace TapestryCloud\Database;
4+
use Doctrine\DBAL\Connection;
5+
use Doctrine\DBAL\DriverManager;
46
use League\Container\ServiceProvider\AbstractServiceProvider;
57
use League\Container\ServiceProvider\BootableServiceProviderInterface;
68
use Tapestry\Entities\Configuration;
@@ -11,7 +13,8 @@ class ServiceProvider extends AbstractServiceProvider implements BootableService
1113
{
1214
/** @var array */
1315
protected $provides = [
14-
Exporter::class
16+
//Exporter::class,
17+
Connection::class
1518
];
1619
/**
1720
* Use the register method to register items with the container via the
@@ -22,9 +25,17 @@ class ServiceProvider extends AbstractServiceProvider implements BootableService
2225
*/
2326
public function register()
2427
{
25-
$this->getContainer()->add(Exporter::class, function() {
26-
return new Exporter();
28+
29+
$this->getContainer()->add(Connection::class, function() {
30+
/** @var Configuration $configuration */
31+
$configuration = $this->getContainer()->get(Configuration::class);
32+
return DriverManager::getConnection($configuration->get('plugins.database', []), new \Doctrine\DBAL\Configuration());
2733
});
34+
35+
// $this->getContainer()->add(Exporter::class, function() {
36+
//
37+
// return new Exporter();
38+
// });
2839
}
2940
/**
3041
* Method will be invoked on registration of a service provider implementing

tests/PluginTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22
namespace TapestryCloud\Asset\Tests;
33
use Symfony\Component\Console\Input\ArrayInput;
4+
use Symfony\Component\Console\Output\NullOutput;
45
use Tapestry\Console\DefaultInputDefinition;
56
use Tapestry\Entities\Project;
7+
use Tapestry\Generator;
68
use Tapestry\Tapestry;
79
class PluginTest extends \PHPUnit_Framework_TestCase
810
{
@@ -13,10 +15,16 @@ public function testPlugin(){
1315
'--env' => 'testing'
1416
], $definitions));
1517

16-
18+
/** @var array $steps */
19+
$steps = $tapestry->getContainer()->get('Compile.Steps');
20+
$generator = new Generator($steps, $tapestry);
1721

1822
/** @var Project $project */
1923
$project = $tapestry->getContainer()->get(Project::class);
24+
$generator->generate($project, new NullOutput());
25+
26+
/** @var Project $project */
27+
//$project = $tapestry->getContainer()->get(Project::class);
2028

2129
$n = 1;
2230
}

tests/mock_project/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build_*
2+
.*_cache
3+
db.sqlite

tests/mock_project/config.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
'url' => 'http://localhost:3000',
44
],
55

6-
'kernel' => \Tests\MockProject\Kernel::class
6+
'kernel' => \Tests\MockProject\Kernel::class,
7+
8+
'plugins' => [
9+
'database' => [
10+
'driver' => 'pdo_sqlite',
11+
'path' => __DIR__ . DIRECTORY_SEPARATOR . 'db.sqlite'
12+
]
13+
]
714
];

0 commit comments

Comments
 (0)