Skip to content

Commit aa4dc08

Browse files
authored
Merge pull request #126 from iazaran/Debugging-and-updates
Debugging and updates
2 parents 7296c47 + 4aa3e0b commit aa4dc08

File tree

9 files changed

+84
-70
lines changed

9 files changed

+84
-70
lines changed

docker-compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,11 @@ services:
8080
- php-mvc-network
8181

8282
php-mvc-db:
83-
image: mysql
83+
image: mysql:8
8484
container_name: php-mvc-db
85-
command: --plugin-load-add=mysql_native_password.so
8685
volumes:
8786
- dbdata:/var/lib/mysql
8887
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
89-
- ./docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
9088
environment:
9189
- MYSQL_ROOT_PASSWORD=root
9290
- MYSQL_DATABASE=mvc_db

docker/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1212

1313
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
1414
RUN docker-php-ext-install mysqli pdo pdo_mysql gd sockets opcache
15+
RUN pecl install xdebug \
16+
&& docker-php-ext-enable xdebug
1517
RUN pecl install -o -f memcached \
1618
&& docker-php-ext-enable memcached
1719
RUN pecl install grpc \
1820
&& docker-php-ext-enable grpc
1921
RUN pecl install protobuf \
2022
&& docker-php-ext-enable protobuf
21-
RUN pecl install xdebug-3.4.0beta1 \
22-
&& docker-php-ext-enable xdebug
2323

2424
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
2525

docker/mysql/init.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

docker/mysql/my.cnf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[mysqld]
2-
plugin-load-add=mysql_native_password.so
32
sql_mode = STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
43
general_log = 1
54
general_log_file = /var/lib/mysql/general.log

docker/php/opcache.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[opcache]
2-
opcache.enable=1
2+
# Enable it in production
3+
opcache.enable=0
34
opcache.revalidate_freq=0
45
opcache.validate_timestamps=0
56
opcache.max_accelerated_files=10000

src/App/Middleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static function WEBauthentication(): mixed
6868
Database::query("SELECT * FROM users WHERE email = :email");
6969
Database::bind(':email', $email);
7070

71-
if (!is_null(Database::fetch()['id'])) return Database::fetch()['id'];
71+
if (!empty(Database::fetch()['id'])) return Database::fetch()['id'];
7272
}
7373
return null;
7474
}
@@ -88,7 +88,7 @@ private static function APIauthentication(): mixed
8888
Database::query("SELECT * FROM users WHERE secret = :secret");
8989
Database::bind(':secret', $matches[1]);
9090

91-
if (!is_null(Database::fetch()['id'])) {
91+
if (!empty(Database::fetch()['id'])) {
9292
setcookie('loggedin', base64_encode(Database::fetch()['email']), time() + (86400 * COOKIE_DAYS));
9393
return Database::fetch()['id'];
9494
}

src/Models/Auth.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static function verify(object $request): bool
4949
Database::bind(':email', $request->email);
5050

5151
if (
52-
!is_null(Database::fetch())
53-
&& !is_null(Database::fetch()['user_token'])
52+
!empty(Database::fetch())
53+
&& !empty(Database::fetch()['user_token'])
5454
&& $request->user_token == Database::fetch()['user_token']
5555
) {
5656
Database::query("UPDATE users SET verified = :verified WHERE email = :email");
@@ -78,7 +78,7 @@ public static function getSecret(object $request): mixed
7878
Database::query("SELECT * FROM users WHERE email = :email");
7979
Database::bind(':email', $request->email);
8080

81-
if (!is_null(Database::fetch()) && !is_null(Database::fetch()['secret'])) return Database::fetch()['secret'];
81+
if (!empty(Database::fetch()) && !empty(Database::fetch()['secret'])) return Database::fetch()['secret'];
8282
return false;
8383
}
8484

@@ -93,7 +93,7 @@ public static function checkEmail(string $email): bool
9393
Database::query("SELECT * FROM users WHERE email = :email");
9494
Database::bind(':email', $email);
9595

96-
if (!is_null(Database::fetch())) return true;
96+
if (!empty(Database::fetch())) return true;
9797
return false;
9898
}
9999

@@ -109,7 +109,7 @@ public static function checkPassword(object $request): bool
109109
Database::bind(':email', $request->email);
110110

