Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch between dev/prod + PDO MySQL example env file #13

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ index.php
composer.phar
/vendor/

### Cache ###
/storage/cache/views/*.php

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
composer.lock
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/env.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$dotenv = Dotenv\Dotenv::createImmutable(app_path());
$environment = $dotenv->load();

// In production, save the environment in a file so that it wont be necessary to parse and load .env file at every call.
// In production, save the environment in a file so that it won't be necessary to parse and load .env file at every call.
if (env('APP_ENV') == 'production') {
$file = new SplFileObject($environment_file, 'w');
$file->fwrite('<?php'.PHP_EOL.PHP_EOL.'return '.var_export($environment, true).';'.PHP_EOL);
Expand Down
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@
],
"optimize": "composer install --prefer-dist --optimize-autoloader --no-dev",
"test": "./vendor/bin/pest tests",
"test-win": ".\\vendor\\bin\\pest tests"
"test-win": ".\\vendor\\bin\\pest tests",
"prod": "composer production",
"dev": "composer development",
"production": [
"rm -f config/environment.php storage/cache/routes.cache.php",
"sed -i \"s|APP_ENV=.*|APP_ENV=production|g\" .env",
"sed -i \"s|APP_DEBUG=.*|APP_DEBUG=false|g\" .env"
],
"development": [
"rm -f config/environment.php storage/cache/routes.cache.php",
"sed -i \"s|APP_ENV=.*|APP_ENV=development|g\" .env",
"sed -i \"s|APP_DEBUG=.*|APP_DEBUG=true|g\" .env"
]
}
}
7 changes: 7 additions & 0 deletions config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
*/

$container->set(PDO::class, function () {
// For MySQL with connexion information from .env file
//$pdo = new PDO(
// 'mysql:host='.env('DB_HOST').';port='.env('DB_PORT').';dbname='.env('DB_DATABASE').';charset=utf8mb4',
// env('DB_USERNAME'),
// env('DB_PASSWORD')
//);

$pdo = new PDO('sqlite:'.storage_path('database.sqlite'));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
Expand Down
13 changes: 8 additions & 5 deletions src/Template/BasicTemplateEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function render(string $template, array $context = []): string

$code = preg_replace(
[
'~\{\s*\*.+?\*\s*}~', // comments
'~\{\*[\s\S]+?\*}~', // comments
'~\{%\s*(.+?)\s*}~', // commands (foreach(): endforeach;)
'~\{!!\s*(.+?)\s*}~', // unescaped vars
'~\{\s*(.+?)\s*}~' // escaped vars (default)
Expand All @@ -88,18 +88,21 @@ public function render(string $template, array $context = []): string
'',
'<?php $1 ?>',
'<?php echo $1 ?>',
'<?php echo htmlspecialchars($1, ENT_QUOTES) ?>'
'<?php echo htmlspecialchars($1, ENT_QUOTES|ENT_SUBSTITUTE, \'UTF-8\') ?>'
],
$code
);

file_put_contents($cache_file, $code);
}

extract($this->parameters);

ob_start();
include $cache_file;

(function ($file) {
extract($this->parameters);
include $file;
})($cache_file);

return ob_get_clean();
}
}
Expand Down