Skip to content

Commit

Permalink
symfony skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed May 31, 2024
1 parent f3caef4 commit 0fa45f3
Show file tree
Hide file tree
Showing 32 changed files with 498 additions and 262 deletions.
3 changes: 2 additions & 1 deletion actions-qrcode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
require("common.php");

require_once "common.php";

function response($message,$error=0,$log=1)
{
Expand Down
14 changes: 1 addition & 13 deletions admin.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
<?php

use BikeShare\Authentication\Auth;
use BikeShare\Db\DbInterface;
use BikeShare\User\User;
use Psr\Log\LoggerInterface;

require_once 'vendor/autoload.php';
require('actions-web.php');

/**
* @var DbInterface $db
* @var LoggerInterface $logger
*/
$user = new User($db);
$auth = new Auth($db);
require_once 'actions-web.php';

$auth->refreshSession();

Expand Down
7 changes: 1 addition & 6 deletions agree.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<?php

use BikeShare\Db\DbInterface;

require_once 'vendor/autoload.php';
require('actions-web.php');
require_once 'actions-web.php';

/**
* @var DbInterface $db
*/
?>
<!DOCTYPE html>
<html lang="en">
Expand Down
17 changes: 17 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env php
<?php

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../common.php');

use \Symfony\Bundle\FrameworkBundle\Console\Application;

$kernel = new \BikeShare\App\Kernel('dev', true);
$kernel->boot();

/** @var \Symfony\Component\Console\CommandLoader\CommandLoaderInterface $commandLoader */
$commandLoader = $kernel->getContainer()->get('console.command_loader');

$application = new Application($kernel);
$application->setCommandLoader($commandLoader);
$application->run();
22 changes: 2 additions & 20 deletions command.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
<?php

use BikeShare\Authentication\Auth;
use BikeShare\Db\DbInterface;
use BikeShare\Purifier\PhonePurifier;
use BikeShare\Purifier\PhonePurifierInterface;
use BikeShare\Rent\RentSystemFactory;
use BikeShare\Rent\RentSystemInterface;
use BikeShare\User\User;
use Psr\Log\LoggerInterface;

require_once 'vendor/autoload.php';
require('actions-web.php');

/**
* @var DbInterface $db
* @var LoggerInterface $logger
*/
$auth = new Auth($db);
$user = new User($db);
/**
* @var PhonePurifierInterface $phonePurifier
*/
$phonePurifier = new PhonePurifier($configuration->get('countrycode'));
require_once 'actions-web.php';

$userid = $auth->getUserId();
$session = $auth->getSessionId();

/**
* @var RentSystemInterface $rentSystem
*/
$rentSystem = RentSystemFactory::create('web');
$rentSystem = $rentSystemFactory->getRentSystem('web');

$action="";
if (isset($_GET["action"])) $action=trim($_GET["action"]);
Expand Down
99 changes: 24 additions & 75 deletions common.php
Original file line number Diff line number Diff line change
@@ -1,98 +1,47 @@
<?php

require_once 'vendor/autoload.php';

use BikeShare\App\Configuration;
use BikeShare\Credit\CodeGenerator\CodeGenerator;
use BikeShare\App\Kernel;
use BikeShare\Authentication\Auth;
use BikeShare\Credit\CodeGenerator\CodeGeneratorInterface;
use BikeShare\Credit\CreditSystemFactory;
use BikeShare\Credit\CreditSystemInterface;
use BikeShare\Mail\DebugMailSender;
use BikeShare\Mail\MailSenderInterface;
use BikeShare\Mail\PHPMailerMailSender;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\Purifier\PhonePurifier;
use BikeShare\Mail\MailSenderInterface;
use BikeShare\Purifier\PhonePurifierInterface;
use BikeShare\Sms\SmsSender;
use BikeShare\Rent\RentSystemFactory;
use BikeShare\Sms\SmsSenderInterface;
use BikeShare\SmsConnector\DebugConnector;
use BikeShare\SmsConnector\SmsConnectorFactory;
use BikeShare\SmsConnector\SmsConnectorInterface;
use BikeShare\User\User;
use Monolog\ErrorHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

require_once 'vendor/autoload.php';
\Symfony\Component\ErrorHandler\Debug::enable();

$configuration = new Configuration();
$kernel = new Kernel('dev', true);
$kernel->boot();

$logger = new Logger('BikeShare');
$logger->pushHandler(new RotatingFileHandler( __DIR__ . '/var/log/log.log', 30, Logger::WARNING));
ErrorHandler::register($logger);
$logger = $kernel->getContainer()->get('logger');
$configuration = $kernel->getContainer()->get(Configuration::class);
$sms = $kernel->getContainer()->get(SmsConnectorInterface::class);
$db = $kernel->getContainer()->get(DbInterface::class);
$db = $kernel->getContainer()->get(DbInterface::class);
$db->connect();
$mailer = $kernel->getContainer()->get(MailSenderInterface::class);
$smsSender = $kernel->getContainer()->get(SmsSenderInterface::class);
$codeGenerator = $kernel->getContainer()->get(CodeGeneratorInterface::class);
$phonePurifier = $kernel->getContainer()->get(PhonePurifierInterface::class);
$creditSystem = $kernel->getContainer()->get(CreditSystemInterface::class);
$user = $kernel->getContainer()->get(User::class);
$auth = $kernel->getContainer()->get(Auth::class);
$rentSystemFactory = $kernel->getContainer()->get(RentSystemFactory::class);

$locale = $configuration->get('systemlang') . ".utf8";
setlocale(LC_ALL, $locale);
putenv("LANG=" . $locale);
bindtextdomain("messages", dirname(__FILE__) . '/languages');
textdomain("messages");

$connectors = $configuration->get('connectors');
$sms = (new SmsConnectorFactory($logger))->getConnector(
!empty($connectors["sms"]) ? $connectors["sms"] : 'disabled',
!empty($connectors["config"][$connectors["sms"]]) ? json_decode($connectors["config"][$connectors["sms"]], true) : array(),
DEBUG
);

/**
* @var DbInterface $db
*/
$db = new MysqliDb(
$configuration->get('dbserver'),
$configuration->get('dbuser'),
$configuration->get('dbpassword'),
$configuration->get('dbname'),
$logger
);
$db->connect();

/**
* @var MailSenderInterface $mailer
*/
if (DEBUG === TRUE) {
$mailer = new DebugMailSender();
} else {
$mailer = new PHPMailerMailSender(
$configuration->get('systemname'),
$configuration->get('systememail'),
$configuration->get('email'),
new \PHPMailer\PHPMailer\PHPMailer(false)
);
}

/**
* @var SmsSenderInterface $smsSender
*/
$smsSender = new SmsSender(
DEBUG === TRUE ? new DebugConnector() : $sms,
$db
);

/**
* @var CodeGeneratorInterface $codeGenerator
*/
$codeGenerator = new CodeGenerator();

$user = new User($db);

/**
* @var PhonePurifierInterface $phonePurifier
*/
$phonePurifier = new PhonePurifier($configuration->get('countrycode'));

/**
* @var CreditSystemInterface $creditSystem
*/
$creditSystem = (new CreditSystemFactory())->getCreditSystem($configuration->get('credit'), $db);

function error($message)
{
global $db;
Expand Down
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@
"ext-mysqli": "*",
"ext-gettext": "*",
"ext-curl": "*",
"monolog/monolog": "^1.27"
"monolog/monolog": "^1.27",
"symfony/dependency-injection": "^5.4",
"symfony/http-kernel": "^5.4",
"symfony/framework-bundle": "^5.4",
"symfony/http-foundation": "^5.4",
"symfony/config": "^5.4",
"symfony/expression-language": "^5.4",
"symfony/monolog-bundle": "^3.10",
"symfony/console": "^5.4",
"symfony/runtime": "^5.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.10|^4.0",
"phpunit/phpunit": "^9.5",
"php-mock/php-mock-phpunit": "^2.3",
"rector/rector": "^1.1"
},
"config": {
"allow-plugins": {
"symfony/runtime": true
}
}
}
18 changes: 18 additions & 0 deletions config/packages/framework.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework): void {
$framework->session()
->storageFactoryId('session.storage.factory.php_bridge')
->handlerId(null);

$cache = $framework->cache();

$cache->pool('cache.static')
->adapters(['cache.adapter.array'])
->defaultLifetime(180)
->public(true);
};
14 changes: 14 additions & 0 deletions config/packages/monolog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog): void {
$monolog->handler('BikeShare')
->appName('BikeShare')
->type('rotating_file')
->path('%kernel.project_dir%/var/log/log.log')
->maxFiles(30)
->level('debug');
};
92 changes: 92 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use BikeShare\App\Configuration;
use BikeShare\Credit\CodeGenerator\CodeGenerator;
use BikeShare\Credit\CodeGenerator\CodeGeneratorInterface;
use BikeShare\Credit\CreditSystem;
use BikeShare\Credit\CreditSystemInterface;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\Mail\MailSenderInterface;
use BikeShare\Mail\PHPMailerMailSender;
use BikeShare\Purifier\PhonePurifier;
use BikeShare\Purifier\PhonePurifierInterface;
use BikeShare\Rent\RentSystemInterface;
use BikeShare\Sms\SmsSender;
use BikeShare\Sms\SmsSenderInterface;
use BikeShare\SmsConnector\SmsConnectorFactory;
use BikeShare\SmsConnector\SmsConnectorInterface;
use PHPMailer\PHPMailer\PHPMailer;

