Commit 2f9059dc authored by Nick Sellen's avatar Nick Sellen

DI refinements and pretty PDO DebugBar

parent 0213dcf1
Pipeline #17388174 failed with stage
in 5 minutes and 6 seconds
......@@ -39,5 +39,10 @@ modules:
dsn: mysql:host=db;dbname=foodsharing
user: root
password: root
\Helper\Container:
# custom module that inherits from Db so also needs db config
dsn: mysql:host=db;dbname=foodsharing
user: root
password: root
\Helper\Maildev:
url: http://maildev:80
......@@ -4,7 +4,6 @@ if (php_sapi_name() != 'cli') {
/* Whoops catches all error messages in CLI mode as well :( */
Foodsharing\Debug\Whoops::register();
}
Foodsharing\Debug\DebugBar::register();
$protocol = 'http';
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
......
......@@ -5,6 +5,9 @@
// TODO: maybe have a default env?
// TODO: check if there is not already a concept of app environment elsewhere
use DebugBar\DataCollector\PDO\TraceablePDO;
use Foodsharing\DI;
$FS_ENV = getenv('FS_ENV');
$env_filename = 'config.inc.' . $FS_ENV . '.php';
......@@ -39,3 +42,16 @@ if (defined('SENTRY_URL')) {
}
define('FPDF_FONTPATH', __DIR__ . '/lib/font/');
if ($FS_ENV === 'dev') {
// In development we need to wrap the PDO instance for the DebugBar
$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_DB, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$traceablePDO = new TraceablePDO($pdo);
DI::useTraceablePDO($traceablePDO);
Foodsharing\Debug\DebugBar::register($traceablePDO);
} else {
DI::useDefaultPDO();
}
DI::compile();
......@@ -4,14 +4,6 @@ services:
autoconfigure: true
public: true
# TODO: put this into code somewhere, so we can use the parameters from the config.inc.*.php files
pdo:
class: PDO
arguments:
- mysql:host=db;dbname=foodsharing
- root
- root
Foodsharing\:
resource: '../src/*'
exclude: '../src/{Lib}'
<?php
require __DIR__ . '/../vendor/autoload.php';
// Move this into a better place!
$pdo = new PDO('mysql:host=db;dbname=foodsharing', 'root', 'root', []);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.yaml');
$container->compile();
......@@ -3,6 +3,7 @@
use Flourish\fDate;
use Flourish\fFile;
use Flourish\fImage;
use Foodsharing\DI;
use Foodsharing\Lib\Db\ManualDb;
use Foodsharing\Lib\Db\Mem;
use Foodsharing\Lib\Mail\AsyncMail;
......@@ -1541,11 +1542,10 @@ function getFqcnPrefix($module)
function loadApp($app)
{
global $container;
$className = $app . 'Control';
$fqcn = getFqcnPrefix($app) . $className;
$appInstance = $container->get(ltrim($fqcn, '\\'));
$appInstance = DI::get(ltrim($fqcn, '\\'));
if (isset($_GET['a']) && method_exists($appInstance, $_GET['a'])) {
$meth = $_GET['a'];
......
<?php
namespace Foodsharing;
use DebugBar\DataCollector\PDO\TraceablePDO;
use PDO;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class DI
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
private static $container;
public static function init() {
DI::$container = new ContainerBuilder();
$loader = new YamlFileLoader(DI::$container, new FileLocator(__DIR__));
$loader->load('../includes/services.yaml');
}
public static function useTraceablePDO($traceablePDO) {
DI::$container->set(PDO::class, $traceablePDO);
DI::$container->register(PDO::class, TraceablePDO::class);
}
public static function useDefaultPDO() {
DI::$container
->register(\PDO::class, \PDO::class)
->addArgument('mysql:host=' . DB_HOST . ';dbname=' . DB_DB)
->addArgument(DB_USER)
->addArgument(DB_PASS)
->addMethodCall('setAttribute', [PDO::ATTR_EMULATE_PREPARES, false]);
}
public static function compile() {
DI::$container->compile();
}
/**
* @throws \Exception
*/
public static function get($id)
{
return DI::$container->get($id);
}
}
DI::init();
<?php
namespace Foodsharing\Debug\Collectors;
use DebugBar\DataCollector\PDO\PDOCollector;
class PrettierPDOCollector extends PDOCollector
{
public function collect()
{
$data = parent::collect();
foreach ($data['statements'] as $i => $statement) {
$data['statements'][$i]['sql'] = $this->formatQueryStr($statement['sql']);
}
return $data;
}
private function formatQueryStr($str)
{
return strtr(preg_replace("/^\W+/", '', $str), ["\t" => '', "\r" => '', "\n" => ' ']);
}
}
......@@ -4,9 +4,11 @@ namespace Foodsharing\Debug;
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PDO\TraceablePDO;
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\RequestDataCollector;
use Foodsharing\Debug\Collectors\DatabaseQueryCollector;
use Foodsharing\Debug\Collectors\PrettierPDOCollector;
/**
* A static helper class to wrap our use of \DebugBar.
......@@ -27,7 +29,7 @@ class DebugBar
/* @var $debugbar \DebugBar\StandardDebugBar */
private static $debugbar;
public static function register()
public static function register(TraceablePDO $traceablePDO)
{
self::$debugbar = new \DebugBar\DebugBar();
......@@ -39,9 +41,9 @@ class DebugBar
self::$queryCollector = new DatabaseQueryCollector();
self::$debugbar->addCollector(self::$queryCollector);
global $pdo;
$pdo = new \DebugBar\DataCollector\PDO\TraceablePDO($pdo);
self::$debugbar->addCollector(new \DebugBar\DataCollector\PDO\PDOCollector($pdo));
$pdoCollector = new PrettierPDOCollector($traceablePDO);
$pdoCollector->setRenderSqlWithParams(true, '');
self::$debugbar->addCollector($pdoCollector);
self::$initialized = true;
}
......
......@@ -2,12 +2,15 @@
namespace Helper;
use Foodsharing\DI;
use PDO;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class Container extends \Codeception\Module
class Container extends \Codeception\Module\Db
{
private $container;
public function __construct($moduleContainer, $config = null)
......@@ -16,11 +19,17 @@ class Container extends \Codeception\Module
$this->container = new ContainerBuilder();
$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__));
$loader->load('../../../includes/services.yaml');
$this->container
->register(\PDO::class, \PDO::class)
->addArgument($this->config['dsn'])
->addArgument($this->config['user'])
->addArgument($this->config['password'])
->addMethodCall('setAttribute', [PDO::ATTR_EMULATE_PREPARES, false]);
$this->container->compile();
}
public function container()
public function get($id)
{
return $this->container;
return $this->container->get($id);
}
}
......@@ -15,7 +15,7 @@ class BasketGatewayTest extends \Codeception\Test\Unit
protected function _before()
{
$this->gateway = $this->tester->container()->get(\Foodsharing\Modules\Basket\BasketGateway::class);
$this->gateway = $this->tester->get(\Foodsharing\Modules\Basket\BasketGateway::class);
$this->foodsaver = $this->tester->createFoodsaver();
}
......
......@@ -15,7 +15,7 @@ class ContentGatewayTest extends \Codeception\Test\Unit
protected function _before()
{
$this->gateway = $this->tester->container()->get(\Foodsharing\Modules\Content\ContentGateway::class);
$this->gateway = $this->tester->get(\Foodsharing\Modules\Content\ContentGateway::class);
}
public function testGetContent()
......
......@@ -15,14 +15,10 @@ class DashboardGatewayTest extends \Codeception\Test\Unit
protected function _before()
{
$this->gateway = $this->tester->container()->get(\Foodsharing\Modules\Dashboard\DashboardGateway::class);
$this->gateway = $this->tester->get(\Foodsharing\Modules\Dashboard\DashboardGateway::class);
$this->foodsaver = $this->tester->createFoodsaver();
}
protected function _after()
{
}
public function testGetNewestFoodBaskets()
{
$this->assertEquals([], $this->gateway->getNewestFoodbaskets(10));
......
<?php
use Foodsharing\DI;
use Foodsharing\Lib\Routing;
use Foodsharing\Lib\Session\S;
use Foodsharing\Lib\Xhr\XhrResponses;
require __DIR__ . '/includes/setup.php';
global $container;
$js = '';
if (isset($_GET['app']) && isset($_GET['m'])) {
$app = str_replace('/', '', $_GET['app']);
......@@ -22,7 +21,7 @@ if (isset($_GET['app']) && isset($_GET['m'])) {
S::init();
$class = Routing::getClassName($app, 'Xhr');
$obj = $container->get(ltrim($class, '\\'));
$obj = DI::get(ltrim($class, '\\'));
if (method_exists($obj, $meth)) {
$out = $obj->$meth();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment