Skip to content

Commit

Permalink
Replace tab by space
Browse files Browse the repository at this point in the history
  • Loading branch information
semhoun committed Dec 30, 2020
1 parent bdf74b5 commit 3ae7425
Show file tree
Hide file tree
Showing 20 changed files with 818 additions and 818 deletions.
44 changes: 22 additions & 22 deletions bin/entities_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
use Symfony\Component\Console\Application;

function rrmdir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
rrmdir($full);
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
rrmdir($full);
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}

// Set the absolute path to the root directory.
Expand Down Expand Up @@ -48,7 +48,7 @@ function rrmdir($src) {
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
$em->getConnection()->getSchemaManager()
);
$driver->setNamespace('\\App\\Entity\\');
$em->getConfiguration()->setMetadataDriverImpl($driver);
Expand All @@ -65,13 +65,13 @@ function rrmdir($src) {
// Move files in the good directory
$files = glob($settings['temporary_path'] . '/tmp_entity/App/Entity/*.{php}', GLOB_BRACE);
foreach($files as $fullpath) {
$file = basename($fullpath);
$src = file_get_contents($fullpath);
if (preg_match('#^namespace\s+(.+?);$#sm', $src)) {
continue;
}
$php = preg_replace("/<\?php/", "<?php\nnamespace App\\Entity;", $src);
file_put_contents($rootPath . '/src/Entity/' . $file, $php);
$file = basename($fullpath);
$src = file_get_contents($fullpath);
if (preg_match('#^namespace\s+(.+?);$#sm', $src)) {
continue;
}
$php = preg_replace("/<\?php/", "<?php\nnamespace App\\Entity;", $src);
file_put_contents($rootPath . '/src/Entity/' . $file, $php);
}

rrmdir($settings['temporary_path'] . '/tmp_entity');
Expand Down
68 changes: 34 additions & 34 deletions conf/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@
use Slim\Views\Twig;

return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
'logger' => function (ContainerInterface $container) {
$settings = $container->get('settings');
$containerBuilder->addDefinitions([
'logger' => function (ContainerInterface $container) {
$settings = $container->get('settings');

$loggerSettings = $settings['logger'];
$logger = new Logger($loggerSettings['name']);
$loggerSettings = $settings['logger'];
$logger = new Logger($loggerSettings['name']);

$processor = new UidProcessor();
$logger->pushProcessor($processor);
$processor = new UidProcessor();
$logger->pushProcessor($processor);

$handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']);
$logger->pushHandler($handler);
$handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']);
$logger->pushHandler($handler);

return $logger;
},
'em' => function (ContainerInterface $container) {
$settings = $container->get('settings');
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
$settings['doctrine']['meta']['entity_path'],
$settings['doctrine']['meta']['auto_generate_proxies'],
$settings['doctrine']['meta']['proxy_dir'],
$settings['doctrine']['meta']['cache'],
false
);
return EntityManager::create($settings['doctrine']['connection'], $config);
},
'session' => function (ContainerInterface $container) {
return new \App\Middleware\SessionMiddleware;
},
'flash' => function (ContainerInterface $container) {
$session = $container->get('session');
return new \Slim\Flash\Messages($session);
},
'view' => function (ContainerInterface $container) {
$settings = $container->get('settings');
return Twig::create($settings['view']['template_path'], $settings['view']['twig']);
},
]);
return $logger;
},
'em' => function (ContainerInterface $container) {
$settings = $container->get('settings');
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
$settings['doctrine']['meta']['entity_path'],
$settings['doctrine']['meta']['auto_generate_proxies'],
$settings['doctrine']['meta']['proxy_dir'],
$settings['doctrine']['meta']['cache'],
false
);
return EntityManager::create($settings['doctrine']['connection'], $config);
},
'session' => function (ContainerInterface $container) {
return new \App\Middleware\SessionMiddleware;
},
'flash' => function (ContainerInterface $container) {
$session = $container->get('session');
return new \Slim\Flash\Messages($session);
},
'view' => function (ContainerInterface $container) {
$settings = $container->get('settings');
return Twig::create($settings['view']['template_path'], $settings['view']['twig']);
},
]);
};

