From 17c7e32444b9a422befa154842811834346eb7df Mon Sep 17 00:00:00 2001 From: KHOUBZA Younes Date: Wed, 2 Dec 2020 00:54:31 +0100 Subject: [PATCH] add php-notify classes and rename namespaces --- .gitattributes | 13 + .github/workflows/tests.yaml | 119 ++++ .gitignore | 5 + .phpcs.xml.dist | 625 ++++++++++++++++++ .phpstorm.meta.php | 14 + CHANGELOG.md | 0 NotifyNotyfServiceProvider.php | 63 ++ README.md | 51 ++ Resources/config/config.php | 51 ++ ServiceProvider/Providers/Laravel.php | 70 ++ ServiceProvider/Providers/Laravel4.php | 28 + ServiceProvider/Providers/Lumen.php | 23 + .../Providers/ServiceProviderInterface.php | 16 + ServiceProvider/ServiceProviderManager.php | 61 ++ Tests/NotifyNotyfServiceProviderTest.php | 43 ++ Tests/TestCase.php | 95 +++ UPGRADING.md | 0 composer.json | 61 ++ phpunit.xml.dist | 22 + 19 files changed, 1360 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/workflows/tests.yaml create mode 100644 .gitignore create mode 100644 .phpcs.xml.dist create mode 100644 .phpstorm.meta.php create mode 100644 CHANGELOG.md create mode 100644 NotifyNotyfServiceProvider.php create mode 100644 README.md create mode 100644 Resources/config/config.php create mode 100644 ServiceProvider/Providers/Laravel.php create mode 100644 ServiceProvider/Providers/Laravel4.php create mode 100644 ServiceProvider/Providers/Lumen.php create mode 100644 ServiceProvider/Providers/ServiceProviderInterface.php create mode 100644 ServiceProvider/ServiceProviderManager.php create mode 100644 Tests/NotifyNotyfServiceProviderTest.php create mode 100644 Tests/TestCase.php create mode 100644 UPGRADING.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist 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..5c733a6b --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,119 @@ +name: Running tests + +on: + push: + pull_request: + schedule: + - cron: '0 * * * *' + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + laravel: [8.*, 7.*, 6.*, 5.8.*, 5.7.*, 5.6.*, 5.5.*, 5.4.*, 5.3.*, 5.2.*, 5.1.*, 5.0.*, 4.2.*, 4.1.*, 4.0.*] + dependency-version: [prefer-lowest, prefer-stable] + include: + - laravel: 8.* + testbench: 6.* + php: 7.3 + phpunit: 9.3.* + + - laravel: 7.* + testbench: 5.* + php: 7.3 + phpunit: 8.4.* + + - laravel: 6.* + testbench: 4.* + php: 7.2 + phpunit: 8.3.* + + - laravel: 5.8.* + testbench: 3.8.* + php: 7.1 + phpunit: 7.5.* + + - laravel: 5.7.* + testbench: 3.7.* + php: 7.1 + phpunit: 7.0.* + + - laravel: 5.6.* + testbench: 3.6.* + php: 7.1 + phpunit: 7.0.* + + - laravel: 5.5.* + testbench: 3.5.* + php: 7.0 + phpunit: 6.0.* + + - laravel: 5.4.* + testbench: 3.4.* + php: 5.6 + phpunit: 5.7.* + + - laravel: 5.3.* + testbench: 3.3.* + php: 5.6 + phpunit: 4.8.* + + - laravel: 5.2.* + testbench: 3.2.* + php: 5.5 + phpunit: 4.8.* + + - laravel: 5.1.* + testbench: 3.1.* + php: 5.5 + phpunit: 4.8.* + + - laravel: 5.0.* + testbench: 3.0.* + php: 5.4 + phpunit: 4.8.* + + - laravel: 4.2.* + testbench: 2.2.* + php: 5.4 + phpunit: 4.8.* + + - laravel: 4.1.* + testbench: 2.1.* + php: 5.4 + phpunit: 4.8.* + + - laravel: 4.0.* + testbench: 2.0.* + php: 5.4 + phpunit: 4.8.* + + name: L${{ matrix.laravel }} - 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-laravel-${{ matrix.laravel }}-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 + # ini-values: memory_limit=-1 + coverage: none + + - name: Install dependencies + run: | + COMPOSER_MEMORY_LIMIT=-1 composer require "laravel/framework:${{ matrix.laravel }}" "phpunit/phpunit:${{ matrix.phpunit }}" "orchestra/testbench:${{ matrix.testbench }}" monolog/monolog --no-interaction --no-update + 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/NotifyNotyfServiceProvider.php b/NotifyNotyfServiceProvider.php new file mode 100644 index 00000000..f3817a32 --- /dev/null +++ b/NotifyNotyfServiceProvider.php @@ -0,0 +1,63 @@ +boot(); + } + + /** + * Register the service provider. + */ + public function register() + { + $manager = new ServiceProviderManager($this); + $manager->register(); + } + + /** + * Get the services provided by the provider. + * + * @return string[] + */ + public function provides() + { + return array( + 'notify.producer', + 'notify.producer.notyf', + 'notify.renderer.notyf', + ); + } + + /** + * @return \Illuminate\Container\Container + */ + public function getApplication() + { + return $this->app; + } + + /** + * {@inheritdoc} + */ + public function mergeConfigFrom($path, $key) + { + parent::mergeConfigFrom($path, $key); + } + + /** + * {@inheritdoc} + */ + public function publishes(array $paths, $groups = null) + { + parent::publishes($paths, $groups); + } +} 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/config.php b/Resources/config/config.php new file mode 100644 index 00000000..21cbc904 --- /dev/null +++ b/Resources/config/config.php @@ -0,0 +1,51 @@ + array( + 'https://cdn.jsdelivr.net/npm/notyf@3/notyf.min.js', + ), + 'styles' => array( + 'https://cdn.jsdelivr.net/npm/notyf@3/notyf.min.css', + ), + 'options' => array( + 'duration' => 5000, + 'ripple' => true, + 'position' => array( + 'x' => 'right', + 'y' => 'top', + ), + 'dismissible' => false, + 'types' => array( + array( + 'type' => 'success', + 'className' => 'notyf__toast--success', + 'backgroundColor' => '#3dc763', + 'icon' => array( + 'className' => 'notyf__icon--success', + 'tagName' => 'i', + ), + ), + array( + 'type' => 'error', + 'className' => 'notyf__toast--error', + 'backgroundColor' => '#ed3d3d', + 'icon' => array( + 'className' => 'notyf__icon--error', + 'tagName' => 'i', + ), + ), + array( + 'type' => 'info', + 'className' => 'notyf__toast--info', + 'backgroundColor' => '#5784E5', + 'icon' => false, + ), + array( + 'type' => 'warning', + 'className' => 'notyf__toast--warning', + 'backgroundColor' => '#E3A008', + 'icon' => false, + ) + ), + ), +); diff --git a/ServiceProvider/Providers/Laravel.php b/ServiceProvider/Providers/Laravel.php new file mode 100644 index 00000000..476e2c96 --- /dev/null +++ b/ServiceProvider/Providers/Laravel.php @@ -0,0 +1,70 @@ +app = $app; + } + + public function shouldBeUsed() + { + return $this->app instanceof Application; + } + + public function publishConfig(NotifyNotyfServiceProvider $provider) + { + $source = realpath($raw = __DIR__.'/../../../resources/config/config.php') ?: $raw; + + $provider->publishes(array($source => config_path('notify_notyf.php')), 'config'); + + $provider->mergeConfigFrom($source, 'notify_notyf'); + } + + public function registerNotifyNotyfServices() + { + $this->app->singleton('notify.producer.notyf', function (Container $app) { + return new NotyfProducer($app['notify.storage'], $app['notify.middleware']); + }); + + $this->app->singleton('notify.renderer.notyf', function (Container $app) { + return new NotyfRenderer($app['notify.config']); + }); + + $this->app->alias('notify.producer.notyf', 'Flasher\Notyf\Prime\Producer\NotyfProducer'); + $this->app->alias('notify.renderer.notyf', 'Flasher\Notyf\Prime\Renderer\NotyfRenderer'); + + $this->app->extend('notify.producer', function (Flasher $manager, Container $app) { + $manager->addDriver('notyf', $app['notify.producer.notyf']); + + return $manager; + }); + + $this->app->extend('notify.renderer', function (RendererManager $manager, Container $app) { + $manager->addDriver('notyf', $app['notify.renderer.notyf']); + + return $manager; + }); + } + + public function mergeConfigFromNotyf() + { + $notifyConfig = $this->app['config']->get('notify.adapters.notyf', array()); + + $notyfConfig = $this->app['config']->get('notify_notyf', array()); + + $this->app['config']->set('notify.adapters.notyf', array_merge($notyfConfig, $notifyConfig)); + } +} diff --git a/ServiceProvider/Providers/Laravel4.php b/ServiceProvider/Providers/Laravel4.php new file mode 100644 index 00000000..58ad7394 --- /dev/null +++ b/ServiceProvider/Providers/Laravel4.php @@ -0,0 +1,28 @@ +app instanceof Application && 0 === strpos(Application::VERSION, '4.'); + } + + public function publishConfig(NotifyNotyfServiceProvider $provider) + { + $provider->package('php-flasher/flasher-laravel-notyf', 'notify_notyf', __DIR__.'/../../../resources'); + } + + public function mergeConfigFromNotyf() + { + $notifyConfig = $this->app['config']->get('notify::config.adapters.notyf', array()); + + $notyfConfig = $this->app['config']->get('notify_notyf::config', array()); + + $this->app['config']->set('notify::config.adapters.notyf', array_merge($notyfConfig, $notifyConfig)); + } +} diff --git a/ServiceProvider/Providers/Lumen.php b/ServiceProvider/Providers/Lumen.php new file mode 100644 index 00000000..811ce74d --- /dev/null +++ b/ServiceProvider/Providers/Lumen.php @@ -0,0 +1,23 @@ +app instanceof Application; + } + + public function publishConfig(NotifyNotyfServiceProvider $provider) + { + $source = realpath($raw = __DIR__.'/../../../resources/config/config.php') ?: $raw; + + $this->app->configure('notify_notyf'); + + $provider->mergeConfigFrom($source, 'notify_notyf'); + } +} diff --git a/ServiceProvider/Providers/ServiceProviderInterface.php b/ServiceProvider/Providers/ServiceProviderInterface.php new file mode 100644 index 00000000..33b9c1d8 --- /dev/null +++ b/ServiceProvider/Providers/ServiceProviderInterface.php @@ -0,0 +1,16 @@ +notifyServiceProvider = $notifyServiceProvider; + } + + public function boot() + { + $provider = $this->resolveServiceProvider(); + + $provider->publishConfig($this->notifyServiceProvider); + $provider->mergeConfigFromNotyf(); + } + + public function register() + { + $provider = $this->resolveServiceProvider(); + $provider->registerNotifyNotyfServices(); + } + + /** + * @return ServiceProviderInterface + */ + private function resolveServiceProvider() + { + if ($this->provider instanceof ServiceProviderInterface) { + return $this->provider; + } + + foreach ($this->providers as $providerClass) { + $provider = new $providerClass($this->notifyServiceProvider->getApplication()); + + if ($provider->shouldBeUsed()) { + return $this->provider = $provider; + } + } + + throw new \InvalidArgumentException('Service Provider not found.'); + } +} diff --git a/Tests/NotifyNotyfServiceProviderTest.php b/Tests/NotifyNotyfServiceProviderTest.php new file mode 100644 index 00000000..4cf5590f --- /dev/null +++ b/Tests/NotifyNotyfServiceProviderTest.php @@ -0,0 +1,43 @@ +assertTrue($this->app->bound('notify.producer')); + $this->assertTrue($this->app->bound('notify.producer.notyf')); + } + + public function testNotifyFactoryIsAddedToExtensionsArray() + { + $manager = $this->app->make('notify.producer'); + + $reflection = new \ReflectionClass($manager); + $property = $reflection->getProperty('drivers'); + $property->setAccessible(true); + + $extensions = $property->getValue($manager); + + $this->assertCount(1, $extensions); + $this->assertInstanceOf('Flasher\Prime\FlasherInterface', $extensions['notyf']); + } + + public function testConfigNotyfInjectedInGlobalNotifyConfig() + { + $manager = $this->app->make('notify.producer'); + + $reflection = new \ReflectionClass($manager); + $property = $reflection->getProperty('config'); + $property->setAccessible(true); + + $config = $property->getValue($manager); + + $this->assertArrayHasKey('notyf', $config->get('adapters')); + + $this->assertEquals(array( + 'notyf' => array('scripts' => array('jquery.js'), 'styles' => array('styles.css'), 'options' => array()), + ), $config->get('adapters')); + } +} diff --git a/Tests/TestCase.php b/Tests/TestCase.php new file mode 100644 index 00000000..37409aaf --- /dev/null +++ b/Tests/TestCase.php @@ -0,0 +1,95 @@ +isLaravel4() ? '::config' : ''; + + $app['config']->set('session'.$separator.'.driver', 'array'); + $app['config']->set('notify'.$separator.'.stamps_middlewares', array()); + $app['config']->set('notify'.$separator.'.adapters', array( + 'notyf' => array('scripts' => array('jquery.js'), 'styles' => array('styles.css'), 'options' => array()), + )); + } + + private function isLaravel4() + { + return 0 === strpos(Application::VERSION, '4.'); + } + + /** + * {@inheritdoc} + */ + public function createApplication() + { + if (0 !== strpos(Application::VERSION, '4.0')) { + return parent::createApplication(); + } + + $app = new Application; + + $app->detectEnvironment(array( + 'local' => array('your-machine-name'), + )); + + $app->bindInstallPaths($this->getApplicationPaths()); + + $app['env'] = 'testing'; + + $app->instance('app', $app); + + Facade::clearResolvedInstances(); + Facade::setFacadeApplication($app); + + $config = new Config($app->getConfigLoader(), $app['env']); + $app->instance('config', $config); + $app->startExceptionHandling(); + + if ($app->runningInConsole()) + { + $app->setRequestForConsoleEnvironment(); + } + + date_default_timezone_set($this->getApplicationTimezone()); + + $aliases = array_merge($this->getApplicationAliases(), $this->getPackageAliases()); + AliasLoader::getInstance($aliases)->register(); + + Request::enableHttpMethodParameterOverride(); + + $providers = array_merge($this->getApplicationProviders(), $this->getPackageProviders()); + $app->getProviderRepository()->load($app, $providers); + + $this->getEnvironmentSetUp($app); + + $app->boot(); + + return $app; + } +} 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..e7039234 --- /dev/null +++ b/composer.json @@ -0,0 +1,61 @@ +{ + "name": "php-flasher/flasher-laravel-notyf", + "description": "Laravel Flasher adapter package for php-flasher/flasher-notyf", + "keywords": [ + "yoeunes", + "notify", + "php", + "laravel", + "symfony", + "Lumen", + "notifications", + "messages", + "alerts", + "pnotify", + "notyf ", + "bundle", + "flex", + "toastr" + ], + "homepage": "https://github.com/php-flasher/flasher-laravel", + "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-laravel": "^1.0.4", + "php-flasher/flasher-notyf": "^1.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8 || ^5.7 || ^6.0 || ^7.0 || ^8.3 || ^9.0", + "orchestra/testbench": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0" + }, + "autoload": { + "psr-4": { + "Flasher\\Laravel\\Notyf\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Flasher\\Laravel\\Notyf\\Tests\\": "tests/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Flasher\\Laravel\\Notyf\\NotifyNotyfServiceProvider" + ] + } + }, + "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/ + + +