commit 0ff63daa2ad7293ed0147623bc2417b63779d330 Author: KHOUBZA Younes Date: Wed Dec 2 00:54:31 2020 +0100 add php-notify classes and rename namespaces diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..3383d44b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,13 @@ + +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/.scrutinizer.yml export-ignore +/.styleci.yml export-ignore +/tests export-ignore +/.editorconfig export-ignore diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 00000000..13900d7b --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,128 @@ +name: Running tests + +on: + push: + pull_request: + schedule: + - cron: '0 * * * *' + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + symfony: [5.2.*, 5.1.*, 5.0.*, 4.4.*, 4.3.*, 4.2.*, 4.1.*, 4.0.*, 3.4.*, 3.3.*, 3.2.*, 3.1.*, 3.0.*, 2.8.*, 2.7.*, 2.6.*, 2.5.*, 2.4.*, 2.3.*, 2.2.*, 2.1.*] + dependency-version: [prefer-lowest, prefer-stable] + include: + - symfony: 5.2.* + php: 7.2 + phpunit: 8.3.* + + - symfony: 5.1.* + php: 7.2 + phpunit: 8.3.* + + - symfony: 5.0.* + php: 7.2 + phpunit: 8.3.* + + - symfony: 4.4.* + php: 7.1 + phpunit: 7.0.* + + - symfony: 4.3.* + php: 7.1 + phpunit: 7.0.* + + - symfony: 4.2.* + php: 7.1 + phpunit: 7.0.* + + - symfony: 4.1.* + php: 7.1 + phpunit: 7.0.* + + - symfony: 4.0.* + php: 7.1 + phpunit: 7.0.* + + - symfony: 3.4.* + php: 5.5 + phpunit: 4.8.36 + + - symfony: 3.3.* + php: 5.5 + phpunit: 4.8.36 + + - symfony: 3.2.* + php: 5.5 + phpunit: 4.8.36 + + - symfony: 3.1.* + php: 5.5 + phpunit: 4.8.36 + + - symfony: 3.0.* + php: 5.5 + phpunit: 4.8.36 + + - symfony: 2.8.* + php: 5.3 + phpunit: 4.8.36 + + - symfony: 2.7.* + php: 5.3 + phpunit: 4.8.36 + + - symfony: 2.6.* + php: 5.3 + phpunit: 4.8.36 + + - symfony: 2.5.* + php: 5.3 + phpunit: 4.8.36 + + - symfony: 2.4.* + php: 5.3 + phpunit: 4.8.36 + + - symfony: 2.3.* + php: 5.3 + phpunit: 4.8.36 + + - symfony: 2.2.* + php: 5.3 + phpunit: 4.8.36 + + - symfony: 2.1.* + php: 5.3 + phpunit: 4.8.36 + + name: S${{ matrix.symfony }} - P${{ matrix.php }} - ${{ matrix.dependency-version }} + + steps: + - name: Checkout code + uses: actions/checkout@v1 + + - name: Cache dependencies + uses: actions/cache@v1 + with: + path: ~/.composer/cache/files + key: dependencies-symfony-${{ matrix.symfony }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick + coverage: none + + - name: Install dependencies + run: | + COMPOSER_MEMORY_LIMIT=-1 composer require "symfony/symfony:${{ matrix.symfony }}" "phpunit/phpunit:${{ matrix.phpunit }}" --no-interaction --no-update + COMPOSER_MEMORY_LIMIT=-1 composer config extra.symfony.require "${{ matrix.symfony }}" + COMPOSER_MEMORY_LIMIT=-1 composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + + - name: Execute tests + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..409f0e32 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea/ +vendor/ +composer.lock +.phpunit.result.cache +.phpcs-cache diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist new file mode 100644 index 00000000..93d245bc --- /dev/null +++ b/.phpcs.xml.dist @@ -0,0 +1,625 @@ + + + + + + + + + + + + + src + tests + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + error + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php new file mode 100644 index 00000000..aa712a5c --- /dev/null +++ b/.phpstorm.meta.php @@ -0,0 +1,14 @@ + '@'])); +override(RendererManager::make(''), map(['' => '@'])); +override(PresenterManager::make(''), map(['' => '@'])); diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..e69de29b diff --git a/DependencyInjection/Compiler/FilterCompilerPass.php b/DependencyInjection/Compiler/FilterCompilerPass.php new file mode 100644 index 00000000..38b44a55 --- /dev/null +++ b/DependencyInjection/Compiler/FilterCompilerPass.php @@ -0,0 +1,32 @@ +has('notify.filter')) { + return; + } + + /** @var \Flasher\Prime\Filter\FilterManager $manager */ + $manager = $container->findDefinition('notify.filter'); + + foreach ($container->findTaggedServiceIds('notify.filter') as $id => $tags) { + foreach ($tags as $attributes) { + $manager->addMethodCall('addDriver', array( + $attributes['alias'], + new Reference($id), + )); + } + } + } +} diff --git a/DependencyInjection/Compiler/PresenterCompilerPass.php b/DependencyInjection/Compiler/PresenterCompilerPass.php new file mode 100644 index 00000000..e7874b10 --- /dev/null +++ b/DependencyInjection/Compiler/PresenterCompilerPass.php @@ -0,0 +1,32 @@ +has('notify.presenter')) { + return; + } + + /** @var \Flasher\Prime\Presenter\PresenterManager $manager */ + $manager = $container->findDefinition('notify.presenter'); + + foreach ($container->findTaggedServiceIds('notify.presenter') as $id => $tags) { + foreach ($tags as $attributes) { + $manager->addMethodCall('addDriver', array( + $attributes['alias'], + new Reference($id), + )); + } + } + } +} diff --git a/DependencyInjection/Compiler/ProducerCompilerPass.php b/DependencyInjection/Compiler/ProducerCompilerPass.php new file mode 100644 index 00000000..8356c75b --- /dev/null +++ b/DependencyInjection/Compiler/ProducerCompilerPass.php @@ -0,0 +1,32 @@ +has('notify.producer')) { + return; + } + + /** @var \Flasher\Prime\Flasher $manager */ + $manager = $container->findDefinition('notify.producer'); + + foreach ($container->findTaggedServiceIds('notify.producer') as $id => $tags) { + foreach ($tags as $attributes) { + $manager->addMethodCall('addDriver', array( + $attributes['alias'], + new Reference($id), + )); + } + } + } +} diff --git a/DependencyInjection/Compiler/RendererCompilerPass.php b/DependencyInjection/Compiler/RendererCompilerPass.php new file mode 100644 index 00000000..2939f256 --- /dev/null +++ b/DependencyInjection/Compiler/RendererCompilerPass.php @@ -0,0 +1,32 @@ +has('notify.renderer')) { + return; + } + + /** @var \Flasher\Prime\Renderer\RendererManager $manager */ + $manager = $container->findDefinition('notify.renderer'); + + foreach ($container->findTaggedServiceIds('notify.renderer') as $id => $tags) { + foreach ($tags as $attributes) { + $manager->addMethodCall('addDriver', array( + $attributes['alias'], + new Reference($id), + )); + } + } + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 00000000..befd1f23 --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,54 @@ +getRootNode(); + } else { + // BC layer for symfony/config 4.1 and older + $rootNode = $treeBuilder->root('notify'); + } + + $rootNode + ->children() + ->scalarNode('default') + ->cannotBeEmpty() + ->defaultValue('toastr') + ->end() + ->arrayNode('scripts') + ->prototype('scalar')->end() + ->defaultValue(array( + '/vendor/php-flasher/flasher/assets/js/notify.js' + )) + ->end() + ->arrayNode('styles') + ->prototype('scalar')->end() + ->defaultValue(array()) + ->end() + ->arrayNode('adapters') + ->ignoreExtraKeys(false) + ->useAttributeAsKey('name') + ->prototype('variable')->end() + ->end() + ->arrayNode('stamps_middlewares') + ->ignoreExtraKeys(false) + ->prototype('variable')->end() + ->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/DependencyInjection/NotifyExtension.php b/DependencyInjection/NotifyExtension.php new file mode 100644 index 00000000..ba33db63 --- /dev/null +++ b/DependencyInjection/NotifyExtension.php @@ -0,0 +1,29 @@ +load('services.yaml'); + + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + + $notifyConfig = $container->getDefinition('notify.config'); + $notifyConfig->replaceArgument(0, $config); + } +} diff --git a/NotifyBundle.php b/NotifyBundle.php new file mode 100644 index 00000000..cbaa07df --- /dev/null +++ b/NotifyBundle.php @@ -0,0 +1,22 @@ +addCompilerPass(new ProducerCompilerPass()); + $container->addCompilerPass(new RendererCompilerPass()); + $container->addCompilerPass(new FilterCompilerPass()); + } +} diff --git a/README.md b/README.md new file mode 100644 index 00000000..fac4b13f --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +

notify

+ +

Easy flash notifications for PHP, Laravel, Symfony, Lumen

+ +

:eyes: PHP Flasher library helps you to add flash notifications to your projects. This library was developed with the idea that you should be able to add flash notification to your application with ease and with few lines of code. No application-wide rewrites and no big investments upfront.

+ +

+ + + + + + + + + + + + + + + +

+ +## Introduction + +PHP Flasher library helps you to add flash notifications to your projects. This library was developed with the idea that you should be able to add flash notification to your application with ease and with few lines of code. No application-wide rewrites and no big investments upfront. + +# Why use PHP Flasher ? + +The PHP Flasher project supports a variety of notification libraries : __toastr.js__, __sweet alert 2__, __pnotify__ and __notyf__ +and its highly extendable so you can add custom adapters. + +This library is designed, so you can take full control when creating you notifications : + +> * Display multiple notifications +> * Sort and filter notifications +> * Render notification from JSON response with Ajax or Websockets +> * Limit the number of displayed notifications +> * Show notifications from different adapters at the same time +> * implementations for popular frameworks : Symfony and Laravel +> * ...and more + + +## Official Documentation + +Documentation for Valet can be found on the [PHP Flasher website](https://php-flasher.github.io/). + +## License + +PHP Flasher is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml new file mode 100644 index 00000000..2c55bad9 --- /dev/null +++ b/Resources/config/services.yaml @@ -0,0 +1,91 @@ +services: + notify.config: + class: Flasher\Prime\Config\Config + arguments: + - null + + notify.producer: + class: Flasher\Prime\Producer\ProducerManager + arguments: + - '@notify.config' + + notify.presenter: + class: Flasher\Prime\Presenter\PresenterManager + + notify.renderer: + class: Flasher\Prime\Renderer\RendererManager + arguments: + - '@notify.config' + + notify.middleware: + class: Flasher\Prime\Middleware\MiddlewareManager + arguments: + - '@notify.config' + + notify.storage: + class: Flasher\Symfony\Storage\Storage + arguments: + - '@session' + + notify.filter: + class: Flasher\Prime\Filter\FilterManager + arguments: + - '@notify.config' + + notify.producer.abstract: + class: Flasher\Prime\Producer\AbstractProducer + abstract: true + arguments: + - '@notify.storage' + - '@notify.middleware' + + notify.presenter.abstract: + class: Flasher\Prime\Presenter\AbstractPresenter + abstract: true + arguments: + - '@notify.config' + - '@notify.storage' + - '@notify.filter' + - '@notify.renderer' + + notify.presenter.html: + class: Flasher\Prime\Presenter\Adapter\HtmlPresenter + parent: 'notify.presenter.abstract' + tags: + - { name: 'notify.presenter', alias: 'html' } + + notify.presenter.json: + class: Flasher\Prime\Presenter\Adapter\JsonPresenter + parent: 'notify.presenter.abstract' + tags: + - { name: 'notify.presenter', alias: 'json' } + + notify.twig_extension: + class: Flasher\Symfony\Twig\NotifyTwigExtension + arguments: + - '@notify.presenter.html' + public: false + tags: + - { name: twig.extension } + + notify.filter_builder: + class: Flasher\Prime\Filter\FilterBuilder + + notify.filter.default: + class: Flasher\Prime\Filter\DefaultFilter + arguments: + - '@notify.filter_builder' + tags: + - { name: 'notify.filter', alias: 'default' } + + Flasher\Prime\Config\Config: '@notify.config' + Flasher\Prime\Producer\ProducerManager: '@notify.producer' + Flasher\Prime\Presenter\PresenterManager: '@notify.presenter' + Flasher\Prime\Renderer\RendererManager: '@notify.renderer' + Flasher\Prime\Middleware\MiddlewareManager: '@notify.middleware' + Flasher\Symfony\Storage\Storage: '@notify.storage' + Flasher\Prime\Filter\FilterManager: '@notify.filter' + Flasher\Prime\Filter\DefaultFilter: '@notify.filter.default' + Flasher\Prime\Filter\FilterBuilder: '@notify.filter_builder' + Flasher\Prime\Presenter\Json\JsonPresenter: '@notify.presenter.json' + Flasher\Prime\Presenter\Html\HtmlPresenter: '@notify.presenter.html' diff --git a/Storage/Storage.php b/Storage/Storage.php new file mode 100644 index 00000000..5913906c --- /dev/null +++ b/Storage/Storage.php @@ -0,0 +1,86 @@ +session = $session; + } + + public function get() + { + return $this->session->get(self::ENVELOPES_NAMESPACE, array()); + } + + public function add(Envelope $envelope) + { + if (null === $envelope->get('Flasher\Prime\Stamp\UuidStamp')) { + $envelope->withStamp(new UuidStamp()); + } + + if (null === $envelope->get('Flasher\Prime\Stamp\UuidStamp')) { + $envelope->withStamp(new UuidStamp()); + } + + if (null === $envelope->get('Flasher\Prime\Stamp\LifeStamp')) { + $envelope->withStamp(new LifeStamp(1)); + } + + if (null === $envelope->get('Flasher\Prime\Stamp\CreatedAtStamp')) { + $envelope->withStamp(new CreatedAtStamp()); + } + + $envelopes = $this->get(); + $envelopes[] = $envelope; + + $this->session->set(self::ENVELOPES_NAMESPACE, $envelopes); + } + + /** + * @param Envelope[] $envelopes + */ + public function flush($envelopes) + { + $envelopesMap = array(); + + foreach ($envelopes as $envelope) { + $life = $envelope->get('Flasher\Prime\Stamp\LifeStamp')->getLife(); + $uuid = $envelope->get('Flasher\Prime\Stamp\UuidStamp')->getUuid(); + + $envelopesMap[$uuid] = $life; + } + + $store = array(); + + foreach ($this->session->get(self::ENVELOPES_NAMESPACE, array()) as $envelope) { + $uuid = $envelope->get('Flasher\Prime\Stamp\UuidStamp')->getUuid(); + + if(isset($envelopesMap[$uuid])) { + $life = $envelopesMap[$uuid] - 1; + + if ($life <= 0) { + continue; + } + + $envelope->with(new LifeStamp($life)); + } + + $store[] = $envelope; + } + + $this->session->set(self::ENVELOPES_NAMESPACE, $store); + } +} diff --git a/Tests/.gitkeep b/Tests/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Twig/NotifyTwigExtension.php b/Twig/NotifyTwigExtension.php new file mode 100644 index 00000000..9cc18061 --- /dev/null +++ b/Twig/NotifyTwigExtension.php @@ -0,0 +1,37 @@ +htmlPresenter = $htmlPresenter; + } + + public function getFunctions() + { + $options = array('is_safe' => array('html')); + + return array( + new TwigFunction('notify_render', array($this, 'notifyRender'), $options), + ); + } + + /** + * @param string|array $criteria + * + * @return string + */ + public function notifyRender($criteria = null) + { + return $this->htmlPresenter->render($criteria); + } +} diff --git a/UPGRADING.md b/UPGRADING.md new file mode 100644 index 00000000..e69de29b diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..07fec205 --- /dev/null +++ b/composer.json @@ -0,0 +1,60 @@ +{ + "name": "php-flasher/flasher-symfony", + "type": "symfony-bundle", + "description": "Symfony Flasher Bundle for php-flasher/flasher", + "keywords": [ + "yoeunes", + "notify", + "php", + "laravel", + "symfony", + "Lumen", + "notifications", + "messages", + "alerts", + "pnotify", + "toastr ", + "bundle", + "flex" + ], + "homepage": "https://github.com/php-flasher/flasher-symfony", + "authors": [ + { + "name": "Younes Khoubza", + "email": "younes.khoubza@gmail.com", + "homepage": "https://github.com/yoeunes", + "role": "Developer" + } + ], + "license": "MIT", + "require": { + "php": ">=5.3", + "php-flasher/flasher": "^1.0", + "symfony/config": "^2.1|^3.0|^4.0|^5.0", + "symfony/dependency-injection": "^2.1|^3.0|^4.0|^5.0", + "symfony/http-kernel": "^2.1|^3.0|^4.0|^5.0", + "symfony/yaml": "^2.1|^3.0|^4.0|^5.0" + }, + "require-dev": { + "phpunit/phpunit": "4.8.36", + "twig/twig": "^1.34|^2.0|^3.0" + }, + "suggest": { + "twig/twig": "Twig support for Htmlpresenter" + }, + "autoload": { + "psr-4": { + "Flasher\\Symfony\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Flasher\\Symfony\\Tests\\": "tests/" + } + }, + "config": { + "sort-packages": true + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 00000000..2621c486 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,22 @@ + + + + + tests + + + + + src/ + + +