|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Esi\SimpleTpl. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2006 - 2025 Eric Sizemore <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* This file is licensed under The MIT License. For the full copyright and |
|
11
|
|
|
* license information, please view the LICENSE.md file that was distributed |
|
12
|
|
|
* with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Esi\SimpleTpl\Storage; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Provides the table name, name of the field that stores template content, and name of the field |
|
19
|
|
|
* that holds the template name. |
|
20
|
|
|
* |
|
21
|
|
|
* For a structure of: |
|
22
|
|
|
* |
|
23
|
|
|
* ``` |
|
24
|
|
|
* CREATE TABLE IF NOT EXISTS `templates` ( |
|
25
|
|
|
* `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, |
|
26
|
|
|
* `name` VARCHAR(255) NOT NULL, |
|
27
|
|
|
* `content` MEDIUMTEXT NOT NULL, |
|
28
|
|
|
* PRIMARY KEY (`id`), |
|
29
|
|
|
* UNIQUE KEY `name` (`name`) |
|
30
|
|
|
* ) |
|
31
|
|
|
* ``` |
|
32
|
|
|
* |
|
33
|
|
|
* You would instantiate this class as: |
|
34
|
|
|
* |
|
35
|
|
|
* ``` |
|
36
|
|
|
* $config = new DatabaseStorageConfig('templates', 'name', 'content'); |
|
37
|
|
|
* ``` |
|
38
|
|
|
*/ |
|
39
|
|
|
final class DatabaseStorageConfig |
|
40
|
|
|
{ |
|
41
|
5 |
|
public function __construct( |
|
42
|
|
|
public string $tableName = 'templates', |
|
43
|
|
|
public string $nameField = 'name', |
|
44
|
|
|
public string $contentField = 'content' |
|
45
|
|
|
) { |
|
46
|
5 |
|
$this->verifyConfig(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Simple utility method to return the table schema with the given tableName, nameField, and contentField. |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function getSchema(): string |
|
53
|
|
|
{ |
|
54
|
1 |
|
return \sprintf(<<<'SQL' |
|
55
|
|
|
CREATE TABLE IF NOT EXISTS `%1$s` ( |
|
56
|
|
|
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, |
|
57
|
|
|
`%2$s` VARCHAR(255) NOT NULL, |
|
58
|
|
|
`%3$s` MEDIUMTEXT NOT NULL, |
|
59
|
|
|
PRIMARY KEY (`id`), |
|
60
|
|
|
UNIQUE KEY `%2$s` (`%2$s`) |
|
61
|
|
|
) |
|
62
|
1 |
|
SQL, $this->tableName, $this->nameField, $this->contentField); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Performs a simple check on the provided options and reverts to defaults if needed. |
|
67
|
|
|
*/ |
|
68
|
5 |
|
private function verifyConfig(): void |
|
69
|
|
|
{ |
|
70
|
5 |
|
$this->contentField = trim($this->contentField); |
|
71
|
5 |
|
$this->tableName = trim($this->tableName); |
|
72
|
5 |
|
$this->nameField = trim($this->nameField); |
|
73
|
|
|
|
|
74
|
|
|
// Set to defaults, if somehow empty after trim. |
|
75
|
5 |
|
if ($this->contentField === '') { |
|
76
|
1 |
|
$this->contentField = 'content'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
5 |
|
if ($this->tableName === '') { |
|
80
|
1 |
|
$this->tableName = 'templates'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
if ($this->nameField === '') { |
|
84
|
1 |
|
$this->nameField = 'name'; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|