111111
if (
112-
!is_null(Database::fetch())
112+
!empty(Database::fetch())
113113
&& password_verify($request->password, Database::fetch()['password'] ?? '')
114114
) {
115115
return true;
@@ -129,7 +129,7 @@ public static function checkVerification(string $email): bool
129129
Database::bind(':email', $email);
130130

131131
if (
132-
!is_null(Database::fetch())
132+
!empty(Database::fetch())
133133
&& Database::fetch()['verified']
134134
) {
135135
return true;

src/migrate.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use App\Database;
4+
5+
/**
6+
* Initialize the migrations table if it doesn't exist.
7+
*/
8+
function initializeMigrationsTable(): void
9+
{
10+
$query = "CREATE TABLE IF NOT EXISTS migrations (
11+
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
12+
migration VARCHAR(255) NOT NULL,
13+
executed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
14+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
15+
Database::query($query);
16+
Database::execute();
17+
}
18+
19+
/**
20+
* Applies a migration only if it has not been applied before.
21+
*
22+
* @param string $migrationName
23+
* @param callable $migrationFunction
24+
*/
25+
function applyMigration(string $migrationName, callable $migrationFunction): void
26+
{
27+
// Check if migration already exists.
28+
$checkQuery = "SELECT COUNT(*) as count FROM migrations WHERE migration = :migration";
29+
Database::query($checkQuery);
30+
Database::bind(':migration', $migrationName);
31+
$result = Database::fetch();
32+
33+
if ((int)$result['count'] === 0) {
34+
// Run the migration.
35+
$migrationFunction();
36+
37+
// Record this migration as executed.
38+
Database::query(
39+
"INSERT INTO migrations (migration, executed_at) VALUES (:migration, NOW())"
40+
);
41+
Database::bind(':migration', $migrationName);
42+
Database::execute();
43+
}
44+
}

src/migrations.php

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
require_once __DIR__ . '/migrate.php';
4+
35
use App\Database;
46

57
/**
@@ -9,24 +11,28 @@
911
*/
1012
function createTables(): void
1113
{
12-
/**
13-
* Tables' structure
14-
*/
15-
$tablesStructures = [
16-
"CREATE TABLE IF NOT EXISTS `users` (
17-
`id` INT UNSIGNED NOT NULL,
14+
initializeMigrationsTable();
15+
16+
applyMigration('create_users_table', function () {
17+
$query = "CREATE TABLE IF NOT EXISTS `users` (
18+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
1819
`email` TINYTEXT NOT NULL,
1920
`password` TINYTEXT NOT NULL,
2021
`secret` TINYTEXT NOT NULL,
2122
`user_token` TINYTEXT NOT NULL,
2223
`verified` TINYINT UNSIGNED NOT NULL DEFAULT 0,
2324
`tagline` TINYTEXT NOT NULL,
2425
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
25-
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
26-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
26+
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(),
27+
PRIMARY KEY (`id`)
28+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
29+
Database::query($query);
30+
Database::execute();
31+
});
2732

28-
"CREATE TABLE IF NOT EXISTS `posts` (
29-
`id` INT UNSIGNED NOT NULL,
33+
applyMigration('create_posts_table', function () {
34+
$query = "CREATE TABLE IF NOT EXISTS `posts` (
35+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
3036
`user_id` INT UNSIGNED NOT NULL,
3137
`category` TINYTEXT NOT NULL,
3238
`title` TINYTEXT NOT NULL,
@@ -35,49 +41,19 @@ function createTables(): void
3541
`body` MEDIUMTEXT NOT NULL,
3642
`position` TINYINT UNSIGNED NOT NULL,
3743
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
38-
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
39-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
40-
];
41-
42-
/**
43-
* Indexes
44-
*/
45-
$tablesIndexes = [
46-
"ALTER TABLE `users` ADD PRIMARY KEY (`id`);",
47-
"ALTER TABLE `posts` ADD PRIMARY KEY (`id`);"
48-
];
49-
50-
/**
51-
* Auto increments
52-
*/
53-
$tablesAutoIncrements = [
54-
"ALTER TABLE `users` MODIFY `id` INT UNSIGNED NOT NULL AUTO_INCREMENT;",
55-
"ALTER TABLE `posts` MODIFY `id` INT UNSIGNED NOT NULL AUTO_INCREMENT;"
56-
];
57-
58-
/**
59-
* Foreign keys
60-
*/
61-
$tablesForeignKeys = [
62-
"ALTER TABLE `posts` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`);"
63-
];
64-
65-
foreach ($tablesStructures as $tablesStructure) {
66-
Database::query($tablesStructure);
44+
`updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(),
45+
PRIMARY KEY (`id`)
46+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
47+
Database::query($query);
6748
Database::execute();
68-
}
69-
foreach ($tablesIndexes as $tablesIndex) {
70-
Database::query($tablesIndex);
71-
Database::execute();
72-
}
73-
foreach ($tablesAutoIncrements as $tablesAutoIncrement) {
74-
Database::query($tablesAutoIncrement);
75-
Database::execute();
76-
}
77-
foreach ($tablesForeignKeys as $tablesForeignKey) {
78-
Database::query($tablesForeignKey);
49+
50+
// You might also include the foreign key creation here.
51+
$fkQuery = "ALTER TABLE `posts` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`);";
52+
Database::query($fkQuery);
7953
Database::execute();
80-
}
54+
});
55+
56+
// Add your new migrations here
8157

8258
/**
8359
* Prevent to create existed tables by commenting a command that call this function

0 commit comments

Comments
 (0)