diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 0c7581e7c0b7d..d4fbc0d57da1e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 3.4.0 ----- + * Added `DotenvDataCollector` to collect envs loaded by the Dotenv component * Deprecated `profiler.matcher` option * Added support for `EventSubscriberInterface` on `MicroKernelTrait` * Removed `doctrine/cache` from the list of required dependencies in `composer.json` diff --git a/src/Symfony/Bundle/FrameworkBundle/DataCollector/DotenvDataCollector.php b/src/Symfony/Bundle/FrameworkBundle/DataCollector/DotenvDataCollector.php new file mode 100644 index 0000000000000..8f29884490ffe --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/DataCollector/DotenvDataCollector.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\DataCollector; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\DataCollector\DataCollector; + +/** + * Collects environment variables loaded by the Dotenv component. + * + * @author Oleg Voronkovich + */ +class DotenvDataCollector extends DataCollector +{ + /** + * {@inheritdoc} + */ + public function collect(Request $request, Response $response, \Exception $exception = null) + { + $this->data['envs'] = array(); + + $loadedVars = array_filter(explode(',', getenv('SYMFONY_DOTENV_VARS'))); + + foreach ($loadedVars as $var) { + if (false !== getenv($var)) { + $this->data['envs'][$var] = getenv($var); + } + } + } + + /** + * Gets loaded environment variables. + * + * @return array + */ + public function getEnvs() + { + return $this->data['envs']; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'dotenv'; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.xml index a1b73097e0bd7..011a71ed8963d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.xml @@ -51,5 +51,9 @@ + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DataCollector/DotenvDataCollectorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DataCollector/DotenvDataCollectorTest.php new file mode 100644 index 0000000000000..c4d4c725ffdad --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DataCollector/DotenvDataCollectorTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\DataCollector; + +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\DataCollector\DotenvDataCollector; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + +class DotenvDataCollectorTest extends TestCase +{ + public function testCollect() + { + putenv('SYMFONY_DOTENV_VARS=APP_DEBUG,DATABASE_URL,DELETED_VAR'); + + putenv('APP_DEBUG=1'); + putenv('DATABASE_URL=sqlite:///var/data/db.sqlite'); + putenv('DELETED_VAR'); + + $collector = new DotenvDataCollector(); + $collector->collect(new Request(), new Response()); + + $this->assertEquals(array('APP_DEBUG' => '1', 'DATABASE_URL' => 'sqlite:///var/data/db.sqlite'), $collector->getEnvs()); + } + + public function testSpecialVariableNotExists() + { + putenv('SYMFONY_DOTENV_VARS'); + + $collector = new DotenvDataCollector(); + $collector->collect(new Request(), new Response()); + + $this->assertEmpty($collector->getEnvs()); + } +} diff --git a/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md b/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md index c1259b0a20e1d..c0a1adc56c92a 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 3.4.0 ----- + * added a pane with envs loaded by the Dotenv component * Deprecated the `web_profiler.position` config option (in 4.0 version the toolbar will always be displayed at the bottom) and the `web_profiler.debug_toolbar.position` container parameter. diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig index 5fe45a0f4ed39..9b51bc357e2e6 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig @@ -249,6 +249,24 @@ + {% if profile.hasCollector('dotenv') %} +
+

.env

+ +
+

Loaded variables

+ + {% if profile.collector('dotenv').envs is empty %} +
+

No variables were loaded.

+
+ {% else %} + {{ include('@WebProfiler/Profiler/table.html.twig', { data: profile.collector('dotenv').envs }, with_context = false) }} + {% endif %} +
+
+ {% endif %} + {% if profile.parent %}

Parent Request