Skip to content

Commit

Permalink
Merge pull request #13 from borschphp/minor-improvments
Browse files Browse the repository at this point in the history
Switch between dev/prod + PDO MySQL example env file
  • Loading branch information
debuss committed Aug 31, 2023
2 parents c77a0b4 + f9051f4 commit b8ff0d6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
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

0 comments on commit b8ff0d6

Please sign in to comment.