Skip to content

Commit

Permalink
Merge pull request #5 from clearlyip/feature/v3
Browse files Browse the repository at this point in the history
  • Loading branch information
tm1000 committed Mar 18, 2024
2 parents e23f43f + c8c9692 commit c3ab321
Show file tree
Hide file tree
Showing 38 changed files with 1,436 additions and 296 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/tests export-ignore
/workbench export-ignore
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/vendor/
vendor
# Before you hate on me for this https://stackoverflow.com/a/21589454
composer.lock
package-lock.json
/node_modules/
node_modules
output
.env
.phpunit.cache
package-lock.json
19 changes: 15 additions & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"trailingCommaPHP": true,
"singleQuote": true,
"useTabs": false,
"braceStyle": "psr-2"
"tabWidth": 4,
"singleQuote": true,
"useTabs": false,
"plugins": ["@prettier/plugin-php"],
"overrides": [
{
"files": ["*.php"],
"options": {
"parser": "php",
"phpVersion": "8.2",
"trailingCommaPHP": true,
"braceStyle": "psr-2"
}
}
]
}
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9001
}
]
}
33 changes: 23 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
{
// I recommend to disable VS Code's built-in PHP IntelliSense by setting php.suggest.basic to false to avoid duplicate suggestions.
"php.suggest.basic": false,
"editor.formatOnSave": true,
"[php]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"workbench.colorCustomizations": {},
"taskExplorer.pathToMake": "make",
"coverage-gutters.showLineCoverage": true,
"coverage-gutters.showRulerCoverage": true,
// I recommend to disable VS Code's built-in PHP IntelliSense by setting php.suggest.basic to false to avoid duplicate suggestions.
"php.suggest.basic": false,
"editor.formatOnSave": true,
"[php]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"workbench.colorCustomizations": {},
"psalm.configPaths": ["psalm.xml"],
"psalm.psalmScriptPath": "vendor/bin/psalm-language-server",
"phpCodeSniffer.exec.linux": "vendor/bin/phpcs",
"phpCodeSniffer.standard": "Custom",
"phpCodeSniffer.standardCustom": "phpcs.xml",
"psalm.psalmScriptArgs": [
"--on-change-debounce-ms=500",
"--show-diagnostic-warnings=false"
],
"editor.rulers": [80],
"intelephense.environment.phpVersion": "8.2.0",
"coverage-gutters.showLineCoverage": true,
"coverage-gutters.showRulerCoverage": true,
"coverage-gutters.coverageBaseDir": "output/**",
"coverage-gutters.remotePathResolve": ["./"],
"prettier.documentSelectors": ["**/*.php", "**/*.blade.php"]
}
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ Laravel-flagsmith was created by, and is maintained by **[Andrew Nagy](https://g

## Features

- Provides a trait to be able to get features based on Laravel Users ([Flagsmith Identities](https://docs.flagsmith.com/basic-features/managing-identities))
- Utilizes [Laravel's Queue](https://laravel.com/docs/8.x/queues) system to update features in the background
- Utilizes [Laravel's Cache](https://laravel.com/docs/8.x/cache) system to store features in a cache for quick access
- Utilizes [Laravel's Task Scheduling](https://laravel.com/docs/8.x/scheduling) system to update features on a schedule
- Adds a route to utilize [Flagsmith's webhooks](https://docs.flagsmith.com/advanced-use/system-administration) to update the cache when features change
- Provides a trait to be able to get flags based on Laravel Users ([Flagsmith Identities](https://docs.flagsmith.com/basic-features/managing-identities))
- Utilizes [Laravel's Queue](https://laravel.com/docs/8.x/queues) system to update flags in the background
- Utilizes [Laravel's Cache](https://laravel.com/docs/8.x/cache) system to store flags in a cache for quick access
- Utilizes [Laravel's Task Scheduling](https://laravel.com/docs/8.x/scheduling) system to update flags on a schedule
- Adds a route to utilize [Flagsmith's webhooks](https://docs.flagsmith.com/advanced-use/system-administration) to update the cache when flags change

## Installation & Usage

> **Requires [PHP 7.4+](https://php.net/releases/)**
> **Requires [PHP 8.2+](https://php.net/releases/)**
Require Laravel-flagsmith using [Composer](https://getcomposer.org):

Expand All @@ -32,46 +32,48 @@ composer require clearlyip/laravel-flagsmith
| :------ | :---------------- |
| 8.x | 1.x |
| 9.x | 2.x |
| 10.x | 2.1.x |
| 10.x | 3.x |

## Usage

### Configuration Files

- Publish the Laravel Flagsmith configuration file using the `vendor:publish` Artisan command. The `flagsmith` configuration file will be placed in your `config` directory (Use `--force` to overwrite your existing `clearly` config file):
- `php artisan vendor:publish --tag="flagsmith" [--force]`
- Publish the Laravel Flagsmith configuration file using the `vendor:publish` Artisan command. The `flagsmith` configuration file will be placed in your `config` directory (Use `--force` to overwrite your existing `clearly` config file):
- `php artisan vendor:publish --tag="flagsmith" [--force]`

All options are fully documented in the configuration file

### User

It's advised to add the trait `Clearlyip\LaravelFlagsmith\Concerns\HasFeatures` to your user model. This will give you the ability to access features directly from your user object.
It's advised to add the interface `Clearlyip\LaravelFlagsmith\Contracts\UserFlags` to your user model. This will give you the ability to access flags directly from your user object.

During inital login user features are synced through a queue which keeps them as up to date as possible
You can add the following trait `Clearlyip\LaravelFlagsmith\Concerns\HasFlagss` to your user model to fulfill the requirements of `UserFlags`

#### List All Features for a User
During initial login user flags are synced through a queue which keeps them as up to date as possible

#### Get All Flags for a User

```php
$user = Auth::user();
$features = $user->getFeatures();
$flags = $user->getFlags();
```

### Check if feature is enabled for a user
### Check if flag is enabled for a user

An optional second parameter can be added as the default if the feature does not exist
An optional second parameter can be added as the default if the flag does not exist

```php
$user = Auth::user();
$features = $user->isFeatureEnabled('foo');
$flags = $user->isFlagEnabled('foo');
```

#### Get a Features value for a User
#### Get a Flag value for a User

An optional second parameter can be added as the default if the feature does not exist
An optional second parameter can be added as the default if the flag does not exist

```php
$user = Auth::user();
$features = $user->getFeatureValue('foo');
$vakue = $user->getFlagValue('foo');
```

### Accessing
Expand Down
51 changes: 45 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
}
],
"require": {
"php": ">=7.4",
"laravel/framework": ">=8.0",
"flagsmith/flagsmith-php-client": "^2.0"
"php": "^8.2",
"laravel/framework": "^10.44.0 || ^11.0",
"flagsmith/flagsmith-php-client": "^4.2.0"
},
"extra": {
"laravel": {
"providers": [
"Clearlyip\\LaravelFlagsmith\\ServiceProvider"
]
],
"aliases": {
"Flag": "Clearlyip\\LaravelFlagsmith\\Facades\\Flag"
}
}
},
"autoload": {
Expand All @@ -27,7 +30,43 @@
}
},
"require-dev": {
"guzzlehttp/psr7": "^2.1",
"guzzlehttp/guzzle": "^7.4"
"guzzlehttp/psr7": "^2.6.2",
"guzzlehttp/guzzle": "^7.8.1",
"orchestra/testbench": "^8.22.0",
"phpunit/phpunit": "^10.5.13",
"vimeo/psalm": "^5.23.1",
"squizlabs/php_codesniffer": "^3.9.0"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
},
"autoload-dev": {
"psr-4": {
"CIP\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"scripts": {
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve"
],
"test": "XDEBUG_MODE=coverage phpunit --configuration phpunit.xml",
"test:filter": "XDEBUG_MODE=coverage,debug phpunit --configuration phpunit.xml --filter",
"psalm": "psalm",
"phpcs": "phpcs --standard=phpcs.xml",
"phpcbf": "phpcbf --standard=phpcs.xml"
}
}
19 changes: 3 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
{
"name": "@clearlyip/laravel-flagsmith",
"version": "1.0.0",
"main": "index.js",
"directories": {
"test": "tests"
},
"private": true,
"repository": {
"type": "git",
"url": "ssh://git@github.com:clearlyip/laravel-flagsmith.git"
},
"author": "",
"license": "BSD 3-Clause",
"dependencies": {
"@prettier/plugin-php": "^0.14.3",
"prettier": "^2.1.2"
"devDependencies": {
"@prettier/plugin-php": "^0.21.0",
"prettier": "^3.1.0"
}
}
53 changes: 53 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0"?>
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">
<!-- https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset -->
<description>ClearlyIP coding standards</description>
<file>./src</file>
<exclude-pattern>*/data/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<arg value="sp"/>

<rule ref="PSR12"/>

<!-- This option is good for inspecting extended methods, however using ReflectionClass may cause phpcs crashes while editing (not possible to catch FatalError). -->
<!-- <autoload>./vendor/autoload.php</autoload> -->
<!-- https://github.com/gskema/phpcs-type-sniff -->
<!--
<rule ref="./vendor/gskema/phpcs-type-sniff/src/Sniffs/CompositeCodeElementSniff.php">
<properties>
<property name="useReflection" value="true"/>
</properties>
<exclude name="Gskema.Sniffs.CompositeCodeElement.FqcnMethodSniff"/>
<exclude name="Gskema.Sniffs.CompositeCodeElement.FqcnConstSniff"/>
</rule>
-->

<rule ref="Generic.Files.LineLength.TooLong">
<!-- This is enforced by prettier -->
<exclude name="Generic.Files.LineLength.TooLong"/>
</rule>

<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration">
<!-- This will break as a result of prettier so dont enforce it -->
<exclude name="Squiz.ControlStructures.ForEachLoopDeclaration.SpacingBeforeAs" />
</rule>

<rule ref="Squiz.ControlStructures.ControlSignature">
<!-- This will break as a result of prettier so dont enforce it -->
<exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace" />
</rule>

<rule ref="Squiz.Functions.MultiLineFunctionDeclaration">
<!-- This will break as a result of prettier so dont enforce it -->
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.OneParamPerLine" />
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.UseOneParamPerLine" />
</rule>

<rule ref="Generic.WhiteSpace.DisallowTabIndent">
<exclude name="Generic.WhiteSpace.DisallowTabIndent.TabsUsed" />
</rule>


</ruleset>
36 changes: 36 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true" colors="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="false">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<clover outputFile="output/coverage/cov.xml"/>
<html outputDirectory="output/coverage/html"/>
<text outputFile="php://stdout" showUncoveredFiles="false"/>
</report>
</coverage>
<logging>
<testdoxHtml outputFile="output/logging/testsDox.html"/>
<testdoxText outputFile="output/logging/testsDox.txt"/>
</logging>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
<server name="DB_CONNECTION" value="testing"/>
<server name="REDIS_CLIENT" value="mock"/>
<server name="APP_KEY" value="base64:+E3QzOdQTPu20d4VY93Oh99eVFhZmbA8D+mpj+r7wJs=" />
</php>
</phpunit>
16 changes: 16 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
</psalm>
Loading

0 comments on commit c3ab321

Please sign in to comment.