/**
  * {@inheritdoc}
  */
 public function process(ContainerInterface $container)
 {
     /**
      * @var LibraryDefinitionInterface $definition
      */
     foreach ($definitions = $container->getLibraries()->getDefinitions() as $definition) {
         /**
          * @var ResourceInterface $resource
          */
         foreach ($resources = $definition->getResources() as $resource) {
             if ($resource instanceof GlobResource) {
                 $files = glob($resource->getSource());
                 if (count($files) > 0) {
                     $globResources = [];
                     foreach ($files as $file) {
                         $globResources[] = new FileResource($file, $resource->getOptions());
                     }
                     $definition->replaceResource($resource, $globResources);
                 } else {
                     throw new \InvalidArgumentException(sprintf('glob() pattern "%s" for definition "%s" yields no results.', $resource->getSource(), $definition->getName()));
                 }
             }
         }
     }
     return new CompilerPassResult($container);
 }
示例#2
0
 public function render($type, $position = null, array $options = [])
 {
     if (!in_array($type = strtolower($type), array(AssetType::JAVASCRIPT, AssetType::STYLESHEET))) {
         throw new InvalidArgumentException(sprintf('Unsupported asset type "".', $type));
     }
     return $this->resourceRenderer->render($this->container->eject($type, $position), array_merge($this->options, $options));
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContainerInterface $container)
 {
     if (count($this->options['include'])) {
         /**
          * @var LibraryDefinitionInterface $definition
          */
         foreach ($definitions = $container->getLibraries()->getDefinitions() as $definition) {
             $hasHelper = false;
             /**
              * @var ResourceInterface $resource
              */
             foreach ($resources = $definition->getResources() as $resource) {
                 if ($resource instanceof HttpResource && in_array(AssetType::guessExtension($resource->getSource()), $this->options['include']) && ($replacement = $this->processResource($resource)) != $resource) {
                     if ($hasHelper) {
                         $definition->replaceResource($resource, [$replacement]);
                     } else {
                         $hasHelper = true;
                         $definition->replaceResource($resource, [new FileResource($this->options['helper']), $replacement]);
                     }
                 }
             }
         }
     }
     return new CompilerPassResult($container);
 }
 public function dump(ContainerInterface $container, array $options = [])
 {
     $variables = array_merge(['namespace' => null, 'classname' => null, 'extends' => '\\RunOpenCode\\AssetsInjection\\Container', 'libraries' => $container->getLibraries(), 'type' => self::DYNAMIC_CONTAINER], $options);
     if (!in_array($variables['type'], array(self::DYNAMIC_CONTAINER, self::STATIC_CONTAINER))) {
         throw new RuntimeException(sprintf('Unknown container type "%s" requested for dumping.', $variables['template']));
     }
     return $this->renderer->render(implode(DIRECTORY_SEPARATOR, array(__DIR__, 'templates', sprintf('%s.template.php', $variables['type']))), $variables);
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContainerInterface $container)
 {
     /**
      * @var LibraryDefinitionInterface $definition
      */
     foreach ($definitions = $container->getLibraries()->getDefinitions() as $name => $definition) {
         $this->doCheckCircularReferences($container, $definition, array($name => $name));
     }
     return new CompilerPassResult($container);
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContainerInterface $container)
 {
     /**
      * @var LibraryDefinitionInterface $definition
      */
     foreach ($definitions = $container->getLibraries()->getDefinitions() as $name => $definition) {
         foreach ($resources = $definition->getResources() as $resource) {
             if (!$resource instanceof FileResource && !$resource instanceof HttpResource && !$resource instanceof JavascriptStringResource && !$resource instanceof StylesheetStringResource) {
                 throw new RuntimeException(sprintf('This container can not be rendered. Maybe you didn\'t included all necessary compiler passes?'));
             }
         }
     }
     return new CompilerPassResult($container);
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContainerInterface $container)
 {
     $methods = array_filter(get_class_methods($this), function ($method) {
         return strpos($method, 'check') === 0;
     });
     /**
      * @var LibraryDefinitionInterface $definition
      */
     foreach ($definitions = $container->getLibraries()->getDefinitions() as $definition) {
         foreach ($methods as $method) {
             call_user_func_array(array($this, $method), array($container, $definition));
         }
     }
     return new CompilerPassResult($container);
 }
 /**
  * Check if resource is available.
  *
  * @param ContainerInterface $container Current container.
  * @param LibraryDefinitionInterface $definition Current definition.
  * @param ResourceInterface $resource Current resource.
  * @throws UnavailableResourceException
  * @throws RuntimeException
  */
 public function checkResourceAvailability(ContainerInterface $container, LibraryDefinitionInterface $definition, ResourceInterface $resource)
 {
     if ($this->options['check_availability']) {
         if ($resource instanceof FileResource && $this->options['check_availability']['file_resource']) {
             if (!@file_exists($resource->getSource())) {
                 throw new UnavailableResourceException(sprintf('File resource "%s" does not exists.', $resource->getSource()));
             }
             if (!@is_file($resource->getSource())) {
                 throw new UnavailableResourceException(sprintf('File resource "%s" is not file.', $resource->getSource()));
             }
             if (!@is_readable($resource->getSource())) {
                 throw new UnavailableResourceException(sprintf('File resource "%s" is not readable.', $resource->getSource()));
             }
         } elseif ($resource instanceof GlobResource && $this->options['check_availability']['glob_resource']) {
             $files = $resource->getFiles();
             if (count($files) == 0) {
                 throw new InvalidArgumentException(sprintf('Glob resource "%s" yields no results.', $resource->getSource()));
             }
             foreach ($files as $file) {
                 if (!@file_exists($file)) {
                     throw new UnavailableResourceException(sprintf('Glob "%s" resulting file "%s" does not exists.', $resource->getSource(), $file));
                 }
                 if (!@is_file($file)) {
                     throw new UnavailableResourceException(sprintf('Glob "%s" result "%s" is not file.', $resource->getSource(), $file));
                 }
                 if (!@is_readable($file)) {
                     throw new UnavailableResourceException(sprintf('Glob "%s" resulting file "%s" is not readable.', $resource->getSource(), $file));
                 }
             }
         } elseif ($resource instanceof HttpResource && $this->options['check_availability']['http_resource']) {
             if (strpos($url = $resource->getSource(), '//') === 0) {
                 $url = 'http:' . $url;
             } elseif (strpos($url, '://') === false) {
                 throw new InvalidArgumentException(sprintf('Http resource "%s" is not valid.', $resource->getSource()));
             }
             if (@file_get_contents($url, false, stream_context_create(array('http' => array('method' => 'HEAD')))) === false) {
                 throw new UnavailableResourceException(sprintf('Http resource "%s" is not available.', $resource->getSource()));
             }
         } elseif ($resource instanceof ReferenceResource && $this->options['check_availability']['reference_resource']) {
             if (!$container->getLibraries()->hasDefinition($resource->getSource())) {
                 throw new UnavailableResourceException(sprintf('Reference resource "%s" references to non-existing library.', $resource->getSource()));
             }
         } elseif ($resource instanceof StringResource) {
             // Always available
         } else {
             throw new RuntimeException(sprintf('Unable to determine resource availability of instance of "%s".', get_class($resource)));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContainerInterface $container)
 {
     /**
      * @var LibraryDefinitionInterface $definition
      */
     foreach ($definitions = $container->getLibraries()->getDefinitions() as $definition) {
         /**
          * @var ResourceInterface $resource
          */
         foreach ($currentResources = $definition->getResources() as $resource) {
             if (isset($resource->getOptions()['filters']) && count($resource->getOptions()['filters'])) {
                 $definition->replaceResource($resource, [$this->filterResource($resource, $this->getFilters($resource))]);
             }
         }
     }
     return new CompilerPassResult($container);
 }
 /**
  * Get referenced resources for referencing resource.
  *
  * @param ReferenceResource $referencingResource        Referencing resource to replace.
  * @param ContainerInterface $container                 Container which is subject of process.
  * @return array                                        Collection of concrete resources.
  */
 private function getReferencedResources(ReferenceResource $referencingResource, ContainerInterface $container)
 {
     $result = [];
     $definition = $container->getLibraries()->getDefinition($referencingResource->getSource());
     /**
      * @var ResourceInterface $resource
      */
     foreach ($resources = $definition->getResources() as $resource) {
         if (!in_array($resource, $result)) {
             $result[] = $resource;
         }
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContainerInterface $container)
 {
     $container->getLibraries()->addDefinition(new LibraryDefinition($this->definitionTestNameMarker));
     return new CompilerPassResult($container, $this->stopProcessing);
 }