Skip to content

Commit 2d153d5

Browse files
committed
Separating out initialization of ORM from loading the schema
1 parent 52cbecd commit 2d153d5

File tree

13 files changed

+187
-199
lines changed

13 files changed

+187
-199
lines changed

SOB/BaseLine.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ class BaseLine
66
{
77
private int $memory = 0;
88

9+
private int $pauseMemory = 0;
10+
11+
private float $pauseSeconds = 0.0;
12+
13+
private \SebastianBergmann\Timer\Timer $pauseTimer;
14+
915
private \SebastianBergmann\Timer\Timer $timer;
1016

1117
public function __construct()
@@ -17,11 +23,28 @@ public function __construct()
1723

1824
public function getMemory() : int
1925
{
20-
return \memory_get_usage() - $this->memory;
26+
return \memory_get_usage() - $this->memory - $this->pauseMemory;
27+
}
28+
29+
public function pause() : static
30+
{
31+
$this->pauseMemory = \memory_get_usage();
32+
$this->pauseTimer = new \SebastianBergmann\Timer\Timer();
33+
$this->pauseTimer->start();
34+
35+
return $this;
36+
}
37+
38+
public function resume() : static
39+
{
40+
$this->pauseSeconds += $this->pauseTimer->stop()->asSeconds();
41+
$this->pauseMemory = \memory_get_usage() - $this->pauseMemory;
42+
43+
return $this;
2144
}
2245

2346
public function stop() : float
2447
{
25-
return $this->timer->stop()->asSeconds();
48+
return $this->timer->stop()->asSeconds() - $this->pauseSeconds;
2649
}
2750
}

SOB/Cake/Tests.php

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,12 @@ public function delete(int $id) : bool
2424
}
2525

2626
/**
27-
* Initialize Responsibilities:
27+
* Initialize the orm
2828
*
29-
* * Initialize the orm
30-
* * open the database
31-
* * initialize the database schema
29+
* @param array<string> $lines sql to import into schema
3230
*/
33-
public function init(\SOB\Configuration $config) : static
31+
public function init(\SOB\Configuration $config, array $lines, \SOB\BaseLine $runTimer) : static
3432
{
35-
$connection = $config->getPDOConnectionString();
36-
37-
if (\str_contains($connection, 'sqlite'))
38-
{
39-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sqlite');
40-
\fclose(\fopen($config->getNamespace() . '.sqlite', 'w'));
41-
}
42-
else
43-
{
44-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sql');
45-
}
46-
4733
$driver = '\\Cake\\Database\\Driver\\' . \ucfirst(\strtolower($config->getDriver()));
4834

4935
$run = \SOB\Cake\RunManager::get();
@@ -52,33 +38,19 @@ public function init(\SOB\Configuration $config) : static
5238
'driver' => $driver,
5339
'persistent' => false,
5440
'host' => $config->getHost(),
41+
'port' => $config->getPort(),
5542
'username' => $config->getUser(),
5643
'password' => $config->getPassword(),
5744
'database' => $config->getDatabase(),
5845
'encoding' => 'utf8mb4',
5946
'timezone' => 'UTC',
6047
'cacheMetadata' => false,
6148
]);
62-
$this->connection = \Cake\Datasource\ConnectionManager::get($run);
6349

64-
$sql = '';
50+
$this->connection = \Cake\Datasource\ConnectionManager::get($run);
51+
$callback = [$this->connection, 'execute'];
6552

66-
foreach ($lines as $line)
67-
{
68-
// Ignoring comments from the SQL script
69-
if (\str_starts_with((string)$line, '--') || '' == $line)
70-
{
71-
continue;
72-
}
73-
74-
$sql .= $line;
75-
76-
if (\str_ends_with(\trim((string)$line), ';'))
77-
{
78-
$this->connection->execute($sql); // @phpstan-ignore-line
79-
$sql = '';
80-
}
81-
} // end foreach
53+
$this->loadSchema($lines, $callback, $runTimer);
8254

8355
return $this;
8456
}

SOB/CakeCached/Tests.php

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,12 @@ public function delete(int $id) : bool
2525
}
2626

