Skip to content

Commit

Permalink
v1.0.5 Update init twig engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolgov_M committed Feb 7, 2018
1 parent 8affb2f commit 358096a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bi0r0b0t/silex-pinba-provider",
"description": "Provide PinbaBundle for Silex",
"minimum-stability": "stable",
"version": "1.0.4",
"version": "1.0.5",
"license": "MIT",
"authors": [
{
Expand Down
19 changes: 8 additions & 11 deletions src/SilexPinbaProvider/SilexPinbaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ public function register(Container $app)

$app['twig.environment_factory'] = $app->protect(function ($app) {
$className = $app['intaro_pinba.templating.engine.twig.class'];
return new $className($app['twig.loader'], $app['twig.options']);
$twig = new $className($app['twig.loader'], $app['twig.options']);
if($twig instanceof TimedTwigEnvironment) {
$twig
->setStopwatch( $app['intaro_pinba.stopwatch'])
->setServerName($app['intaro_pinba.server.name'])
;
}
return $twig;
});
}

Expand All @@ -84,16 +91,6 @@ public function register(Container $app)
*/
public function boot(Application $app)
{
$app->extend('twig', function (\Twig_Environment $twig) use ($app) {
if($twig instanceof TimedTwigEnvironment) {
$twig
->setStopwatch( $app['intaro_pinba.stopwatch'])
->setServerName($app['intaro_pinba.server.name'])
;
}
return $twig;
});

if (!function_exists('pinba_script_name_set') || PHP_SAPI === 'cli' || !$app['intaro_pinba.script_name_configure.enable']) {
return;
}
Expand Down
63 changes: 59 additions & 4 deletions src/SilexPinbaProvider/Test/PinbaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Intaro\PinbaBundle\Stopwatch\Stopwatch;
use PHPUnit\Framework\TestCase;
use Psr\Log\AbstractLogger;
use Silex\Provider\TwigServiceProvider;
use SilexPinbaProvider\SilexPinbaProvider;
use Silex\Application;
Expand All @@ -18,10 +19,17 @@ class PinbaTest extends TestCase
public function testTwigExtension()
{


global $app;
$storage = new \ArrayObject();

$app = new ApplicationEmulator();
$app = new ApplicationEmulator();
$emulate = false;
if(!function_exists('pinba_script_name_set')) {
$emulate = true;
$app['pinba_logger'] = function () {
return new PinbaLogger();
};
require __DIR__.'/../pinba_emulator.php';
}
$app
->register(new TwigServiceProvider(),array(
'twig.templates' => array('hello' => 'Hello {{ name }}!'),
Expand All @@ -30,9 +38,28 @@ public function testTwigExtension()

$app['intaro_pinba.stopwatch.class'] = 'SilexPinbaProvider\Test\StopwatchEmulate';
$app->boot();
$app['intaro_pinba.stopwatch'] -> setStorage($storage);
/**
* @var $stopwatch StopwatchEmulate
*/
$stopwatch = $app['intaro_pinba.stopwatch'];
$this->assertTrue($stopwatch instanceof Stopwatch);
$stopwatch-> setStorage($storage);
$app->renderView('hello');
$this->assertTrue(is_array($storage['tags']), var_export($storage, true));
if($emulate) {
/**
* @var $logger PinbaLogger
*/
$logger = $app['pinba_logger'];
$this->assertTrue($logger instanceof PinbaLogger);
$stack = $logger->getLogStack();
$this->assertTrue(is_array($stack));
$this->assertNotEmpty($stack);
$expected = [
['debug', 'pinba_get_info', []],
];
$this->assertEquals($expected, $stack);
}
}
}

Expand Down Expand Up @@ -98,4 +125,32 @@ public function stop()
class ApplicationEmulator extends Application
{
use Application\TwigTrait;
}

class PinbaLogger extends AbstractLogger {

private $logStack = [];

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = array())
{
$this->logStack[] = [$level, $message, $context];
}

/**
* @return array
*/
public function getLogStack()
{
return $this->logStack;
}

}

0 comments on commit 358096a

Please sign in to comment.