1
|
|
|
<?php |
2
|
|
|
namespace Germania\Websites; |
3
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
5
|
|
|
|
6
|
|
|
class PdoRouteWebsiteFactory implements ContainerInterface |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
public $table = 'pages'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \PDOStatement |
16
|
|
|
*/ |
17
|
|
|
public $stmt; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param PDO $pdo |
21
|
|
|
* @param string $table Websites table name |
22
|
|
|
* @param WebsiteAbstract $website Optional: Website template object |
23
|
|
|
*/ |
24
|
|
|
public function __construct( \PDO $pdo, $table, WebsiteAbstract $website = null ) |
25
|
|
|
{ |
26
|
|
|
$this->table = $table; |
27
|
|
|
|
28
|
|
|
$sql = "SELECT |
29
|
|
|
id, |
30
|
|
|
title, |
31
|
|
|
via, |
32
|
|
|
route, |
33
|
|
|
route_name, |
34
|
|
|
content_file, |
35
|
|
|
controller, |
36
|
|
|
template, |
37
|
|
|
dom_id, |
38
|
|
|
javascripts, |
39
|
|
|
stylesheets, |
40
|
|
|
is_active |
41
|
|
|
FROM {$this->table} |
42
|
|
|
|
43
|
|
|
WHERE route_name = :route_name |
44
|
|
|
LIMIT 1"; |
45
|
|
|
|
46
|
|
|
$this->stmt = $pdo->prepare( $sql ); |
47
|
|
|
$this->stmt->setFetchMode( \PDO::FETCH_CLASS, $website ? get_class($website) : Website::class ); |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
public function __invoke( $route_name ) |
53
|
|
|
{ |
54
|
|
|
return $this->get( $route_name ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
public function has ($route_name) { |
60
|
|
|
$this->executeStatement( $route_name ); |
61
|
|
|
return (bool) $this->stmt->fetch(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function get ($route_name) { |
66
|
|
|
$this->executeStatement( $route_name ); |
67
|
|
|
|
68
|
|
|
if ($row = $this->stmt->fetch()) { |
69
|
|
|
|
70
|
|
|
// Cast numeric is_active field to integer |
71
|
|
|
$row->is_active = (int) $row->is_active; |
72
|
|
|
|
73
|
|
|
// Split into array |
74
|
|
|
if (isset($row->javascripts)): |
75
|
|
|
$row->javascripts = preg_split("/[\s,]+/", trim($row->javascripts)); |
76
|
|
|
endif; |
77
|
|
|
if (isset($row->stylesheets)): |
78
|
|
|
$row->stylesheets = preg_split("/[\s,]+/", trim($row->stylesheets)); |
79
|
|
|
endif; |
80
|
|
|
if (isset($row->via)): |
81
|
|
|
$row->via = preg_split("/[\s,]+/", trim($row->via)); |
82
|
|
|
endif; |
83
|
|
|
|
84
|
|
|
return $row; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$msg = sprintf("Could not find website for route name '%s'", $route_name); |
88
|
|
|
throw new WebsiteNotFoundException( $msg ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
protected function executeStatement ($route_name) { |
93
|
|
|
if (!$this->stmt->execute([ |
94
|
|
|
':route_name' => $route_name |
95
|
|
|
])): |
96
|
|
|
throw new \RuntimeException("Could not execute PDOStatement", [ |
97
|
|
|
'table' => $this->table, |
98
|
|
|
'route_name' => $route_name |
99
|
|
|
]); |
100
|
|
|
endif; |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|