2727
/**
28-
* Initialize Responsibilities:
28+
* Initialize the orm
2929
*
30-
* * Initialize the orm
31-
* * open the database
32-
* * initialize the database schema
30+
* @param array<string> $lines sql to import into schema
3331
*/
34-
public function init(\SOB\Configuration $config) : static
32+
public function init(\SOB\Configuration $config, array $lines, \SOB\BaseLine $runTimer) : static
3533
{
36-
$connection = $config->getPDOConnectionString();
37-
38-
if (\str_contains($connection, 'sqlite'))
39-
{
40-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sqlite');
41-
\fclose(\fopen($config->getNamespace() . '.sqlite', 'w'));
42-
}
43-
else
44-
{
45-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sql');
46-
}
47-
4834
$driver = '\\Cake\\Database\\Driver\\' . \ucfirst(\strtolower($config->getDriver()));
4935

5036
$run = \SOB\Cake\RunManager::get();
@@ -53,35 +39,21 @@ public function init(\SOB\Configuration $config) : static
5339
'driver' => $driver,
5440
'persistent' => false,
5541
'host' => $config->getHost(),
42+
'port' => $config->getPort(),
5643
'username' => $config->getUser(),
5744
'password' => $config->getPassword(),
5845
'database' => $config->getDatabase(),
5946
'encoding' => 'utf8mb4',
6047
'timezone' => 'UTC',
6148
'cacheMetadata' => false,
6249
]);
63-
$this->connection = \Cake\Datasource\ConnectionManager::get($run);
6450

65-
$sql = '';
66-
67-
foreach ($lines as $line)
68-
{
69-
// Ignoring comments from the SQL script
70-
if (\str_starts_with((string)$line, '--') || '' == $line)
71-
{
72-
continue;
73-
}
74-
75-
$sql .= $line;
51+
$this->connection = \Cake\Datasource\ConnectionManager::get($run);
52+
$this->employeeTable = new \SOB\Cake\Table\Employee();
7653

77-
if (\str_ends_with(\trim((string)$line), ';'))
78-
{
79-
$this->connection->execute($sql); // @phpstan-ignore-line
80-
$sql = '';
81-
}
82-
} // end foreach
54+
$callback = [$this->connection, 'execute'];
8355

84-
$this->employeeTable = new \SOB\Cake\Table\Employee();
56+
$this->loadSchema($lines, $callback, $runTimer);
8557

8658
return $this;
8759
}

SOB/Doctrine/Tests.php

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function closeConnection() : void
1919
public function delete(int $id) : bool
2020
{
2121
$this->entityManager->remove($this->read($id));
22-
$this->entityManager->flush();
22+
$this->flush();
2323

2424
return true;
2525
}
@@ -30,16 +30,14 @@ public function flush() : void
3030
}
3131

