Overview
  • Namespace
  • Class

Namespaces

  • yii2cdn

Classes

  • yii2cdn\Cdn
  • yii2cdn\Component
  • yii2cdn\ConfigFile
  • yii2cdn\ConfigLoader
  • yii2cdn\ConfigParser
  • yii2cdn\File
  • yii2cdn\Section
  1 <?php
  2 
  3 /**
  4  * @copyright Copyright (c) 2016 Junaid Atari
  5  * @link http://junaidatari.com Website
  6  * @see http://www.github.com/blacksmoke26/yii2-cdn
  7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8  */
  9 
 10 namespace yii2cdn;
 11 
 12 use yii\base\UnknownPropertyException;
 13 
 14 /**
 15  * Yii2 Section File Component
 16  *
 17  * @package yii2cdn
 18  * @author Junaid Atari <mj.atari@gmail.com>
 19  *
 20  * @access public
 21  * @version 0.1
 22  */
 23 class File {
 24 
 25     /**
 26      * File ID
 27      * @var string
 28      */
 29     protected $fileId;
 30 
 31     /**
 32      * File Url
 33      * @var string
 34      */
 35     protected $fileUrl;
 36 
 37     /**
 38      * Section name
 39      * @var string
 40      */
 41     protected $section;
 42 
 43     /**
 44      * Component name
 45      * @var string
 46      */
 47     protected $component;
 48 
 49     /**
 50      * File options
 51      * @var array
 52      */
 53     protected $options = [];
 54 
 55     /**
 56      * File attributes
 57      * @var array
 58      */
 59     protected $attributes = [];
 60 
 61     /**
 62      * ComponentFile constructor.
 63      *
 64      * @param array $config Configuration object
 65      */
 66     public function __construct ( array $config ) {
 67         if ( !count( $config ) ) {
 68             return;
 69         }
 70         
 71         foreach ($config as $prop => $value ) {
 72             $this->$prop = $value;
 73         }
 74     }
 75 
 76     /**
 77      * Get current file url
 78      * @return string
 79      */
 80     public function getUrl () {
 81         return $this->fileUrl;
 82     }
 83 
 84     /**
 85      * Get file section or it's id
 86      *
 87      * @param boolean $asId (optional) True will return section name (default: false)
 88      * @param string $property (optional) Property name of `cdn` defined in @app/config/main.php (default: 'cdn')
 89      * @return Section|string Section object | Section name
 90      */
 91     public function getSection ( $asId, $property = 'cdn' ) {
 92         return $asId ? $this->section : $this->getComponent ( false, $property )->getSection ( $this->section );
 93     }
 94 
 95     /**
 96      * Get current file component or it's id
 97      *
 98      * @param boolean $asId (optional) True will return component id (default: false)
 99      * @param string $property (optional) Property name of `cdn` defined in @app/config/main.php (default: 'cdn')
100      * @return Component|string Component object | Component id
101      */
102     public function getComponent ( $asId, $property = 'cdn' ) {
103         return $asId ? $this->component : \Yii::$app->cdn->get ( $property )->get ( $this->component );
104     }
105 
106     /**
107      * Get the file attributes
108      * @return array
109      */
110     public function getAttributes () {
111         return $this->attributes;
112     }
113 
114     /**
115      * @param string $name File attribute name
116      * @param boolean $throwException (optional) True will throw exception (default: true)
117      * @throws \yii\base\UnknownPropertyException
118      * @return mixed|null Attribute value | null on empty
119      */
120     public function getAttr ( $name, $throwException = true ) {
121         if ( isset($this->attributes[$name]) ) {
122             return $this->attributes[$name];
123         }
124 
125         if ( $throwException ) {
126             throw new UnknownPropertyException ( "Unknown file attribute '{$name}' given" );
127         }
128 
129         return null;
130     }
131 
132     /**
133      * Register as CSS file
134      * @param array $options the HTML attributes for the link tag. Please refer to [[Html::cssFile()]] for
135      * the supported options. The following options are specially handled and are not treated as HTML attributes:
136      *
137      * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on.
138      *
139      * @param string $key the key that identifies the CSS script file. If null, it will use
140      */
141     public function registerAsCssFile ( array $options = [], $key = null ) {
142         $options = !count($options) ? $this->options : $options;
143         $key = is_null($key) ? $this->getId() : $key;
144 
145         \Yii::$app->controller->view->registerCssFile( $this->fileUrl, $options, $key );
146     }
147 
148     /**
149      * Get current file id
150      *
151      * @return string
152      */
153     public function getId () {
154         return $this->fileId;
155     }
156 
157     /**
158      * Register as JavaScript file
159      * @param array $options the HTML attributes for the script tag. The following options are specially handled
160      * and are not treated as HTML attributes:
161      *
162      * - `depends`: array, specifies the names of the asset bundles that this JS file depends on.
163      * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are:
164      *     * [[POS_HEAD]]: in the head section
165      *     * [[POS_BEGIN]]: at the beginning of the body section
166      *     * [[POS_END]]: at the end of the body section. This is the default value.
167      *
168      * Please refer to [[Html::jsFile()]] for other supported options.
169      *
170      * @param string $key the key that identifies the JS script file. If null, it will use
171      * $url as the key. If two JS files are registered with the same key, the latter
172      * will overwrite the former.
173      */
174     public function registerAsJsFile ( array $options = [], $key = null ) {
175         $options = !count($options) ? $this->options : $options;
176         $key = is_null($key) ? $this->getId() : $key;
177 
178         \Yii::$app->controller->view->registerJsFile( $this->fileUrl, $options, $key );
179     }
180 }
181 
API documentation generated by ApiGen