class ResourceController

Same name in other branches
  1. 8.9.x core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php \Drupal\media_test_oembed\Controller\ResourceController
  2. 10 core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php \Drupal\media_test_oembed\Controller\ResourceController
  3. 11.x core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php \Drupal\media_test_oembed\Controller\ResourceController

Test controller returning oEmbed resources from Media's test fixtures.

Hierarchy

Expanded class hierarchy of ResourceController

4 files declare their use of ResourceController
MediaSourceOEmbedVideoTest.php in core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
MediaStandardProfileTest.php in core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php
OEmbedFormatterTest.php in core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php
WidgetOEmbedTest.php in core/modules/media_library/tests/src/FunctionalJavascript/WidgetOEmbedTest.php

File

core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php, line 12

Namespace

Drupal\media_test_oembed\Controller
View source
class ResourceController {
    
    /**
     * Creates an oEmbed resource response.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The request.
     *
     * @return \Symfony\Component\HttpFoundation\Response
     *   The oEmbed resource response.
     */
    public function get(Request $request) {
        $asset_url = $request->query
            ->get('url');
        $resources = \Drupal::state()->get(static::class, []);
        if ($resources[$asset_url] === 404) {
            $response = new Response('Not Found', 404);
        }
        else {
            $content = file_get_contents($resources[$asset_url]);
            $response = new Response($content);
            $response->headers
                ->set('Content-Type', 'application/' . pathinfo($resources[$asset_url], PATHINFO_EXTENSION));
        }
        return $response;
    }
    
    /**
     * Returns an example thumbnail file without an extension.
     *
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     *   The response.
     */
    public function getThumbnailWithNoExtension() {
        $response = new BinaryFileResponse('core/misc/druplicon.png');
        $response->headers
            ->set('Content-Type', 'image/png');
        return $response;
    }
    
    /**
     * Maps an asset URL to a local fixture representing its oEmbed resource.
     *
     * @param string $asset_url
     *   The asset URL.
     * @param string $resource_path
     *   The path of the oEmbed resource representing the asset.
     */
    public static function setResourceUrl($asset_url, $resource_path) {
        $resources = \Drupal::state()->get(static::class, []);
        $resources[$asset_url] = $resource_path;
        \Drupal::state()->set(static::class, $resources);
    }
    
    /**
     * Maps an asset URL to a 404 response.
     *
     * @param string $asset_url
     *   The asset URL.
     */
    public static function setResource404($asset_url) {
        $resources = \Drupal::state()->get(static::class, []);
        $resources[$asset_url] = 404;
        \Drupal::state()->set(static::class, $resources);
    }

}

Members

Title Sort descending Modifiers Object type Summary
ResourceController::get public function Creates an oEmbed resource response.
ResourceController::getThumbnailWithNoExtension public function Returns an example thumbnail file without an extension.
ResourceController::setResource404 public static function Maps an asset URL to a 404 response.
ResourceController::setResourceUrl public static function Maps an asset URL to a local fixture representing its oEmbed resource.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.