Overview

Namespaces

  • Minion
    • Service
    • Twig

Classes

  • Minion\Application
  • Minion\Console
  • Minion\Controller
  • Minion\Service\ServiceConfig
  • Minion\Service\ServiceProvider
  • Minion\Twig\AssetExtension
  • Minion\Twig\MiscExtension
  • Minion\Twig\TwigExtensionTagServiceProvider
  • Minion\Twig\UrlExtension
  • Minion\Utils

Interfaces

  • Minion\ControllerInterface
  • Minion\Service\ServiceConfigInterface
  • Minion\Service\ServiceProviderInterface
  • Overview
  • Namespace
  • Class
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Minion package.
 5:  * For the full copyright and license information, please view the LICENSE
 6:  * file that was distributed with this source code.
 7:  *
 8:  * @license MIT License
 9:  */
10: 
11: namespace Minion\Twig;
12: 
13: use Minion\Application;
14: use Minion\Utils;
15: use Symfony\Component\HttpFoundation\File\Exception\FileException;
16: use Symfony\Component\HttpFoundation\Request;
17: 
18: /**
19:  * Class AssetExtension.
20:  *
21:  * @package Minion
22:  * @author Damian SzczerbiƄski <dszczer@gmail.com>
23:  */
24: class AssetExtension extends \Twig_Extension
25: {
26:     /** @var Application Application container */
27:     private $container;
28: 
29:     /**
30:      * AssetExtension constructor.
31:      * Inject dependencies
32:      *
33:      * @param Application $app Framework
34:      *
35:      * @return AssetExtension
36:      */
37:     public function __construct(Application $app) {
38:         $this->container = $app;
39:     }
40: 
41:     /**
42:      * {@inheritdoc}
43:      */
44:     public function getName() {
45:         return 'minion_twig_asset';
46:     }
47: 
48:     /**
49:      * {@inheritdoc}
50:      */
51:     public function getFunctions() {
52:         return [
53:             new \Twig_Function('asset', [
54:                     $this,
55:                     'assetFunction',
56:                 ]
57:             ),
58:         ];
59:     }
60: 
61:     /**
62:      * Return fixed asset's path.
63:      *
64:      * @param string $asset      Path to the asset
65:      * @param bool   $serverPath Server-related path or web-related
66:      *
67:      * @throws FileException File does not exist on server
68:      *
69:      * @return string
70:      */
71:     public function assetFunction($asset, $serverPath = false) {
72:         /** @var Request|null $request */
73:         $request = isset($this->container['request']) ? $this->container['request'] : null;
74:         $path = \ltrim($asset, '/\\');
75:         $assetPath = Utils::fixPath($this->container->getRootDir() . '/web/' . $path);
76: 
77:         if(!\file_exists($assetPath))
78:             throw new FileException("Asset '$asset' with path '$assetPath' not found");
79: 
80:         if(!$serverPath)
81:             if($request instanceof Request)
82:                 $assetPath = $request->getSchemeAndHttpHost() . '/' . $path;
83:             else
84:                 $assetPath = '/' . $path;
85: 
86:         return $assetPath;
87:     }
88: }
API documentation generated by ApiGen