From df4b6f214e9b2630a742a97ca7460f4e8101a0f5 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 21 Aug 2014 11:05:24 +0000 Subject: [PATCH] Add an easy way to generate assets URL --- src/mibew/libs/classes/Mibew/Application.php | 11 ++ .../classes/Mibew/Asset/AssetUrlGenerator.php | 166 ++++++++++++++++++ .../Asset/AssetUrlGeneratorAwareInterface.php | 40 +++++ .../Asset/AssetUrlGeneratorInterface.php | 47 +++++ .../Mibew/Controller/AbstractController.php | 43 ++++- .../Mibew/Controller/ControllerResolver.php | 32 +++- .../Controller/Operator/AvatarController.php | 2 +- 7 files changed, 338 insertions(+), 3 deletions(-) create mode 100644 src/mibew/libs/classes/Mibew/Asset/AssetUrlGenerator.php create mode 100644 src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorAwareInterface.php create mode 100644 src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorInterface.php diff --git a/src/mibew/libs/classes/Mibew/Application.php b/src/mibew/libs/classes/Mibew/Application.php index 3f2b76b3..12f196f1 100644 --- a/src/mibew/libs/classes/Mibew/Application.php +++ b/src/mibew/libs/classes/Mibew/Application.php @@ -20,6 +20,7 @@ namespace Mibew; use Mibew\AccessControl\Check\CheckResolver; +use Mibew\Asset\AssetUrlGenerator; use Mibew\Authentication\AuthenticationManagerInterface; use Mibew\Authentication\AuthenticationManagerAwareInterface; use Mibew\Controller\ControllerResolver; @@ -65,6 +66,11 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt */ protected $authenticationManager = null; + /** + * @var AssetUrlGenerator|null + */ + protected $assetUrlGenerator = null; + /** * Class constructor. * @@ -76,10 +82,12 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt { $this->router = $router; $this->authenticationManager = $manager; + $this->assetUrlGenerator = new AssetUrlGenerator(); $this->controllerResolver = new ControllerResolver( $this->router, $this->authenticationManager ); + $this->controllerResolver->setAssetUrlGenerator($this->assetUrlGenerator); $this->accessCheckResolver = new CheckResolver($this->authenticationManager); } @@ -105,6 +113,9 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt } $authentication_manager->setOperatorFromRequest($request); + // Actualize properties in AssetUrlGenerator + $this->assetUrlGenerator->fromRequest($request); + try { // Try to match a route, check if the client can access it and add // extra data to the request. diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetUrlGenerator.php b/src/mibew/libs/classes/Mibew/Asset/AssetUrlGenerator.php new file mode 100644 index 00000000..cc7e3eeb --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Asset/AssetUrlGenerator.php @@ -0,0 +1,166 @@ +scheme = strtolower($scheme); + $this->host = $host; + $this->port = $port; + $this->basePath = $base_path; + } + + /** + * Sets all needed values according to the request. + * + * @param Request $request A request to get values from. + */ + public function fromRequest(Request $request) + { + $this->setScheme($request->getScheme()); + $this->setHost($request->getHost()); + $this->setPort($request->getPort()); + $this->setBasePath($request->getBasePath()); + } + + /** + * Gets the scheme. + * + * @return string + */ + public function getScheme() + { + return $this->scheme; + } + + /** + * Sets the scheme. + * + * @param string $scheme + */ + public function setScheme($scheme) + { + $this->scheme = $scheme; + } + + /** + * Gets the host. + * + * @return string + */ + public function getHost() + { + return $this->host; + } + + /** + * Sets the host. + * + * @param string $host + */ + public function setHost($host) + { + $this->host = $host; + } + + /** + * Gets the port. + * + * @return int + */ + public function getPort() + { + return $this->port; + } + + /** + * Sets the port. + * + * @param int $port + */ + public function setPort($port) + { + $this->port = $port; + } + + /** + * Gets the base path. + * + * @return string + */ + public function getBasePath() + { + return $this->basePath; + } + + /** + * Sets the base path. + * + * @param string $base_path + */ + public function setBasePath($base_path) + { + $this->basePath = $base_path; + } + + /** + * {@inheritdoc} + */ + public function generate($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH) + { + $host = ''; + $need_port = ($this->getScheme() == 'http' && $this->getPort() != 80) + || ($this->getScheme() == 'https' && $this->getPort() != 443); + + if ($need_port || $reference_type === AssetUrlGeneratorInterface::ABSOLUTE_URL) { + $host = $this->getScheme() . '://' . $this->getHost(); + + if ($need_port) { + // A non standatd port is used. It should be added to the + // resulting URL. + $host .= ':' . $this->getPort(); + } + } + + // Make sure path componets are properly encoded. + $path_parts = explode('/', $relative_path); + $encoded_path = implode('/', array_map('rawurlencode', $path_parts)); + + return $host . $this->getBasePath() . '/' . ltrim($encoded_path, '/'); + } +} diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorAwareInterface.php b/src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorAwareInterface.php new file mode 100644 index 00000000..eda03cfd --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorAwareInterface.php @@ -0,0 +1,40 @@ +authenticationManager; } + /** + * {@inheritdoc} + */ + public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator) + { + $this->assetUrlGenerator = $generator; + } + + /** + * {@inheritdoc} + */ + public function getAssetUrlGenerator() + { + return $this->assetUrlGenerator; + } + /** * Generates a URL from the given parameters. * @@ -170,4 +196,19 @@ abstract class AbstractController implements RouterAwareInterface, Authenticatio { return $this->getAuthenticationManager()->getOperator(); } + + /** + * Generates URL for an asset with the specified relative path. + * + * @param string $relative_path Relative path of an asset. + * @param bool|string $reference_type Indicates what type of URL should be + * generated. It is equal to one of the + * {@link \Mibew\Asset\AssetUrlGeneratorInterface} constants. + * @return string Asset URL. + */ + public function asset($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH) + { + return $this->getAssetUrlGenerator() + ->generate($relative_path, $reference_type); + } } diff --git a/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php b/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php index c21da0c9..56f61e6b 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php +++ b/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php @@ -19,13 +19,18 @@ namespace Mibew\Controller; +use Mibew\Asset\AssetUrlGeneratorAwareInterface; +use Mibew\Asset\AssetUrlGeneratorInterface; use Mibew\Authentication\AuthenticationManagerAwareInterface; use Mibew\Authentication\AuthenticationManagerInterface; use Mibew\Routing\RouterAwareInterface; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\HttpFoundation\Request; -class ControllerResolver implements RouterAwareInterface, AuthenticationManagerAwareInterface +class ControllerResolver implements + RouterAwareInterface, + AuthenticationManagerAwareInterface, + AssetUrlGeneratorAwareInterface { /** * @var RouterInterface|null @@ -37,6 +42,11 @@ class ControllerResolver implements RouterAwareInterface, AuthenticationManagerA */ protected $authenticationManager = null; + /** + * @var AssetUrlGeneratorInterface|null + */ + protected $assetUrlGenerator = null; + /** * Class constructor. * @@ -82,6 +92,22 @@ class ControllerResolver implements RouterAwareInterface, AuthenticationManagerA $this->authenticationManager = $manager; } + /** + * {@inheritdoc} + */ + public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator) + { + $this->assetUrlGenerator = $generator; + } + + /** + * {@inheritdoc} + */ + public function getAssetUrlGenerator() + { + return $this->assetUrlGenerator; + } + /** * Resolves controller by request. * @@ -143,6 +169,10 @@ class ControllerResolver implements RouterAwareInterface, AuthenticationManagerA $object->setAuthenticationManager($this->getAuthenticationManager()); } + if ($object instanceof AssetUrlGeneratorAwareInterface) { + $object->setAssetUrlGenerator($this->getAssetUrlGenerator()); + } + return array($object, $method); } } diff --git a/src/mibew/libs/classes/Mibew/Controller/Operator/AvatarController.php b/src/mibew/libs/classes/Mibew/Controller/Operator/AvatarController.php index 93fbc0e8..93da5528 100644 --- a/src/mibew/libs/classes/Mibew/Controller/Operator/AvatarController.php +++ b/src/mibew/libs/classes/Mibew/Controller/Operator/AvatarController.php @@ -123,7 +123,7 @@ class AvatarController extends AbstractController // Move uploaded file to avatar directory try { $file->move($avatar_local_dir, $new_file_name); - $avatar = $request->getBasePath() . "/files/avatar/" . $new_file_name; + $avatar = $this->asset('files/avatar/' . $new_file_name); } catch (Exception $e) { $errors[] = failed_uploading_file($orig_filename, "Error moving file"); }