Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 798136301446e01ae1e39037c75d0e90cee97564 1 parent 2f18eab
@Borales authored
Showing with 115 additions and 3 deletions.
  1. +42 −3 README.md
  2. +73 −0 StaticClientScript.php
View
45 README.md
@@ -1,4 +1,43 @@
-yii-staticClientScript
-======================
+Static ClientScript
+===================
+Extending default Yii [CClientScript](https://github.com/yiisoft/yii/blob/master/framework/web/CClientScript.php) class to use static subdomains for assets files.
-Extending default Yii CClientScript class to use static subdomains
+Preinstall
+----------
+You should make sure, that your HTTP-server configured properly - `'assetsSubdomain'` and all its combinations are pointing to your `'public_html'` directory.
+
+Install and config (available and default settings)
+---------------------------------------------------
+Place `StaticClientScript.php` file into `protected/components` directory.
+After this, you can enhance `CClientScript` class by making some changes in your config file (`protected/config/main.php`):
+
+<pre>
+'components' => array(
+ ...
+ 'clientScript' => array(
+ 'class' => 'application.components.StaticClientScript',
+ 'enableStaticAssets' => true,
+ 'assetsSubdomain' => 'assets',
+ 'indexedAssetsSubdomain' => false,
+ ),
+ ...
+)
+</pre>
+
+Params
+------
+`enableStaticAssets` - whether to use subdomains for ClientScript assets. Default is `true`
+`assetsSubdomain` - subdomain name (e.g. 'http://assets.example.com'. Default is `'assets'`)
+`indexedAssetsSubdomain` - whether to use indexed subdomains for registered script files basing on their `'position'` param. Default is `false`
+
+Examples
+--------
+Example for `indexedAssetsSubdomain`=`true` param:
+
+<pre>Yii::app()->clientScript->registerScriptFile('/js/script.js', CClientScript::POS_HEAD)
+// will output:
+&lt;head&gt;...&lt;script type=&quot;text/javascript&quot; src=&quot;http://assets0.example.com/js/script.js&quot;&gt;&lt;/script&gt;...&lt;/head&gt;&#10;</pre>
+
+<pre>`Yii::app()->clientScript->registerScriptFile('/js/script.js', CClientScript::POS_END)`
+// will output:
+`...&lt;script type=&quot;text/javascript&quot; src=&quot;http://assets2.example.com/js/script.js&quot;&gt;&lt;/script&gt;&lt;/body&gt;`</pre>
View
73 StaticClientScript.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * @author Borales <bordun.alexandr@gmail.com>
+ *
+ * @property string $assetsBaseUrl Use it (Yii::app()->clientScript->assetsBaseUrl) instead of Yii::app()->request->baseUrl
+ */
+class StaticClientScript extends CClientScript {
+
+ /**
+ * @var bool Whether the static assets are enabled
+ */
+ public $enableStaticAssets = true;
+
+ /**
+ * @var string Subdomain name
+ */
+ public $assetsSubdomain = "assets";
+
+ /**
+ * @var bool Whether to use multiple assets subdomains
+ */
+ public $indexedAssetsSubdomain = false;
+
+ /**
+ * @param string $subDomainIndex
+ * @return string
+ */
+ public function getAssetsBaseUrl($subDomainIndex="") {
+ $baseUrl = Yii::app()->request->baseUrl;
+ if($this->enableStaticAssets===false)
+ return $baseUrl;
+
+ $schema = Yii::app()->request->isSecureConnection?"https://":"http://";
+ $subDomainIndex = $this->indexedAssetsSubdomain?$subDomainIndex:"";
+ $baseUrl = $schema.$this->assetsSubdomain.$subDomainIndex.'.'.Yii::app()->request->serverName.$baseUrl;
+ return $baseUrl;
+ }
+
+ /**
+ * Renders the registered scripts.
+ * This method is called in {@link CController::render} when it finishes
+ * rendering content. CClientScript thus gets a chance to insert script tags
+ * at <code>head</code> and <code>body</code> sections in the HTML output.
+ * @param string $output the existing output that needs to be inserted with script tags
+ */
+ public function render(&$output)
+ {
+ if($this->enableStaticAssets && $this->hasScripts) {
+ $this->renderCoreScripts();
+ $this->coreScripts=null;
+ $this->processAssetsUrl();
+ }
+
+ parent::render($output);
+ }
+
+ protected function processAssetsUrl() {
+ foreach($this->cssFiles as $file=>$media) {
+ if(strpos($file, '/') === 0) {
+ unset($this->cssFiles[$file]);
+ $this->cssFiles[$this->assetsBaseUrl.$file] = $media;
+ }
+ }
+
+ foreach($this->scriptFiles as $pos=>$scripts) {
+ foreach($scripts as $scriptName=>$script) {
+ if(strpos($script, '/') === 0) {
+ $this->scriptFiles[$pos][$scriptName] = $this->getAssetsBaseUrl($pos).$script;
+ }
+ }
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.