-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.php
More file actions
75 lines (66 loc) · 3.37 KB
/
app.php
File metadata and controls
75 lines (66 loc) · 3.37 KB
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
#!/usr/bin/env php
<?php declare(strict_types=1);
/*
* Command-line script to create application database
* and stage it with test data.
*/
// Restricts usage to command-line
if (PHP_SAPI !== 'cli') {
die();
}
// Ignores script filename
unset($argv[0]);
// Parses command-line arguments
foreach ($argv as $arg) {
$sql = '';
switch ($arg) {
case 'install':
// Creates table for movie data
$sql .= 'CREATE TABLE movies (id INT NOT NULL AUTO_INCREMENT, attributes BLOB, PRIMARY KEY (id));';
// Creates table for movieratings
$sql .= 'CREATE TABLE movieratings (id INT NOT NULL AUTO_INCREMENT, movie_id INT NOT NULL, average_rating INT NOT NULL, total_ratings INT NOT NULL, PRIMARY KEY (id), UNIQUE (movie_id));';
// Creates table for usermovieratings
$sql .= 'CREATE TABLE usermovieratings (id INT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, movie_id INT NOT NULL, rating INT NOT NULL, PRIMARY KEY (id), UNIQUE (user_id, movie_id));';
break;
case 'stage':
// Prepares PDO connection
require_once 'src/App.php';
$connection = $connection ?? \RestSample\App::withConfig()->getDbConnection();
// Generates dummy movie records
$statement = $connection->prepare('INSERT INTO movies (attributes) VALUES (?);INSERT INTO movies (attributes) VALUES (?);INSERT INTO movies (attributes) VALUES (?);');
$statement->execute([json_encode(['name'=>'Jaws']), json_encode(['name'=>'The Ten Commandments']), json_encode(['name'=>'Titanic'])]);
// Generates dummy movieratings records
$statement = $connection->prepare('INSERT INTO movieratings (movie_id, average_rating, total_ratings) VALUES (?, ?, ?);');
$statement->execute([1, 4, 3]);
// Generates dummy usermovieratings records
$statement = $connection->prepare('INSERT INTO usermovieratings (user_id, movie_id, rating) VALUES (?, ?, ?);INSERT INTO usermovieratings (user_id, movie_id, rating) VALUES (?, ?, ?);INSERT INTO usermovieratings (user_id, movie_id, rating) VALUES (?, ?, ?);');
$statement->execute([1, 1, 10, 2, 1, 1, 3, 1, 1]);
// Continues with the next iteration of foreach
continue(2);
case 'unstage':
// Truncates table for movies
$sql .= 'TRUNCATE TABLE movies;';
// Truncates table for movieratings
$sql .= 'TRUNCATE TABLE movieratings;';
// Truncates table for usermovieratings
$sql .= 'TRUNCATE TABLE usermovieratings;';
break;
case 'uninstall':
// Includes settings definition
require_once 'src/App.php';
$connection = $connection ?? \RestSample\App::withConfig()->getDbConnection();
// Drops app database
$connection->exec('DROP DATABASE IF EXISTS '.APP_CONFIG['db']['dbname']);
unset($connection);
continue(2);
default:
// Displays available commands
echo 'Valid commands are "install", "stage", "unstage", and "uninstall"'.PHP_EOL;
exit(1);
}
// Creates new database if it doesn't exist
require_once 'src/App.php';
$connection = $connection ?? \RestSample\App::withConfig()->getDbConnection();
// Executes sql commands
$connection->exec($sql);
}