mirror of
https://github.com/Mibew/mibew.git
synced 2025-03-03 18:38:31 +03:00
Add an easy way to generate assets URL
This commit is contained in:
parent
f0f41cbfb6
commit
df4b6f214e
@ -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.
|
||||
|
166
src/mibew/libs/classes/Mibew/Asset/AssetUrlGenerator.php
Normal file
166
src/mibew/libs/classes/Mibew/Asset/AssetUrlGenerator.php
Normal file
@ -0,0 +1,166 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Asset;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class AssetUrlGenerator implements AssetUrlGeneratorInterface
|
||||
{
|
||||
|
||||
protected $scheme;
|
||||
protected $host;
|
||||
protected $port;
|
||||
protected $basePath;
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @param string $host The HTTP host name without port and scheme.
|
||||
* @param string $base_path The base path.
|
||||
* @param string $scheme The scheme.
|
||||
* @param int $port The port.
|
||||
*/
|
||||
public function __construct($host = 'localhost', $base_path = '', $scheme = 'http', $port = 80)
|
||||
{
|
||||
$this->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, '/');
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Asset;
|
||||
|
||||
/**
|
||||
* Indicates if a class is aware of AssetUrlGenerator class.
|
||||
*/
|
||||
interface AssetUrlGeneratorAwareInterface
|
||||
{
|
||||
/**
|
||||
* Gets an asset URL generator.
|
||||
*
|
||||
* @return AssetUrlGeneratorInterface
|
||||
*/
|
||||
public function getAssetUrlGenerator();
|
||||
|
||||
/**
|
||||
* Sets an asset URL generator.
|
||||
*
|
||||
* @param AssetUrlGeneratorInterface $generator
|
||||
*/
|
||||
public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator);
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Asset;
|
||||
|
||||
/**
|
||||
* UrlGeneratorInterface is the interface that all Asset URL generator classes
|
||||
* must implement.
|
||||
*/
|
||||
interface AssetUrlGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generates an absolute URL, e.g. "http://example.com/dir/file".
|
||||
*/
|
||||
const ABSOLUTE_URL = true;
|
||||
|
||||
/**
|
||||
* Generates an absolute path, e.g. "/dir/file".
|
||||
*/
|
||||
const ABSOLUTE_PATH = false;
|
||||
|
||||
/**
|
||||
* 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 interface constants.
|
||||
* @return string Asset URL.
|
||||
*/
|
||||
public function generate($relative_path, $reference_type = self::ABSOLUTE_PATH);
|
||||
}
|
@ -19,6 +19,8 @@
|
||||
|
||||
namespace Mibew\Controller;
|
||||
|
||||
use Mibew\Asset\AssetUrlGeneratorAwareInterface;
|
||||
use Mibew\Asset\AssetUrlGeneratorInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerAwareInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerInterface;
|
||||
use Mibew\Routing\RouterAwareInterface;
|
||||
@ -31,7 +33,10 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
/**
|
||||
* A base class for all controllers.
|
||||
*/
|
||||
abstract class AbstractController implements RouterAwareInterface, AuthenticationManagerAwareInterface
|
||||
abstract class AbstractController implements
|
||||
RouterAwareInterface,
|
||||
AuthenticationManagerAwareInterface,
|
||||
AssetUrlGeneratorAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var RouterInterface|null
|
||||
@ -48,6 +53,11 @@ abstract class AbstractController implements RouterAwareInterface, Authenticatio
|
||||
*/
|
||||
protected $style = null;
|
||||
|
||||
/**
|
||||
* @var AssetUrlGeneratorInterface|null
|
||||
*/
|
||||
protected $assetUrlGenerator = null;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -80,6 +90,22 @@ abstract class AbstractController implements RouterAwareInterface, Authenticatio
|
||||
return $this->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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user