8 changes: 4 additions & 4 deletions conf/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Slim\Views\TwigMiddleware;

return function(App $app) {
$container = $app->getContainer();
$settings = $container->get('settings');
$container = $app->getContainer();
$settings = $container->get('settings');

$app->add($container->get('session'));
$app->add($container->get('session'));

$app->add(TwigMiddleware::createFromContainer($app));
$app->add(TwigMiddleware::createFromContainer($app));
};
12 changes: 6 additions & 6 deletions conf/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use Slim\Interfaces\RouteCollectorProxyInterface as Group;

return function (App $app) {
$app->get('/', 'App\Controller\HomeController:index')->setName('home');
$app->get('/', 'App\Controller\HomeController:index')->setName('home');

$app->get('/post/{id}', 'App\Controller\HomeController:viewPost')->setName('post');
$app->get('/post/{id}', 'App\Controller\HomeController:viewPost')->setName('post');

$app->group('/member', function (Group $group) {
$group->map(['GET', 'POST'],'/login', 'App\Controller\AuthController:login')->setName('login');
$group->get('/logout', 'App\Controller\AuthController:logout')->setName('logout');
$app->group('/member', function (Group $group) {
$group->map(['GET', 'POST'],'/login', 'App\Controller\AuthController:login')->setName('login');
$group->get('/logout', 'App\Controller\AuthController:logout')->setName('logout');

});
});
};
106 changes: 53 additions & 53 deletions conf/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,57 @@
use Monolog\Logger;

return function (ContainerBuilder $containerBuilder) {
$rootPath = realpath(__DIR__ . '/..');

// Global Settings Object
$containerBuilder->addDefinitions([
'settings' => [
// Base path
'base_path' => '',
// Is debug mode
'debug' => (getenv('APPLICATION_ENV') != 'production'),

// 'Temprorary directory
'temporary_path' => $rootPath . '/var/tmp',

// Route cache
'route_cache' =>$rootPath . '/var/routes.cache',

// View settings
'view' => [
'template_path' =>$rootPath . '/tmpl',
'twig' => [
'cache' =>$rootPath . '/var/cache/twig',
'debug' => (getenv('APPLICATION_ENV') != 'production'),
'auto_reload' => true,
],
],

// doctrine settings
'doctrine' => [
'meta' => [
'entity_path' => [ $rootPath . '/src/Entity' ],
'auto_generate_proxies' => true,
'proxy_dir' => $rootPath . '/var/cache/proxies',
'cache' => null,
],
'connection' => [
'driver' => 'pdo_sqlite',
'path' => $rootPath . '/var/blog.sqlite'
]
],

// monolog settings
'logger' => [
'name' => 'app',
'path' => getenv('docker') ? 'php://stdout' : $rootPath . '/var/log/app.log',
'level' => (getenv('APPLICATION_ENV') != 'production') ? Logger::DEBUG : Logger::INFO,
]
],
]);

if (getenv('APPLICATION_ENV') == 'production') { // Should be set to true in production
$containerBuilder->enableCompilation($rootPath . '/var/cache');
}
$rootPath = realpath(__DIR__ . '/..');

// Global Settings Object
$containerBuilder->addDefinitions([
'settings' => [
// Base path
'base_path' => '',
// Is debug mode
'debug' => (getenv('APPLICATION_ENV') != 'production'),

// 'Temprorary directory
'temporary_path' => $rootPath . '/var/tmp',

// Route cache
'route_cache' =>$rootPath . '/var/cache/routes',

// View settings
'view' => [
'template_path' =>$rootPath . '/tmpl',
'twig' => [
'cache' =>$rootPath . '/var/cache/twig',
'debug' => (getenv('APPLICATION_ENV') != 'production'),
'auto_reload' => true,
],
],

// doctrine settings
'doctrine' => [
'meta' => [
'entity_path' => [ $rootPath . '/src/Entity' ],
'auto_generate_proxies' => true,
'proxy_dir' => $rootPath . '/var/cache/proxies',
'cache' => null,
],
'connection' => [
'driver' => 'pdo_sqlite',
'path' => $rootPath . '/var/blog.sqlite'
]
],

// monolog settings
'logger' => [
'name' => 'app',
'path' => getenv('docker') ? 'php://stdout' : $rootPath . '/var/log/app.log',
'level' => (getenv('APPLICATION_ENV') != 'production') ? Logger::DEBUG : Logger::INFO,
]
],
]);

if (getenv('APPLICATION_ENV') == 'production') { // Should be set to true in production
$containerBuilder->enableCompilation($rootPath . '/var/cache');
}
};
78 changes: 39 additions & 39 deletions src/Command/InitDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,56 @@