3232
/**
33-
* Initialize Responsibilities:
33+
* Initialize the orm
3434
*
35-
* * Initialize the orm
36-
* * open the database
37-
* * initialize the database schema
35+
* @param array<string> $lines sql to import into schema
3836
*/
39-
public function init(\SOB\Configuration $config) : static
37+
public function init(\SOB\Configuration $config, array $lines, \SOB\BaseLine $runTimer) : static
4038
{
41-
$queryCache = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
42-
$metadataCache = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
39+
$queryCache = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
40+
$metadataCache = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
4341

4442
$doctrineConfig = new \Doctrine\ORM\Configuration();
4543
$doctrineConfig->setMetadataCache($metadataCache);
@@ -49,17 +47,14 @@ public function init(\SOB\Configuration $config) : static
4947
$doctrineConfig->setQueryCache($queryCache);
5048
$doctrineConfig->setProxyDir(__DIR__ . '/Proxy');
5149
$doctrineConfig->setProxyNamespace('SOB\Doctrine\Proxy');
52-
$doctrineConfig->setAutoGenerateProxyClasses(true);
50+
$doctrineConfig->setAutoGenerateProxyClasses(true);
5351

5452
// configuring the database connection
55-
$database = $config->getDatabase();
5653
$settings = [
5754
'driver' => 'pdo_' . $config->getDriver(),
5855
'host' => $config->getHost(),
59-
// 'path' => $config->getDatabase(),
6056
'user' => $config->getUser(),
6157
'password' => $config->getPassword(),
62-
// 'dbname' => $config->getDatabase(),
6358
'charset' => 'utf8',
6459
'port' => $config->getPort(),
6560
];
@@ -78,36 +73,9 @@ public function init(\SOB\Configuration $config) : static
7873

7974
// obtaining the entity manager
8075
$this->entityManager = new \Doctrine\ORM\EntityManager($connection, $doctrineConfig);
76+
$callback = [$connection, 'executeQuery'];
8177

82-
if (\str_contains($config->getDriver(), 'sqlite'))
83-
{
84-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sqlite');
85-
\fclose(\fopen($config->getNamespace() . '.sqlite', 'w'));
86-
}
87-
else
88-
{
89-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sql');
90-
}
91-
92-
$sql = '';
93-
$conn = $this->entityManager->getConnection();
94-
95-
foreach ($lines as $line)
96-
{
97-
// Ignoring comments from the SQL script
98-
if (\str_starts_with((string)$line, '--') || \str_starts_with((string)$line, '#') || '' == $line)
99-
{
100-
continue;
101-
}
102-
103-
$sql .= $line;
104-
105-
if (\str_ends_with(\trim((string)$line), ';'))
106-
{
107-
$conn->executeUpdate($sql);
108-
$sql = '';
109-
}
110-
} // end foreach
78+
$this->loadSchema($lines, $callback, $runTimer);
11179

11280
return $this;
11381
}
@@ -125,7 +93,7 @@ public function insert(int $id) : int
12593
$employee->last_name = "Last {$id}";
12694
$employee->first_name = "First {$id}";
12795
$this->entityManager->persist($employee);
128-
$this->entityManager->flush();
96+
$this->flush();
12997

13098
return $employee->employee_id;
13199
}
@@ -149,7 +117,7 @@ public function update(int $id, int $to) : bool
149117

150118
$this->entityManager->persist($employee);
151119

152-
$this->entityManager->flush();
120+
$this->flush();
153121

154122
return true;
155123
}

SOB/Eloquent/Tests.php

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,12 @@ public function delete(int $id) : bool
2626
}
2727

2828
/**
29-
* Initialize Responsibilities:
29+
* Initialize the orm
3030
*
31-
* * Initialize the orm
32-
* * open the database
33-
* * initialize the database schema
31+
* @param array<string> $lines sql to import into schema
3432
*/
35-
public function init(\SOB\Configuration $config) : static
33+
public function init(\SOB\Configuration $config, array $lines, \SOB\BaseLine $runTimer) : static
3634
{
37-
if (\str_contains($config->getDriver(), 'sqlite'))
38-
{
39-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sqlite');
40-
\fclose(\fopen($config->getNamespace() . '.sqlite', 'w'));
41-
}
42-
else
43-
{
44-
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sql');
45-
}
46-
4735
$this->capsule = new \Illuminate\Database\Capsule\Manager();
4836

4937
$this->capsule->addConnection([
@@ -69,25 +57,9 @@ public function init(\SOB\Configuration $config) : static
6957
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
7058
$this->capsule->bootEloquent();
7159

72-
$sql = '';
73-
74-
foreach ($lines as $line)
75-
{
76-
// Ignoring comments from the SQL script
77-
if (\str_starts_with((string)$line, '--') || \str_starts_with((string)$line, '#') || '' == $line)
78-
{
79-
continue;
80-
}
81-
82-
$sql .= $line;
83-
84-
if (\str_ends_with(\trim((string)$line), ';'))
85-
{
86-
$stmt = $this->pdo->prepare($sql);
87-
$stmt->execute();
88-
$sql = '';
89-
}
90-
} // end foreach
60+
$callback = [$this->pdo, 'exec'];
61+
62+
$this->loadSchema($lines, $callback, $runTimer);
9163

9264
return $this;
9365
}

0 commit comments

Comments
 (0)