vendor/uvdesk/extension-framework/EventListener/Console.php line 23

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\ExtensionFrameworkBundle\EventListener;
  3. use Symfony\Component\Console\Input\ArrayInput;
  4. use Symfony\Component\HttpKernel\KernelInterface;
  5. use Symfony\Bundle\FrameworkBundle\Console\Application;
  6. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  7. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Command as SymfonyFrameworkCommand;
  10. use Webkul\UVDesk\ExtensionFrameworkBundle\Definition\MappingResource;
  11. class Console
  12. {
  13. private $kernel;
  14. private $container;
  15. public function __construct(ContainerInterface $container, KernelInterface $kernel, MappingResource $mappingResource)
  16. {
  17. $this->kernel = $kernel;
  18. $this->container = $container;
  19. $this->mappingResource = $mappingResource;
  20. }
  21. public function onConsoleCommand(ConsoleCommandEvent $event)
  22. {
  23. $command = $event->getCommand();
  24. switch (true) {
  25. case $command instanceof SymfonyFrameworkCommand\CacheClearCommand:
  26. // $application = new Application($this->kernel);
  27. // $application->setAutoExit(false);
  28. // $application->run(new ArrayInput(['command' => 'uvdesk_extensions:build']), $event->getOutput());
  29. break;
  30. case $command instanceof SymfonyFrameworkCommand\AssetsInstallCommand:
  31. // Update symbolic links between packages
  32. $this->symlinkAvailablePackageResources();
  33. break;
  34. default:
  35. break;
  36. }
  37. return;
  38. }
  39. public function onConsoleTerminate(ConsoleTerminateEvent $event)
  40. {
  41. return;
  42. }
  43. private function scanDirectory(string $path, bool $return_full_path = true)
  44. {
  45. if (!file_exists($path) || !is_dir($path)) {
  46. throw new \Exception("Not a directory : '$path'");
  47. }
  48. $scannedFiles = array_diff(scandir($path), ['.', '..']);
  49. if ($return_full_path) {
  50. $scannedFiles = array_map(function ($file) use ($path) {
  51. return "$path/$file";
  52. }, $scannedFiles);
  53. }
  54. return $scannedFiles;
  55. }
  56. private function emptyDirectory(string $path)
  57. {
  58. if (!file_exists($path) || !is_dir($path)) {
  59. throw new \Exception("Not a directory : '$path'");
  60. }
  61. $scannedFiles = $this->scanDirectory($path);
  62. if (!empty($scannedFiles)) {
  63. foreach ($scannedFiles as $filepath) {
  64. if (!is_dir($filepath) || is_link($filepath)) {
  65. unlink($filepath);
  66. } else {
  67. if (null != $this->scanDirectory($filepath)) {
  68. $this->emptyDirectory($filepath);
  69. }
  70. rmdir($filepath);
  71. }
  72. }
  73. }
  74. }
  75. private function symlinkAvailablePackageResources()
  76. {
  77. $collection = [];
  78. $prefix = dirname(__DIR__) . '/Resources/public/extensions';
  79. $prefix = $this->container->getParameter('kernel.project_dir') . '/public/community-packages';
  80. foreach (array_keys($this->mappingResource->getPackages()) as $id) {
  81. $packageReflectionClass = new \ReflectionClass($id);
  82. $attributes = $this->mappingResource->getPackage($id);
  83. list($vendorName, $packageName) = explode('/', $attributes['metadata']['name']);
  84. $source = dirname($packageReflectionClass->getFileName()) . '/Resources/public';
  85. if (file_exists($source) && is_dir($source)) {
  86. $collection[] = [
  87. 'source' => $source,
  88. 'destination' => [
  89. 'base' => "$prefix/$vendorName",
  90. 'path' => "$prefix/$vendorName/$packageName",
  91. ],
  92. ];
  93. }
  94. }
  95. // Clear existing resources from extensions directory
  96. if (file_exists($prefix) && is_dir($prefix)) {
  97. $this->emptyDirectory($prefix);
  98. }
  99. // Link package assets within bundle assets
  100. foreach ($collection as $package) {
  101. if (!is_dir($package['destination']['base'])) {
  102. mkdir($package['destination']['base'], 0755, true);
  103. }
  104. symlink($package['source'], $package['destination']['path']);
  105. }
  106. }
  107. }