-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from guillaume-ro-fr/master
Add PHP Attributes compatibility
- Loading branch information
Showing
10 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
|
||
namespace Farmatholin\SegmentIoBundle\Configuration; | ||
|
||
use Doctrine\Common\Annotations\Annotation; | ||
use Doctrine\Common\Annotations\Annotation\Required; | ||
|
||
/** | ||
|
@@ -22,9 +21,9 @@ | |
* @author Vladislav Marin <[email protected]> | ||
* | ||
* @Annotation | ||
* | ||
* @Target("METHOD") | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
class Page implements AnalyticsInterface | ||
{ | ||
|
||
|
@@ -33,20 +32,40 @@ class Page implements AnalyticsInterface | |
* | ||
* @var string | ||
*/ | ||
public $category; | ||
public string $category; | ||
|
||
/** | ||
* @Required | ||
* | ||
* @var string | ||
*/ | ||
public $name; | ||
public string $name; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public array $properties = []; | ||
|
||
/** | ||
* @param string $category | ||
*/ | ||
public function __construct( | ||
$category = null, | ||
string $name = null, | ||
array $properties = [] | ||
) { | ||
if (is_array($category)) { | ||
// Doctrine annotation | ||
$this->category = $category['category']; | ||
$this->name = $category['name']; | ||
$this->properties = $category['properties'] ?? $this->properties; | ||
} else { | ||
// PHP Attributes | ||
$this->category = $category; | ||
$this->name = $name; | ||
$this->properties = $properties; | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,11 @@ | |
|
||
namespace Farmatholin\SegmentIoBundle\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
|
||
|
@@ -23,7 +26,7 @@ | |
* | ||
* @author Vladislav Marin <[email protected]> | ||
*/ | ||
class SegmentIoExtension extends Extension | ||
class SegmentIoExtension extends Extension implements CompilerPassInterface | ||
{ | ||
/** | ||
* @see https://segment.com/docs/connections/data-residency/ | ||
|
@@ -48,6 +51,12 @@ public function load(array $configs, ContainerBuilder $container) | |
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('services.xml'); | ||
|
||
$loader->load('annotations.xml'); | ||
if (PHP_VERSION_ID >= 80000) { | ||
// PHP Attributes | ||
$loader->load('attributes.xml'); | ||
} | ||
|
||
if (isset(static::DATA_RESIDENCIES[$config['data_residency']]) && !isset($config['options']['host'])) { | ||
$config['options']['host'] = static::DATA_RESIDENCIES[$config['data_residency']]; | ||
} | ||
|
@@ -58,4 +67,16 @@ public function load(array $configs, ContainerBuilder $container) | |
$container->setParameter('farma.segment_io_env', $config['env']); | ||
$container->setParameter('farma.segment_io_options', $config['options']); | ||
} | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
// Doctrine annotations can be disabled by Symfony Framework or if doctrine/annotations is missing | ||
try { | ||
$container->findDefinition('annotation_reader'); | ||
} catch (ServiceNotFoundException $ignored) { | ||
// Remove Doctrine annotation listener on Kernel request | ||
$container->getDefinition('farma.segment_io.annotation_listener') | ||
->clearTag('kernel.event_listener'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */ | ||
|
||
namespace Farmatholin\SegmentIoBundle\EventListener; | ||
|
||
use Farmatholin\SegmentIoBundle\Configuration\Page; | ||
use Farmatholin\SegmentIoBundle\Configuration\Track; | ||
use Farmatholin\SegmentIoBundle\Util\SegmentIoProvider; | ||
use Symfony\Component\HttpKernel\Event\ControllerEvent; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
|
||
class AttributeListener | ||
{ | ||
protected SegmentIoProvider $segmentIoProvider; | ||
protected TokenStorageInterface $tokenStorage; | ||
protected string $guestId; | ||
|
||
/** | ||
* AttributeListener constructor. | ||
* | ||
* @param TokenStorageInterface $tokenStorage | ||
* @param SegmentIoProvider $segmentIoProvider | ||
* @param string $guestId | ||
*/ | ||
public function __construct(TokenStorageInterface $tokenStorage, SegmentIoProvider $segmentIoProvider, string $guestId) | ||
{ | ||
$this->segmentIoProvider = $segmentIoProvider; | ||
$this->tokenStorage = $tokenStorage; | ||
$this->guestId = $guestId; | ||
} | ||
|
||
public function onKernelController(ControllerEvent $event): void | ||
{ | ||
$controller = $event->getController(); | ||
|
||
if (!is_array($controller)) { | ||
return; | ||
} | ||
|
||
[$controllerObject, $methodName] = $controller; | ||
|
||
$controllerReflectionObject = new \ReflectionObject($controllerObject); | ||
$reflectionMethod = $controllerReflectionObject->getMethod($methodName); | ||
|
||
$userId = $this->getUserId(); | ||
foreach ($reflectionMethod->getAttributes(Page::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { | ||
/** @var Page $page */ | ||
$page = $attribute->newInstance(); | ||
|
||
$message = $page->getMessage(); | ||
$message['userId'] = $userId; | ||
|
||
$this->segmentIoProvider->page($message); | ||
|
||
$this->segmentIoProvider->flush(); | ||
} | ||
foreach ($reflectionMethod->getAttributes(Track::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { | ||
/** @var Track $track */ | ||
$track = $attribute->newInstance(); | ||
|
||
$message = $track->getMessage(); | ||
$message['userId'] = $userId; | ||
|
||
$this->segmentIoProvider->track($message); | ||
|
||
$this->segmentIoProvider->flush(); | ||
} | ||
} | ||
|
||
/** | ||
* @return string|null | ||
* | ||
* @throws \ReflectionException | ||
*/ | ||
private function getUserId(): ?string | ||
{ | ||
if (null === $token = $this->tokenStorage->getToken()) { | ||
return $this->guestId; | ||
} | ||
|
||
if (!is_object($user = $token->getUser())) { | ||
return $this->guestId; | ||
} | ||
|
||
$reflect = new \ReflectionClass($user); | ||
if ($reflect->hasMethod('getId')) { | ||
$userId = $user->getId(); | ||
if (null !== $userId) { | ||
return $userId; | ||
} | ||
} | ||
|
||
return $this->guestId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="farma.segment_io.event_listener.annotation_listener.class">Farmatholin\SegmentIoBundle\EventListener\AnnotationListener</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="farma.segment_io.annotation_listener" class="%farma.segment_io.event_listener.annotation_listener.class%"> | ||
<argument type="service" id="annotation_reader" on-invalid="ignore" /> | ||
<argument type="service" id="security.token_storage" /> | ||
<argument type="service" id="segment_io.analytics"/> | ||
<argument>%farma.segment_io_guest_id%</argument> | ||
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController"/> | ||
</service> | ||
</services> | ||
|
||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="farma.segment_io.event_listener.attribute_listener.class">Farmatholin\SegmentIoBundle\EventListener\AttributeListener</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="farma.segment_io.attribute_listener" class="%farma.segment_io.event_listener.attribute_listener.class%"> | ||
<argument type="service" id="security.token_storage" /> | ||
<argument type="service" id="segment_io.analytics"/> | ||
<argument>%farma.segment_io_guest_id%</argument> | ||
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController"/> | ||
</service> | ||
</services> | ||
|
||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters