diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..485dee64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php b/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php index 37944157..0382b7d0 100644 --- a/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php +++ b/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php @@ -4,29 +4,28 @@ namespace CodelyTv\Application; -use CodelyTv\Debug; use CodelyTv\Email\EmailNotifier; -use CodelyTv\FeatureFlags; +use CodelyTv\FeatureFlagsInmutable; use CodelyTv\Flags; use CodelyTv\Persistence\MySqlConnection; final class Subscribe { + public function __construct(private MySqlConnection $connection, private FeatureFlagsInmutable $featureFlags, private EmailNotifier $emailNotifier) { + + } + public function __invoke(string $email, ?string $name = null): void { - if (Debug::instance()->isDebugModeEnabled()) { - FeatureFlags::instance()->deactivateAll(); - } - - $flag = FeatureFlags::instance()->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); + $flag = $this->featureFlags->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); if ($flag) { // The new subscription added a "name" field - MySqlConnection::instance()->persist($email, $name); + $this->connection->persist($email, $name); } else { - MySqlConnection::instance()->persist($email); + $this->connection->persist($email); } - EmailNotifier::instance()->sendSubscriptionEmail($email); + $this->emailNotifier->sendSubscriptionEmail($email); } } diff --git a/examples/php/php-feature_flags-01_base/src/Debug.php b/examples/php/php-feature_flags-01_base/src/Debug.php index 564bcb0b..9d29dabe 100644 --- a/examples/php/php-feature_flags-01_base/src/Debug.php +++ b/examples/php/php-feature_flags-01_base/src/Debug.php @@ -6,29 +6,12 @@ final class Debug { - private static ?Debug $instance = null; - - private bool $debugMode = false; - - private function __construct() - { - } - - public static function instance(): Debug - { - if (!self::$instance) { - self::$instance = new self(); - } - return self::$instance; - } - - public function enableDebugMode(): void + public function __construct(private bool $debugMode) { - $this->debugMode = true; } public function isDebugModeEnabled(): bool { - return $this->debugMode; + return $this->debugMode; } } diff --git a/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php b/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php index 85d11c92..6bfe5604 100644 --- a/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php +++ b/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php @@ -4,24 +4,17 @@ namespace CodelyTv\Email; +use CodelyTv\FeatureFlagsInmutable; + final class EmailNotifier { - private static ?EmailNotifier $instance = null; - - private function __construct() - { - } - - public static function instance(): EmailNotifier + public function __construct(private FeatureFlagsInmutable $flags) { - if (!self::$instance) { - self::$instance = new self(); - } - return self::$instance; } public function sendSubscriptionEmail(string $to) { + $flags = $this->flags; echo "Email sent to $to"; require __DIR__ . '/subscription-email.php'; } diff --git a/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php b/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php index 17ea3406..12b08ac9 100644 --- a/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php +++ b/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php @@ -1,5 +1,6 @@
Message for all subscribers
- -get(\CodelyTv\Flags::NEW_SUBSCRIPTION_PAGE_NAME)): ?> +get(\CodelyTv\Flags::NEW_SUBSCRIPTION_PAGE_NAME)): ?>Additional message for subscribers with active flag
diff --git a/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php b/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php new file mode 100644 index 00000000..1b235b38 --- /dev/null +++ b/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php @@ -0,0 +1,19 @@ +flags)) { + return false; + } + + return $this->flags[$flagName]; + } +} diff --git a/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php b/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php index 44545f6d..5b481de5 100644 --- a/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php +++ b/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php @@ -4,30 +4,20 @@ namespace CodelyTv\Persistence; -use CodelyTv\FeatureFlags; +use CodelyTv\FeatureFlagsInmutable; use CodelyTv\Flags; final class MySqlConnection { - private static ?MySqlConnection $instance = null; - - private function __construct() + public function __construct(private FeatureFlagsInmutable $flags) { } - public static function instance(): MySqlConnection - { - if (!self::$instance) { - self::$instance = new self(); - } - return self::$instance; - } - public function persist(string $email, ?string $name = null): void { $subscription = ['email' => $email]; - $flag = FeatureFlags::instance()->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); + $flag = $this->flags->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); if ($flag) { $subscription['name'] = $name; } diff --git a/examples/php/php-feature_flags-01_base/src/SubscribeController.php b/examples/php/php-feature_flags-01_base/src/SubscribeController.php index 7847e235..398378b5 100644 --- a/examples/php/php-feature_flags-01_base/src/SubscribeController.php +++ b/examples/php/php-feature_flags-01_base/src/SubscribeController.php @@ -5,22 +5,43 @@ namespace CodelyTv; use CodelyTv\Application\Subscribe; +use CodelyTv\Email\EmailNotifier; +use CodelyTv\Persistence\MySqlConnection; use Symfony\Component\HttpFoundation\Request; final class SubscribeController { - public function __invoke(Request $request) + public function __construct(private Debug $debug) { - $flagHeader = $request->headers->get('X-FLAG'); - if ($flagHeader === Flags::NEW_SUBSCRIPTION_PAGE_TOKEN) { - FeatureFlags::instance()->set('new_subscription_page', true); - } + } + + public function __invoke(Request $request) + { + $flags = $this->getFeatures($request, $this->debug); + $subscribeUseCase = new Subscribe( + new MySqlConnection($flags), + $flags, + new EmailNotifier($flags) + ); - $subscribeUseCase = new Subscribe(); $subscribeUseCase->__invoke( $request->request->get('email'), $request->request->get('name'), ); } + + private function getFeatures(Request $request, Debug $debug): FeatureFlagsInmutable + { + $result = new FeatureFlagsInmutable(); + if ($debug->isDebugModeEnabled()) { + return $result; + } + + $flagHeader = $request->headers->get('X-FLAG'); + if ($flagHeader === Flags::NEW_SUBSCRIPTION_PAGE_TOKEN) { + return new FeatureFlagsInmutable(['new_subscription_page' => true]); + } + return $result; + } } diff --git a/examples/php/php-feature_flags-01_base/src/index.php b/examples/php/php-feature_flags-01_base/src/index.php index 21708413..4c81c5a3 100644 --- a/examples/php/php-feature_flags-01_base/src/index.php +++ b/examples/php/php-feature_flags-01_base/src/index.php @@ -8,12 +8,12 @@ use CodelyTv\SubscribeController; use Symfony\Component\HttpFoundation\Request; -Debug::instance()->enableDebugMode(); +$debug = new Debug(true); $request = Request::createFromGlobals(); $request->headers->set('X-FLAG', Flags::NEW_SUBSCRIPTION_PAGE_TOKEN); $request->request->set('email', 'email@example.com'); $request->request->set('name', 'Codely'); -$controller = new SubscribeController(); +$controller = new SubscribeController($debug); $controller->__invoke($request);