/**
  * Returns the library definitions for a given extension.
  *
  * This also implements libraries-overrides for entire libraries that have
  * been specified by the LibraryDiscoveryParser.
  *
  * @param string $extension
  *   The name of the extension for which library definitions will be returned.
  *
  * @return array
  *   The library definitions for $extension with overrides applied.
  *
  * @throws \Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException
  */
 protected function getLibraryDefinitions($extension)
 {
     $libraries = $this->discoveryParser->buildByExtension($extension);
     foreach ($libraries as $name => $definition) {
         // Handle libraries that are marked for override or removal.
         // @see \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride()
         if (isset($definition['override'])) {
             if ($definition['override'] === FALSE) {
                 // Remove the library definition if FALSE is given.
                 unset($libraries[$name]);
             } else {
                 // Otherwise replace with existing library definition if it exists.
                 // Throw an exception if it doesn't.
                 list($replacement_extension, $replacement_name) = explode('/', $definition['override']);
                 $replacement_definition = $this->get($replacement_extension);
                 if (isset($replacement_definition[$replacement_name])) {
                     $libraries[$name] = $replacement_definition[$replacement_name];
                 } else {
                     throw new InvalidLibrariesOverrideSpecificationException(sprintf('The specified library %s does not exist.', $definition['override']));
                 }
             }
         } else {
             // If libraries are not overridden, then apply libraries-extend.
             $libraries[$name] = $this->applyLibrariesExtend($extension, $name, $definition);
         }
     }
     return $libraries;
 }
Ejemplo n.º 2
0
 /**
  * Tests a library with various licenses, some GPL-compatible, some not.
  *
  * @covers ::buildByExtension
  */
 public function testLibraryWithLicenses()
 {
     $this->moduleHandler->expects($this->atLeastOnce())->method('moduleExists')->with('licenses')->will($this->returnValue(TRUE));
     $path = __DIR__ . '/library_test_files';
     $path = substr($path, strlen($this->root) + 1);
     $this->libraryDiscoveryParser->setPaths('module', 'licenses', $path);
     $libraries = $this->libraryDiscoveryParser->buildByExtension('licenses');
     // For libraries without license info, the default license is applied.
     $library = $libraries['no-license-info'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $this->assertTrue(isset($library['license']));
     $default_license = array('name' => 'GNU-GPL-2.0-or-later', 'url' => 'https://www.drupal.org/licensing/faq', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $default_license);
     // GPL2-licensed libraries.
     $library = $libraries['gpl2'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'gpl2', 'url' => 'https://url-to-gpl2-license', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $expected_license);
     // MIT-licensed libraries.
     $library = $libraries['mit'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'MIT', 'url' => 'https://url-to-mit-license', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $expected_license);
     // Libraries in the Public Domain.
     $library = $libraries['public-domain'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'Public Domain', 'url' => 'https://url-to-public-domain-license', 'gpl-compatible' => TRUE);
     $this->assertEquals($library['license'], $expected_license);
     // Apache-licensed libraries.
     $library = $libraries['apache'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => 'apache', 'url' => 'https://url-to-apache-license', 'gpl-compatible' => FALSE);
     $this->assertEquals($library['license'], $expected_license);
     // Copyrighted libraries.
     $library = $libraries['copyright'];
     $this->assertCount(1, $library['css']);
     $this->assertCount(1, $library['js']);
     $expected_license = array('name' => '© Some company', 'gpl-compatible' => FALSE);
     $this->assertEquals($library['license'], $expected_license);
 }
 /**
  * {@inheritdoc}
  */
 protected function resolveCacheMiss($key)
 {
     $this->storage[$key] = $this->discoveryParser->buildByExtension($key);
     $this->persist($key);
     return $this->storage[$key];
 }