view_mode_crop-1.0.x-dev/src/StreamWrapper/CropPublicStreamWrapper.php
src/StreamWrapper/CropPublicStreamWrapper.php
<?php
namespace Drupal\view_mode_crop\StreamWrapper;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\HttpFoundation\Request;
/**
* Crop-public:// stream wrapper.
*/
class CropPublicStreamWrapper extends CropStreamWrapper {
/**
* {@inheritdoc}
*/
public function getDirectoryPath() {
return static::basePath();
}
/**
* {@inheritdoc}
*/
public static function getType() {
return StreamWrapperInterface::LOCAL;
}
/**
* {@inheritdoc}
*/
public function getName() {
return new TranslatableMarkup('Crop public files');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return new TranslatableMarkup('Crop public local files served by the webserver.');
}
/**
* Returns the base path for crop-public://.
*
* @param string $site_path
* (optional) The site.path service parameter, which is typically the path
* to sites/ in a Drupal installation. This allows you to inject the site
* path using services from the caller. If omitted, this method will use
* the
* global service container or the kernel's default behavior to determine
* the site path.
*
* @return string
* The base path for crop-public:// typically sites/default/files.
*/
public static function basePath($site_path = NULL) {
if ($site_path === NULL) {
// Find the site path. Kernel service is not always available at this
// point, but is preferred, when available.
if (\Drupal::hasService('kernel')) {
$site_path = \Drupal::getContainer()->getParameter('site.path');
}
else {
// If there is no kernel available yet, we call the static
// findSitePath().
$site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
}
}
return Settings::get('file_public_path', $site_path . '/files') . '/crop';
}
/**
* Finds and returns the base URL for crop-public://.
*
* @return string
* The external base URL for crop-public://
*/
public static function baseUrl() {
return $GLOBALS['base_url'] . '/' . static::basePath();
}
/**
* {@inheritdoc}
*/
public function getExternalUrl() {
$path = str_replace('\\', '/', $this->getTarget());
return static::baseUrl() . '/' . UrlHelper::encodePath($path);
}
}
