コード例 #1
0
 /**
  * Prepare Url objects to prevent exceptions by the URL generator.
  *
  * Helper function to get us an external URL if this is legal, and to catch
  * the exception Drupal throws if this is not possible.
  *
  * In Drupal 8, the URL generator is very sensitive to how you set things
  * up, and some functions, in particular LinkGeneratorTrait::l(), will throw
  * exceptions if you deviate from what's expected. This function will raise
  * the chances your URL will be valid, and not do this.
  *
  * @param \Drupal\file\Entity\File|string $file_object
  *   A file entity object.
  *
  * @return \Drupal\Core\Url
  *   A Url object that can be displayed as an internal URL.
  */
 protected function getExternalUrl($file_object)
 {
     if ($file_object instanceof FileInterface) {
         $uri = $file_object->getFileUri();
     } else {
         // A little tricky, since file.inc is a little inconsistent, but often
         // this is a Uri.
         $uri = file_create_url($file_object);
     }
     try {
         // If we have been given a PHP stream URI, ask the stream itself if it
         // knows how to create an external URL.
         $wrapper = $this->streamWrapperManager->getViaUri($uri);
         if ($wrapper) {
             $external_url = $wrapper->getExternalUrl();
             // Some streams may not have the concept of an external URL, so we
             // check here to make sure, since the example assumes this.
             if ($external_url) {
                 $url = Url::fromUri($external_url);
                 return $url;
             }
         } else {
             $url = Url::fromUri($uri);
             // If we did not throw on ::fromUri (you can), we return the URL.
             return $url;
         }
     } catch (\Exception $e) {
         return FALSE;
     }
     return FALSE;
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function guess($path)
 {
     if ($wrapper = $this->streamWrapperManager->getViaUri($path)) {
         // Get the real path from the stream wrapper.
         $path = $wrapper->realpath();
     }
     if ($this->sortedGuessers === NULL) {
         // Sort is not triggered yet.
         $this->sortedGuessers = $this->sortGuessers();
     }
     foreach ($this->sortedGuessers as $guesser) {
         $mime_type = $guesser->guess($path);
         if ($mime_type !== NULL) {
             return $mime_type;
         }
     }
 }
コード例 #3
0
ファイル: FileSystem.php プロジェクト: ddrozdik/dmaps
 /**
  * {@inheritdoc}
  */
 public function realpath($uri)
 {
     // If this URI is a stream, pass it off to the appropriate stream wrapper.
     // Otherwise, attempt PHP's realpath. This allows use of this method even
     // for unmanaged files outside of the stream wrapper interface.
     if ($wrapper = $this->streamWrapperManager->getViaUri($uri)) {
         return $wrapper->realpath();
     }
     return realpath($uri);
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function guess($path)
 {
     if ($wrapper = $this->streamWrapperManager->getViaUri($path)) {
         // Get the real path from the stream wrapper, if available. Files stored
         // in remote file systems will not have one.
         $real_path = $wrapper->realpath();
         if ($real_path !== FALSE) {
             $path = $real_path;
         }
     }
     if ($this->sortedGuessers === NULL) {
         // Sort is not triggered yet.
         $this->sortedGuessers = $this->sortGuessers();
     }
     foreach ($this->sortedGuessers as $guesser) {
         $mime_type = $guesser->guess($path);
         if ($mime_type !== NULL) {
             return $mime_type;
         }
     }
 }