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

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 $containerKernelInterface $kernelMappingResource $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 $pathbool $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.         
  62.         $scannedFiles $this->scanDirectory($path);
  63.         if (!empty($scannedFiles)) {
  64.             foreach ($scannedFiles as $filepath) {
  65.                 if (!is_dir($filepath) || is_link($filepath)) {
  66.                     unlink($filepath);
  67.                 } else {
  68.                     if (null != $this->scanDirectory($filepath)) {
  69.                         $this->emptyDirectory($filepath);
  70.                     }
  71.                     rmdir($filepath);
  72.                 }
  73.             }
  74.         }
  75.     }
  76.     private function symlinkAvailablePackageResources()
  77.     {
  78.         $collection = [];
  79.         $prefix dirname(__DIR__) . '/Resources/public/extensions';
  80.         $prefix $this->container->getParameter('kernel.project_dir') . '/public/community-packages';
  81.         foreach (array_keys($this->mappingResource->getPackages()) as $id) {
  82.             $packageReflectionClass = new \ReflectionClass($id);
  83.             $attributes $this->mappingResource->getPackage($id);
  84.             list($vendorName$packageName) = explode('/'$attributes['metadata']['name']);
  85.             $source dirname($packageReflectionClass->getFileName()) . '/Resources/public';
  86.             if (file_exists($source) && is_dir($source)) {
  87.                 $collection[] = [
  88.                     'source' => $source,
  89.                     'destination' => [
  90.                         'base' => "$prefix/$vendorName",
  91.                         'path' => "$prefix/$vendorName/$packageName",
  92.                     ],
  93.                 ];
  94.             }
  95.         }
  96.         // Clear existing resources from extensions directory
  97.         if (file_exists($prefix) && is_dir($prefix)) {
  98.             $this->emptyDirectory($prefix);
  99.         }
  100.         // Link package assets within bundle assets
  101.         foreach ($collection as $package) {
  102.             if (!is_dir($package['destination']['base'])) {
  103.                 mkdir($package['destination']['base'], 0755true);
  104.             }
  105.             symlink($package['source'], $package['destination']['path']);
  106.         }
  107.     }
  108. }