diff --git a/src/DI/NewRelicExtension.php b/src/DI/NewRelicExtension.php index 9e977b6..f711013 100644 --- a/src/DI/NewRelicExtension.php +++ b/src/DI/NewRelicExtension.php @@ -12,8 +12,7 @@ use Contributte\NewRelic\Config\TransactionTracerConfig; use Contributte\NewRelic\Environment; use Contributte\NewRelic\Formatters\DefaultWebTransactionNameFormatter; -use Contributte\NewRelic\RUM\FooterControl; -use Contributte\NewRelic\RUM\HeaderControl; +use Contributte\NewRelic\RUM\RUMControlFactory; use Contributte\NewRelic\Tracy\Logger; use Nette\DI\CompilerExtension; use Nette\DI\Definitions\ServiceDefinition; @@ -217,14 +216,8 @@ private function setupRUM(): void $rumEnabled = $this->enabled && $config->rum->enabled === true; - $builder->addDefinition($this->prefix('rum.headerControl')) - ->setFactory(HeaderControl::class, [ - '@' . $this->prefix('agent'), - $rumEnabled, - ]); - - $builder->addDefinition($this->prefix('rum.footerControl')) - ->setFactory(FooterControl::class, [ + $builder->addDefinition($this->prefix('rum.controlFactory')) + ->setFactory(RUMControlFactory::class, [ '@' . $this->prefix('agent'), $rumEnabled, ]); diff --git a/src/RUM/FooterControl.php b/src/RUM/FooterControl.php index b298700..853d4d2 100644 --- a/src/RUM/FooterControl.php +++ b/src/RUM/FooterControl.php @@ -23,19 +23,13 @@ class FooterControl extends Control /** * @var bool */ - private $withScriptTag = true; + private $withScriptTag; - public function __construct(Agent $agent, bool $enabled = true) + public function __construct(Agent $agent, bool $enabled = true, bool $withScriptTag = true) { $this->agent = $agent; $this->enabled = $enabled; - } - - public function disableScriptTag(): self - { - $this->withScriptTag = false; - - return $this; + $this->withScriptTag = $withScriptTag; } public function render(): void diff --git a/src/RUM/HeaderControl.php b/src/RUM/HeaderControl.php index 545ee6e..82d2b9c 100644 --- a/src/RUM/HeaderControl.php +++ b/src/RUM/HeaderControl.php @@ -23,19 +23,13 @@ class HeaderControl extends Control /** * @var bool */ - private $withScriptTag = true; + private $withScriptTag; - public function __construct(Agent $agent, bool $enabled = true) + public function __construct(Agent $agent, bool $enabled = true, bool $withScriptTag = true) { $this->agent = $agent; $this->enabled = $enabled; - } - - public function disableScriptTag(): self - { - $this->withScriptTag = false; - - return $this; + $this->withScriptTag = $withScriptTag; } public function render(): void diff --git a/src/RUM/RUMControlFactory.php b/src/RUM/RUMControlFactory.php new file mode 100644 index 0000000..be04f8c --- /dev/null +++ b/src/RUM/RUMControlFactory.php @@ -0,0 +1,38 @@ +agent = $agent; + $this->enabled = $enabled; + } + + public function createHeader(bool $withScriptTag = true): HeaderControl + { + return new HeaderControl($this->agent, $this->enabled, $withScriptTag); + } + + public function createFooter(bool $withScriptTag = true): FooterControl + { + return new FooterControl($this->agent, $this->enabled, $withScriptTag); + } + +} diff --git a/src/RUM/RUMControlTrait.php b/src/RUM/RUMControlTrait.php new file mode 100644 index 0000000..d4b1890 --- /dev/null +++ b/src/RUM/RUMControlTrait.php @@ -0,0 +1,30 @@ +rumControlFactory = $rumControlFactory; + } + + protected function createComponentNewRelicHeader(): HeaderControl + { + return $this->rumControlFactory->createHeader(); + } + + protected function createComponentNewRelicFooter(): FooterControl + { + return $this->rumControlFactory->createFooter(); + } + +} diff --git a/tests/Cases/DI/NewRelicExtensionTest.phpt b/tests/Cases/DI/NewRelicExtensionTest.phpt index 269b698..965104c 100644 --- a/tests/Cases/DI/NewRelicExtensionTest.phpt +++ b/tests/Cases/DI/NewRelicExtensionTest.phpt @@ -11,6 +11,7 @@ use Contributte\NewRelic\DI\NewRelicConsoleExtension; use Contributte\NewRelic\DI\NewRelicExtension; use Contributte\NewRelic\RUM\FooterControl; use Contributte\NewRelic\RUM\HeaderControl; +use Contributte\NewRelic\RUM\RUMControlFactory; use Contributte\NewRelic\Tracy\Logger; use ContributteTests\NewRelic\Libs\TestCase; use ContributteTests\NewRelic\Mocks\ApplicationExtension; @@ -83,8 +84,9 @@ final class NewRelicExtensionTest extends TestCase Assert::type(Logger::class, $container->getService('newrelic.logger')); Assert::type(OnRequestCallback::class, $container->getService('newrelic.onRequestCallback')); Assert::type(OnErrorCallback::class, $container->getService('newrelic.onErrorCallback')); - Assert::type(HeaderControl::class, $container->getService('newrelic.rum.headerControl')); - Assert::type(FooterControl::class, $container->getService('newrelic.rum.footerControl')); + Assert::type(RUMControlFactory::class, $container->getService('newrelic.rum.controlFactory')); + Assert::type(HeaderControl::class, $container->getService('newrelic.rum.controlFactory')->createHeader()); + Assert::type(FooterControl::class, $container->getService('newrelic.rum.controlFactory')->createFooter()); } } diff --git a/tests/phpcs-ruleset.xml b/tests/phpcs-ruleset.xml index 32aed20..23f6e1b 100644 --- a/tests/phpcs-ruleset.xml +++ b/tests/phpcs-ruleset.xml @@ -4,6 +4,9 @@ + + /src/RUM/RUMControlTrait.php + /src/Tracy/Logger.php