return static function (ContainerConfigurator $container): void {
$services = $container->services();
$services->defaults()->public()->autoconfigure()->autowire();

$services->instanceof(CreditSystemInterface::class)->tag('creditSystem');
$services->instanceof(RentSystemInterface::class)->tag('rentSystem');
$services->instanceof(MailSenderInterface::class)->tag('mailSender');
$services->instanceof(SmsConnectorInterface::class)->tag('smsConnector');

$services->alias('logger','monolog.logger');

$services->set(Configuration::class)
->args([__DIR__ . '/../config.php']);

$services->load('BikeShare\\', '../src/')
->exclude([
'../src/Db/MysqliDbResult.php',
'../src/SmsConnector/SmsGateway/SmsGateway.php',
'../src/App/Configuration.php',
]);

$services->get(MysqliDb::class)
->args([
expr("service('BikeShare\\\App\\\Configuration').get('dbserver')"),
expr("service('BikeShare\\\App\\\Configuration').get('dbuser')"),
expr("service('BikeShare\\\App\\\Configuration').get('dbpassword')"),
expr("service('BikeShare\\\App\\\Configuration').get('dbname')"),
service('logger'),
false, // throwException
]);

$services->alias(DbInterface::class, MysqliDb::class);

$services->get(PHPMailerMailSender::class)
->args([
expr("service('BikeShare\\\App\\\Configuration').get('systemname')"),
expr("service('BikeShare\\\App\\\Configuration').get('systememail')"),
expr("service('BikeShare\\\App\\\Configuration').get('email')"),
inline_service( PHPMailer::class)->args([false])
]);

$services->get(PhonePurifier::class)
->args([
expr("service('BikeShare\\\App\\\Configuration').get('countrycode')"),
]);

$services->get(CreditSystem::class)
->bind('$creditConfiguration', expr("service('BikeShare\\\App\\\Configuration').get('credit')"));

$services->load('BikeShare\\Rent\\', '../src/Rent')
->bind('$watchesConfig', expr("service('BikeShare\\\App\\\Configuration').get('watches')"))
->bind('$connectorsConfig', expr("service('BikeShare\\\App\\\Configuration').get('connectors')"))
->bind('$forceStack', expr("service('BikeShare\\\App\\\Configuration').get('forceStack')"));

$services->load('BikeShare\\SmsConnector\\', '../src/SmsConnector')
->bind('$config', expr("service('BikeShare\\\App\\\Configuration').get('connectors')"))
->exclude([
'../src/SmsConnector/SmsGateway/SmsGateway.php'
]);
$services->get(SmsConnectorFactory::class)
->arg('$connectorConfig', expr("service('BikeShare\\\App\\\Configuration').get('connectors')"));

$services->alias(SmsSenderInterface::class, SmsSender::class);

$services->alias(CodeGeneratorInterface::class, CodeGenerator::class);

$services->alias(PhonePurifierInterface::class, PhonePurifier::class);
};
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

volumes:
db-data:

Expand Down
Loading

0 comments on commit 0fa45f3

Please sign in to comment.