Merge pull request #115 from php-flasher/feat/publish-resources

feat: add command to publish PHPFlasher resources
This commit is contained in:
Younes KHOUBZA
2023-01-15 23:26:47 +01:00
committed by GitHub
17 changed files with 359 additions and 122 deletions
+42 -52
View File
@@ -7,26 +7,17 @@
namespace Flasher\Laravel\Command;
use Flasher\Laravel\Support\ServiceProvider as FlasherServiceProvider;
use Flasher\Prime\Plugin\PluginInterface;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class InstallCommand extends Command
{
/**
* @var string
*/
private $projectDir;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @return void
*/
@@ -34,9 +25,8 @@ class InstallCommand extends Command
{
$this
->setName('flasher:install')
->setDescription('Install all of the PHPFlasher resources.')
->addArgument('target', InputArgument::OPTIONAL, 'The target directory')
;
->setDescription('Installs all <fg=blue;options=bold>PHPFlasher</> resources to the <comment>public</comment> and <comment>config</comment> directories.')
->setHelp('The command copies <fg=blue;options=bold>PHPFlasher</> assets to <comment>public/vendor/flasher/</comment> directory and config files to the <comment>config/</comment> directory without overwriting any existing config files.');
}
/**
@@ -44,9 +34,6 @@ class InstallCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$publicDir = $this->getPublicDirectory($input);
$publicDir = rtrim($publicDir, '/').'/vendor/flasher/';
$output->writeln('');
$output->writeln('<fg=blue;options=bold>
██████╗ ██╗ ██╗██████╗ ███████╗██╗ █████╗ ███████╗██╗ ██╗███████╗██████╗
@@ -59,11 +46,10 @@ class InstallCommand extends Command
$output->writeln('');
$output->writeln('');
$output->writeln(sprintf('<bg=blue;options=bold> INFO </> Copying <fg=blue;options=bold>PHPFlasher</> assets into <fg=blue;options=bold>%s</>', $publicDir));
$output->writeln('<bg=blue;options=bold> INFO </> Copying <fg=blue;options=bold>PHPFlasher</> resources...');
$output->writeln('');
$this->filesystem->cleanDirectory($publicDir);
$publicDir = App::publicPath().'/vendor/flasher/';
$exitCode = 0;
foreach (ServiceProvider::publishableProviders() as $provider) {
@@ -71,19 +57,14 @@ class InstallCommand extends Command
continue;
}
/** @var \Flasher\Laravel\Support\ServiceProvider $provider */
/** @var FlasherServiceProvider $provider */
$provider = App::getProvider($provider);
$plugin = $provider->createPlugin();
$originDir = $plugin->getAssetsDir();
if (!is_dir($originDir)) {
continue;
}
$configFile = $provider->getConfigurationFile();
try {
$this->filesystem->ensureDirectoryExists($originDir, 0777);
$this->filesystem->copyDirectory($originDir, $publicDir);
$this->publishAssets($plugin, $publicDir);
$this->publishConfig($plugin, $configFile);
$status = sprintf('<fg=green;options=bold>%s</>', '\\' === \DIRECTORY_SEPARATOR ? 'OK' : "\xE2\x9C\x94" /* HEAVY CHECK MARK (U+2714) */);
$output->writeln(sprintf(' %s <fg=blue;options=bold>%s</>', $status, $plugin->getAlias()));
@@ -96,43 +77,52 @@ class InstallCommand extends Command
$output->writeln('');
if (0 === $exitCode) {
$output->writeln('<bg=green;options=bold> SUCCESS </> <fg=blue;options=bold>PHPFlasher</> resources have been successfully installed.');
} else {
$output->writeln('<bg=red;options=bold> ERROR </> An error occurred during the installation of <fg=blue;options=bold>PHPFlasher</> resources.');
}
$output->writeln('');
return $exitCode;
}
/**
* {@inheritdoc}
* @param string $publicDir
*
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output)
private function publishAssets(PluginInterface $plugin, $publicDir)
{
$this->filesystem = new Filesystem();
$this->projectDir = App::basePath();
$originDir = $plugin->getAssetsDir();
if (!is_dir($originDir)) {
return;
}
$filesystem = new Filesystem();
$filesystem->ensureDirectoryExists($originDir, 0777);
$filesystem->copyDirectory($originDir, $publicDir);
}
/**
* @return string
* @param string $configFile
*
* @return void
*/
private function getPublicDirectory(InputInterface $input)
private function publishConfig(PluginInterface $plugin, $configFile)
{
$targetDir = $this->getTargetDirectory($input);
if (is_dir($targetDir)) {
return $targetDir;
if (!file_exists($configFile)) {
return;
}
$publicDir = App::basePath().'/'.$targetDir;
if (is_dir($publicDir)) {
return $publicDir;
$target = App::configPath($plugin->getName().'.php');
if (file_exists($target)) {
return;
}
throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $publicDir));
}
/**
* @return string
*/
private function getTargetDirectory(InputInterface $input)
{
$targetDir = rtrim($input->getArgument('target') ?: '', '/');
return $targetDir ?: App::publicPath();
$filesystem = new Filesystem();
$filesystem->copy($configFile, $target);
}
}
+2 -2
View File
@@ -43,7 +43,7 @@ return array(
| using npm.
|
| To use the local version of the library, run the following command:
| php artisan vendor:publish --force --tag=flasher-assets
| php artisan flasher:install
|
| This will copy the necessary assets to your application's public folder.
| You can then specify the local path to the javascript file in the 'local'
@@ -65,7 +65,7 @@ return array(
|
| If you decide to use local assets, don't forget to publish the necessary
| files to your application's public folder by running the following command:
| php artisan vendor:publish --force --tag=flasher-assets
| php artisan flasher:install
|
| This will copy the necessary assets to your application's public folder.
*/
+1 -1
View File
@@ -132,7 +132,7 @@ abstract class ServiceProvider extends BaseServiceProvider
/**
* @return string
*/
protected function getConfigurationFile()
public function getConfigurationFile()
{
return rtrim($this->getResourcesDir(), '/').'/config.php';
}
@@ -0,0 +1,6 @@
flasher_noty:
scripts:
cdn:
- 'https://cdn.jsdelivr.net/npm/@flasher/flasher-noty@1.2.4/dist/flasher-noty.min.js'
local:
- '/vendor/flasher/flasher-noty.min.js'
@@ -0,0 +1,6 @@
flasher_notyf:
scripts:
cdn:
- 'https://cdn.jsdelivr.net/npm/@flasher/flasher-notyf@1.2.4/dist/flasher-notyf.min.js'
local:
- '/vendor/flasher/flasher-notyf.min.js'
@@ -0,0 +1,6 @@
flasher_pnotify:
scripts:
cdn:
- 'https://cdn.jsdelivr.net/npm/@flasher/flasher-pnotify@1.2.4/dist/flasher-pnotify.min.js'
local:
- '/vendor/flasher/flasher-pnotify.min.js'
+1 -1
View File
@@ -20,7 +20,7 @@ namespace Flasher\Prime\Config;
* }>,
* auto_render: bool,
* auto_translate: bool,
* search_criteria: array<string, mixed>,
* filter_criteria: array<string, mixed>,
* flash_bag: array{
* enabled: bool,
* mapping: array<string, string[]>,
@@ -0,0 +1,6 @@
flasher_sweetalert:
scripts:
cdn:
- 'https://cdn.jsdelivr.net/npm/@flasher/flasher-sweetalert@1.2.4/dist/flasher-sweetalert.min.js'
local:
- '/vendor/flasher/flasher-sweetalert.min.js'
+92 -61
View File
@@ -7,10 +7,10 @@
namespace Flasher\Symfony\Command;
use Flasher\Prime\Plugin\PluginInterface;
use Flasher\Symfony\Bridge\Bridge;
use Flasher\Symfony\Support\Bundle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Container;
@@ -20,16 +20,6 @@ use Symfony\Component\HttpKernel\KernelInterface;
class InstallCommand extends Command
{
/**
* @var string
*/
private $projectDir;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @return void
*/
@@ -37,9 +27,8 @@ class InstallCommand extends Command
{
$this
->setName('flasher:install')
->setDescription('Install all of the PHPFlasher resources.')
->addArgument('target', InputArgument::OPTIONAL, 'The target directory')
;
->setDescription('Installs all <fg=blue;options=bold>PHPFlasher</> resources to the <comment>public</comment> and <comment>config</comment> directories.')
->setHelp('The command copies <fg=blue;options=bold>PHPFlasher</> assets to <comment>public/vendor/flasher/</comment> directory and config files to the <comment>config/packages/</comment> directory without overwriting any existing config files.');
}
/**
@@ -47,9 +36,6 @@ class InstallCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$publicDir = $this->getPublicDirectory($input);
$publicDir = rtrim($publicDir, '/').'/vendor/flasher/';
$output->writeln('');
$output->writeln('<fg=blue;options=bold>
██████╗ ██╗ ██╗██████╗ ███████╗██╗ █████╗ ███████╗██╗ ██╗███████╗██████╗
@@ -62,31 +48,26 @@ class InstallCommand extends Command
$output->writeln('');
$output->writeln('');
$output->writeln(sprintf('<bg=blue;options=bold> INFO </> Copying <fg=blue;options=bold>PHPFlasher</> assets into <fg=blue;options=bold>%s</>', $publicDir));
$output->writeln('<bg=blue;options=bold> INFO </> Copying <fg=blue;options=bold>PHPFlasher</> resources...');
$output->writeln('');
$this->filesystem->remove($publicDir);
$publicDir = $this->getPublicDir().'/vendor/flasher/';
$configDir = $this->getConfigDir();
$exitCode = 0;
/** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel();
foreach ($kernel->getBundles() as $bundle) {
if (!$bundle instanceof Bundle) {
continue;
}
$plugin = $bundle->createPlugin();
$originDir = $plugin->getAssetsDir();
if (!is_dir($originDir)) {
continue;
}
$configFile = $bundle->getConfigurationFile();
try {
$this->filesystem->mkdir($originDir, 0777);
$this->filesystem->mirror($originDir, $publicDir, Finder::create()->ignoreDotFiles(false)->in($originDir));
$this->publishAssets($plugin, $publicDir);
$this->publishConfig($plugin, $configDir, $configFile);
$status = sprintf('<fg=green;options=bold>%s</>', '\\' === \DIRECTORY_SEPARATOR ? 'OK' : "\xE2\x9C\x94" /* HEAVY CHECK MARK (U+2714) */);
$output->writeln(sprintf(' %s <fg=blue;options=bold>%s</>', $status, $plugin->getAlias()));
@@ -99,72 +80,122 @@ class InstallCommand extends Command
$output->writeln('');
if (0 === $exitCode) {
$output->writeln('<bg=green;options=bold> SUCCESS </> <fg=blue;options=bold>PHPFlasher</> resources have been successfully installed.');
} else {
$output->writeln('<bg=red;options=bold> ERROR </> An error occurred during the installation of <fg=blue;options=bold>PHPFlasher</> resources.');
}
$output->writeln('');
return $exitCode;
}
/**
* {@inheritdoc}
* @param string|null $publicDir
*
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output)
private function publishAssets(PluginInterface $plugin, $publicDir)
{
$this->filesystem = new Filesystem();
/** @var Container $container */
$container = $this->getApplication()->getKernel()->getContainer();
if ($container->hasParameter('kernel.project_dir')) {
$this->projectDir = realpath($container->getParameter('kernel.project_dir')) ?: '';
} elseif ($container->hasParameter('kernel.root_dir')) {
$this->projectDir = realpath($container->getParameter('kernel.root_dir').'/../') ?: '';
if (null === $publicDir) {
return;
}
$originDir = $plugin->getAssetsDir();
if (!is_dir($originDir)) {
return;
}
$filesystem = new Filesystem();
$filesystem->mkdir($originDir, 0777);
$filesystem->mirror($originDir, $publicDir, Finder::create()->ignoreDotFiles(false)->in($originDir));
}
/**
* @return string
* @param string|null $configDir
* @param string $configFile
*
* @return void
*/
private function getPublicDirectory(InputInterface $input)
private function publishConfig(PluginInterface $plugin, $configDir, $configFile)
{
$targetDir = $this->getTargetDirectory($input);
if (is_dir($targetDir)) {
return $targetDir;
if (null === $configDir || !file_exists($configFile)) {
return;
}
$publicDir = $this->projectDir.'/'.$targetDir;
$target = $configDir.$plugin->getName().'.yaml';
if (file_exists($target)) {
return;
}
$filesystem = new Filesystem();
$filesystem->copy($configFile, $target);
}
/**
* @return string|null
*/
private function getPublicDir()
{
$projectDir = $this->getProjectDir();
$publicDir = Bridge::versionCompare('4', '>=') ? '/public' : '/web';
$publicDir = rtrim($projectDir, '/').$publicDir;
if (is_dir($publicDir)) {
return $publicDir;
}
throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $publicDir));
return $this->getComposerDir('public-dir');
}
/**
* @return string|null
*/
private function getConfigDir()
{
$projectDir = $this->getProjectDir();
$configDir = Bridge::versionCompare('4', '>=') ? '/config/packages/' : '/config';
$configDir = rtrim($projectDir, '/').$configDir;
if (is_dir($configDir)) {
return $configDir;
}
return $this->getComposerDir('config-dir');
}
/**
* @return string
*/
private function getTargetDirectory(InputInterface $input)
private function getProjectDir()
{
$targetDir = rtrim($input->getArgument('target') ?: '', '/');
if ($targetDir) {
return $targetDir;
}
/** @var Container $container */
$container = $this->getApplication()->getKernel()->getContainer();
$defaultPublicDir = Bridge::versionCompare('4', '>=') ? 'public' : 'web';
return $container->hasParameter('kernel.project_dir')
? $container->getParameter('kernel.project_dir')
: $container->getParameter('kernel.root_dir').'/../';
}
if (null === $this->projectDir) {
return $defaultPublicDir;
}
/**
* @return string|null
*/
private function getComposerDir($dir)
{
$projectDir = $this->getProjectDir();
$composerFilePath = $this->projectDir.'/composer.json';
$composerFilePath = $projectDir.'/composer.json';
if (!file_exists($composerFilePath)) {
return $defaultPublicDir;
return null;
}
$composerConfig = json_decode(file_get_contents($composerFilePath), true);
return isset($composerConfig['extra']['public-dir'])
? $composerConfig['extra']['public-dir']
: $defaultPublicDir;
return isset($composerConfig['extra'][$dir]) ? $composerConfig['extra'][$dir] : null;
}
}
@@ -38,7 +38,8 @@ final class Configuration implements ConfigurationInterface
->cannotBeEmpty()
->defaultValue($plugin->getDefault())
->end()
->scalarNode('root_script')
->arrayNode('root_script')
->prototype('scalar')->end()
->defaultValue($plugin->getRootScript())
->end()
->arrayNode('options')
@@ -47,7 +48,7 @@ final class Configuration implements ConfigurationInterface
->booleanNode('use_cdn')->defaultTrue()->end()
->booleanNode('auto_translate')->defaultTrue()->end()
->booleanNode('auto_render')->defaultTrue()->end()
->arrayNode('search_criteria')
->arrayNode('filter_criteria')
->prototype('scalar')->end()
->end()
->end()
@@ -154,7 +154,7 @@ final class FlasherExtension extends Extension implements CompilerPassInterface
*/
private function registerStorageManager(array $config, ContainerBuilder $container)
{
$criteria = $config['search_criteria'];
$criteria = $config['filter_criteria'];
$storageManager = $container->getDefinition('flasher.storage_manager');
$storageManager->replaceArgument(2, $criteria);
}
+155
View File
@@ -0,0 +1,155 @@
flasher:
# --------------------------------------------------------------------------
# Default PHPFlasher library
# --------------------------------------------------------------------------
# This option controls the default library that will be used by PHPFlasher
# to display notifications in your Symfony application. PHPFlasher supports
# several libraries, including "flasher", "toastr", "noty", "notyf",
# "sweetalert" and "pnotify".
#
# The "flasher" library is used by default. If you want to use a different
# library, you will need to install it using composer. For example, to use
# the "toastr" library, run the following command:
# composer require php-flasher/flasher-toastr-symfony
#
# Here is a list of the supported libraries and the corresponding composer
# commands to install them:
#
# "toastr" : composer require php-flasher/flasher-toastr-symfony
# "noty" : composer require php-flasher/flasher-noty-symfony
# "notyf" : composer require php-flasher/flasher-notyf-symfony
# "sweetalert" : composer require php-flasher/flasher-sweetalert-symfony
# "pnotify" : composer require php-flasher/flasher-pnotify-symfony
#
default: flasher
# --------------------------------------------------------------------------
# Main PHPFlasher javascript file
# --------------------------------------------------------------------------
# This option specifies the location of the main javascript file that is
# required by PHPFlasher to display notifications in your Symfony application.
#
# By default, PHPFlasher uses a CDN to serve the latest version of the library.
# However, you can also choose to download the library locally or install it
# using npm.
#
# To use the local version of the library, run the following command:
# php bin/console flasher:install
#
# This will copy the necessary assets to your application's public folder.
# You can then specify the local path to the javascript file in the 'local'
# field of this option.
#
root_script:
cdn: 'https://cdn.jsdelivr.net/npm/@flasher/flasher@1.2.4/dist/flasher.min.js'
local: '/vendor/flasher/flasher.min.js'
# --------------------------------------------------------------------------
# Whether to use CDN for PHPFlasher assets or not
# --------------------------------------------------------------------------
# This option controls whether PHPFlasher should use CDN links or local assets
# for its javascript and CSS files. By default, PHPFlasher uses CDN links
# to serve the latest version of the library. However, you can also choose
# to use local assets by setting this option to 'false'.
#
# If you decide to use local assets, don't forget to publish the necessary
# files to your application's public folder by running the following command:
# php bin/console flasher:install
#
# This will copy the necessary assets to your application's public folder.
#
use_cdn: true
# --------------------------------------------------------------------------
# Translate PHPFlasher messages
# --------------------------------------------------------------------------
# This option controls whether PHPFlasher should pass its messages to the Symfony's
# translation service for localization.
#
# By default, this option is set to 'true', which means that PHPFlasher will
# attempt to translate its messages using the translation service.
#
# If you don't want PHPFlasher to use the Symfony's translation service, you can
# set this option to 'false'. In this case, PHPFlasher will use the messages
# as-is, without attempting to translate them.
#
auto_translate: true
# --------------------------------------------------------------------------
# Inject PHPFlasher in Response
# --------------------------------------------------------------------------
# This option controls whether PHPFlasher should automatically inject its
# javascript and CSS files into the HTML response of your Symfony application.
#
# By default, this option is set to 'true', which means that PHPFlasher will
# listen to the response of your application and automatically insert its
# scripts and stylesheets into the HTML before the closing `</body>` tag.
#
# If you don't want PHPFlasher to automatically inject its scripts and stylesheets
# into the response, you can set this option to 'false'. In this case, you will
# need to manually include the necessary files in your application's layout.
#
auto_render: true
flash_bag:
# -----------------------------------------------------------------------
# Enable flash bag
# -----------------------------------------------------------------------
# This option controls whether PHPFlasher should automatically convert
# Symfony's flash messages to PHPFlasher notifications. This feature is
# useful when you want to migrate from a legacy system or another
# library that uses similar conventions for flash messages.
#
# When this option is set to 'true', PHPFlasher will check for flash
# messages in the session and convert them to notifications using the
# mapping specified in the 'mapping' option. When this option is set
# to 'false', PHPFlasher will ignore flash messages in the session.
#
enabled: true
# -----------------------------------------------------------------------
# Flash bag type mapping
# -----------------------------------------------------------------------
# This option allows you to map or convert session keys to PHPFlasher
# notification types. On the left side are the PHPFlasher types.
# On the right side are the Symfony session keys that you want to
# convert to PHPFlasher types.
#
# For example, if you want to convert Symfony's 'danger' flash
# messages to PHPFlasher's 'error' notifications, you can add
# the following entry to the mapping:
# error: ['danger'],
#
mapping:
success: ['success']
error: ['error', 'danger']
warning: ['warning', 'alarm']
info: ['info', 'notice', 'alert']
# -----------------------------------------------------------------------
# Global Filter Criteria
# -----------------------------------------------------------------------
# This option allows you to filter the notifications that are displayed
# in your Symfony application. By default, all notifications are displayed,
# but you can use this option to limit the number of notifications or
# filter them by type.
#
# For example, to limit the number of notifications to 5, you can set
# the 'limit' field to 5:
# limit: 5
#
# To filter the notifications by type, you can specify an array of
# types that you want to display. For example, to only display
# error notifications, you can set the 'types' field to ['error']:
# types: ['error'],
#
# You can also combine multiple criteria by specifying multiple fields.
# For example, to display up to 5 error notifications, you can set
# the 'limit' and 'types' fields like this:
# limit: 5,
# types: ['error'],
#
filter_criteria:
limit: 5 # Limit the number of notifications to display
+15
View File
@@ -21,4 +21,19 @@ abstract class Bundle extends FlasherBundle
{
return new Extension($this->createPlugin());
}
public function getConfigurationFile()
{
return rtrim($this->getResourcesDir(), '/').'/config/config.yaml';
}
/**
* @return string
*/
protected function getResourcesDir()
{
$r = new \ReflectionClass($this);
return pathinfo($r->getFileName() ?: '', PATHINFO_DIRNAME).'/Resources/';
}
}
+2 -2
View File
@@ -34,11 +34,11 @@ class Configuration implements ConfigurationInterface
$rootNode
->children()
->arrayNode('scripts')
->prototype('scalar')->end()
->prototype('variable')->end()
->defaultValue($this->plugin->getScripts())
->end()
->arrayNode('styles')
->prototype('scalar')->end()
->prototype('variable')->end()
->defaultValue($this->plugin->getStyles())
->end()
->arrayNode('options')
+13
View File
@@ -10,6 +10,7 @@ namespace Flasher\Symfony\Support;
use Flasher\Prime\Plugin\PluginInterface;
use Flasher\Symfony\Bridge\Bridge;
use Flasher\Symfony\Bridge\DependencyInjection\FlasherExtension;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -62,6 +63,18 @@ final class Extension extends FlasherExtension implements CompilerPassInterface
return $this->plugin->getName();
}
/**
* Returns extension configuration.
*
* @param array<int, array<string, mixed>> $config
*
* @return ConfigurationInterface|null
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration($this->plugin);
}
/**
* {@inheritdoc}
*
@@ -0,0 +1,8 @@
flasher_toastr:
scripts:
cdn:
- 'https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js'
- 'https://cdn.jsdelivr.net/npm/@flasher/flasher-toastr@1.2.4/dist/flasher-toastr.min.js'
local:
- '/vendor/flasher/jquery.min.js'
- '/vendor/flasher/flasher-toastr.min.js'