class InitDB extends Command
{
private $settings;
private $settings;

public function __construct(Container $container)
{
parent::__construct();
$this->settings = $container->get('settings');
}
public function __construct(Container $container)
{
parent::__construct();
$this->settings = $container->get('settings');
}

protected function configure()
{
$this
// the name of the command (the part after "bin/console")
->setName('app:init-db')
protected function configure()
{
$this
// the name of the command (the part after "bin/console")
->setName('app:init-db')

// the short description shown while running "php bin/console list"
->setDescription('Initialize database')
// the short description shown while running "php bin/console list"
->setDescription('Initialize database')

// the full command description shown when running the command with
// the "--help" option
->setHelp('Create database structe and add initial data');
;
}
// the full command description shown when running the command with
// the "--help" option
->setHelp('Create database structe and add initial data');
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$dbi = new \SQLite3($this->settings['doctrine']['connection']['path']);
if (!$dbi) {
$output->writeLn('Can\'t open sqlite3 database [' . $this->settings['doctrine']['connection']['path'] . ']');
return;
}
$dbi->exec('BEGIN TRANSACTION;');
protected function execute(InputInterface $input, OutputInterface $output)
{
$dbi = new \SQLite3($this->settings['doctrine']['connection']['path']);
if (!$dbi) {
$output->writeLn('Can\'t open sqlite3 database [' . $this->settings['doctrine']['connection']['path'] . ']');
return;
}
$dbi->exec('BEGIN TRANSACTION;');

$queries = [
$queries = [
"CREATE TABLE post (id int primary key not null, title char(100) default null, slug char(200) not null, content text not null);",
"INSERT INTO post VALUES(1,'First blog post','first-blog-post','This is sample blog post. If you see this content, doctrine is working fine.');",
"CREATE TABLE user (id int primary key not null, username char(30) not null, password char(60) not null, first_name char(50), last_name char(50), email char(50));",
"INSERT INTO user VALUES(1,'admin','\$2y\$10\$h2DgpuQvOWhpVmthACoKTuEVQHwHvcg5WjUekdvZx41hukm6LaUzy', 'Administator', 'THE', 'admin@admin.com');",
];
];

foreach($queries as $query) {
if (!$dbi->exec($query)) {
$output->writeLn('Can\'t execute SQL query "' . $query . '": ' . $dbi->lastErrorMsg());
$dbi->exec('ROLLBACK;');
return;
}
}
foreach($queries as $query) {
if (!$dbi->exec($query)) {
$output->writeLn('Can\'t execute SQL query "' . $query . '": ' . $dbi->lastErrorMsg());
$dbi->exec('ROLLBACK;');
return;
}
}

$dbi->exec('COMMIT;');
$dbi->exec('COMMIT;');

$output->writeLn("Database structure created");
$dbi->close();
}
$output->writeLn("Database structure created");
$dbi->close();
}
}
Loading

0 comments on commit 3ae7425

Please sign in to comment.