-
Notifications
You must be signed in to change notification settings - Fork 10
/
database_importer.php
75 lines (59 loc) · 1.63 KB
/
database_importer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
use ILess\Exception\Exception;
use ILess\Importer\DatabaseImporter;
use ILess\Parser;
require_once '_bootstrap.php';
$pdo = new PDO('sqlite::memory:');
$statements = [
'CREATE TABLE [less] (
[filename] VARCHAR(255),
[data] LONGVARCHAR,
[updated_at] TIMESTAMP
)',
'CREATE UNIQUE INDEX [filename_idx] ON [less] ([filename])',
];
foreach ($statements as $statement) {
if (!$pdo->query($statement)) {
$error = $pdo->errorInfo();
throw new Exception($error[2], $error[1]);
}
}
$stmt = $pdo->prepare('INSERT INTO less(filename, data, updated_at) VALUES(?, ?, ?)');
foreach ([
['foo.less', 'body { background: @color; }', time()],
['mixins.less', '.mixin(@a) { background: @a; }', time()],
] as $line) {
$result = $stmt->execute([
$line[0],
$line[1],
$line[2],
]);
}
try {
$cacheDir = dirname(__FILE__) . '/cache';
$parser = new Parser();
$parser->getImporter()->registerImporter(new DatabaseImporter($pdo, [
'table_name' => 'less',
'filename_column' => 'filename',
'data_column' => 'data',
'updated_at_column' => 'data',
]));
$parser->parseString('
@color: red;
@import url("foo.less");
@import (reference) url("mixins.less");
#head {
color: @color + #fff;
.mixin(yellow);
}
');
$cssContent = $parser->getCSS();
file_put_contents($cacheDir . '/database.css', $cssContent);
$css = 'cache/database.css';
} catch (Exception $e) {
@header('HTTP/1.0 500 Internal Server Error');
echo $e;
exit;
}
$example = 'database importer';
include '_page.php';