GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Booster::init()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 0
crap 3
1
<?php
2
/**
3
 *## Booster class file.
4
 *
5
 * @author Christoffer Niska <[email protected]>
6
 * @copyright Copyright &copy; Christoffer Niska 2011-2012
7
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
8
 * @version 1.0.0
9
 *
10
 * Modified for YiiBooster
11
 * @author Antonio Ramirez <[email protected]>
12
 * @version 1.0.7
13
 *
14
 * Maintenance
15
 * @author Mark Safronov <[email protected]>
16
 * @version 2.0.0
17
 *
18
 * Maintenance
19
 * @author Maksim Naumov <[email protected]>
20
 * @version 2.1.0
21
 * 
22
 * Maintenance
23
 * @author Amr Bedair <[email protected]>
24
 * @version 3.0.1
25
 * 
26
 * Bootstrap 3.x.x
27
 * @author Amr Bedair <[email protected]>
28
 * @version 4.0.0
29
 * 
30
 */
31
32
/**
33
 *## Booster application component.
34
 *
35
 * This is the main YiiBooster component which you should attach to your Yii CWebApplication instance.
36
 *
37
 * Almost all configuration options are meaningful only at the initialization time,
38
 * changing them after `Booster` was attached to application will have no effect.
39
 *
40
 * @package booster.components
41
 */
42
class Booster extends CApplicationComponent {
43
	
44
	/**
45
	 * @var boolean Whether to use CDN server URLs for assets.
46
	 * Note that not all assets will be served from CDN and we are using several public CDN servers,
47
	 * not some single private one.
48
	 *
49
	 * Consult with the packages configuration to discover precisely which assets will be served from CDN.
50
	 */
51
	public $enableCdn = false;
52
53
	/**
54
	 * @var boolean Whether to register any CSS at all.
55
	 * Defaults to true.
56
	 */
57
	public $coreCss = true;
58
59
	/**
60
	 * @var boolean Whether to register the Bootstrap core CSS (bootstrap.min.css).
61
	 * Defaults to true.
62
	 */
63
	public $bootstrapCss = true;
64
65
	/**
66
	 * @var boolean whether to register the Bootstrap responsive CSS (bootstrap-responsive.min.css).
67
	 * Defaults to false.
68
	 */
69
	public $responsiveCss = true;
70
	
71
	/**
72
	 * @var boolean whether to disable zooming capabilities on mobile devices
73
	 * Defaults to false
74
	 * @since 4.0.0
75
	 */
76
	public $disableZooming = false;
77
78
	/**
79
	 * @var boolean Whether to register the Font Awesome CSS (font-awesome.min.css).
80
	 * Defaults to false.
81
	 *
82
	 * Note that FontAwesome does not include some of the Twitter Bootstrap built-in icons!
83
	 */
84
	public $fontAwesomeCss = false;
85
86
	/**
87
	 * @var bool Whether to use minified CSS and Javascript files. Default to true.
88
	 */
89
	public $minify = true;
90
91
	/**
92
	 * @var boolean Whether to register YiiBooster custom CSS overrides
93
	 * providing compatibility between various parts of the system.
94
	 *
95
	 * @since 0.9.12
96
	 */
97
	public $yiiCss = true;
98
    
99
    /*
100
     *  $customTheme will look it there is a custom theme (boolean)
101
     *  $customThemeName will have the name of the file placed in theme folder in root (string) [Default: NULL]
102
     *  Added by: Salik Sadruddin Merani
103
     *  DragoTech Innovations PK
104
     *  www.dragotech-innovation.com
105
    */
106
    public $customTheme = false;
107
    public $customThemeName = null;
108
109
	/**
110
	 * @var boolean Whether to register the JQuery-specific CSS missing from Bootstrap.
111
	 */
112
	public $jqueryCss = true;
113
114
	/**
115
	 * @var boolean Whether to register jQuery and the Bootstrap JavaScript.
116
	 * @since 0.9.10
117
	 */
118
	public $enableJS = true;
119
120
	/**
121
	 * @var bool Whether to enable bootbox messages or not. Default value is true.
122
	 * @since 1.0.5
123
	 */
124
	public $enableBootboxJS = true;
125
126
	/**
127
	 * @var bool Whether to enable bootstrap notifier.
128
	 * Defaults to true.
129
	 *
130
	 * @see https://github.com/Nijikokun/bootstrap-notify
131
	 */
132
	public $enableNotifierJS = true;
133
134
	/**
135
	 * @var boolean to register Bootstrap CSS files in AJAX requests
136
	 * Defaults to false and you probably have no reason to set it to true.
137
	 */
138
	public $ajaxCssLoad = false;
139
140
	/**
141
	 * @var boolean to register the Bootstrap JavaScript files in AJAX requests
142
	 * Defaults to false and you probably have no reason to set it to true.
143
	 */
144
	public $ajaxJsLoad = false;
145
146
	/**
147
	 * @var bool|null Whether to republish assets on each request.
148
	 * If set to true, all YiiBooster assets will be republished on each request.
149
	 * Passing null to this option restores the default handling of CAssetManager of YiiBooster assets.
150
	 *
151
	 * @since YiiBooster 1.0.6
152
	 */
153
	public $forceCopyAssets = false;
154
	
155
	public $enablePopover = true;
156
	
157
	public $enableTooltip = true;
158
159
	/**
160
	 * @var string Default popover target CSS selector.
161
	 *
162
	 * @since 0.10.0
163
	 * @since 1.1.0 NOTE: this parameter changed its logic completely!
164
	 * Previously it was the selector from which to start delegating the popovers.
165
	 * Now the popovers are always being bound to specific elements.
166
	 * According to the documentation: http://twitter.github.io/bootstrap/javascript.html#popovers
167
	 */
168
	public $popoverSelector = '[data-toggle=popover]';
169
170
	/**
171
	 * @var string default tooltip CSS selector.
172
	 * @since 0.10.0
173
	 * @since 1.1.0 NOTE: this parameter changed its logic completely!
174
	 * Previously it was the selector from which to start delegating the tooltips.
175
	 * Now the tooltips always start spreading from `body`, and this parameter controls
176
	 * what elements will actually receive the tooltip behavior.
177
	 * According to the documentation: http://twitter.github.io/bootstrap/javascript.html#tooltips
178
	 * previously it was the direct selector to which to apply the `tooltip` plugin,
179
	 * now it is the value for `selector` plugin option.
180
	 */
181
	public $tooltipSelector = '[data-toggle=tooltip]';
182
183
	/**
184
	 * @var array list of script packages (name=>package spec).
185
	 * This property keeps a list of named script packages, each of which can contain
186
	 * a set of CSS and/or JavaScript script files, and their dependent package names.
187
	 * By calling {@link registerPackage}, one can register a whole package of client
188
	 * scripts together with their dependent packages and render them in the HTML output.
189
	 * @since 1.0.7
190
	 */
191
	public $packages = array();
192
193
	/**
194
	 * @var CClientScript Something which can register assets for later inclusion on page.
195
	 * For now it's just the `Yii::app()->clientScript`
196
	 */
197
	public $cs;
198
199
	/**
200
	 * @var string handles the assets folder path.
201
	 */
202
	public $_assetsUrl;
203
204
    /**
205
     * @var Booster
206
     */
207
    private static $_instance;
208
209
	/**
210
	 * Initializes the component.
211
	 */
212 5
	public function init() {
213
		
214
		// Prevents the extension from registering scripts and publishing assets when ran from the command line.
215 5
		if ($this->isInConsoleMode() && !$this->isInTests())
216 5
			return;
217
218 5
        self::setBooster($this);
219
220 5
        $this->setRootAliasIfUndefined();
221
222 5
		$this->setAssetsRegistryIfNotDefined();
223
224 5
		$this->includeAssets();
225
226 5
		parent::init();
227 5
	}
228
229
	/** @return bool */
230 5
	protected function isInConsoleMode() {
231
		
232 5
		return Yii::app() instanceof CConsoleApplication || PHP_SAPI == 'cli';
0 ignored issues
show
Bug introduced by
The class CConsoleApplication does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
233
	}
234
235
	/** @return bool */
236 5
	protected function isInTests() {
237
		
238 5
		return defined('IS_IN_TESTS') && IS_IN_TESTS;
239
	}
240
241
	/**
242
	 * 
243
	 */
244 5
	protected function setRootAliasIfUndefined() {
245
		
246 5
		if (Yii::getPathOfAlias('booster') === false) {
247 1
			Yii::setPathOfAlias('booster', realpath(dirname(__FILE__) . '/..'));
248 1
		}
249 5
	}
250
251
	/**
252
	 *
253
	 */
254 5
	protected function includeAssets() {
255
		
256 5
		$this->appendUserSuppliedPackagesToOurs();
257
258 5
		$this->addOurPackagesToYii();
259
260 5
		$this->registerCssPackagesIfEnabled();
261
262 5
		$this->registerJsPackagesIfEnabled();
263 5
	}
264
265
	/**
266
	 *
267
	 */
268 5
	protected function appendUserSuppliedPackagesToOurs() {
269
270
		/** @noinspection PhpIncludeInspection */
271 5
		$bootstrapPackages = require(Yii::getPathOfAlias('booster.components') . '/packages.php');
272 5
		$bootstrapPackages += $this->createBootstrapCssPackage();
273 5
		$bootstrapPackages += $this->createSelect2Package();
274 5
		$bootstrapPackages += $this->createChosenPackage();
275
276 5
		$this->packages = CMap::mergeArray(
277 5
			$bootstrapPackages,
278 5
			$this->packages
279 5
		);
280 5
	}
281
282
	/**
283
	 *
284
	 */
285 5
	protected function addOurPackagesToYii() {
286
		
287 5
		foreach ($this->packages as $name => $definition) {
288 5
			$this->cs->addPackage($name, $definition);
289 5
		}
290 5
        $this->cs->scriptMap['jquery-ui.min.js'] = $this->getAssetsUrl() . '/js/jquery-ui-no-conflict.min.js';
291 5
	}
292
293
	/**
294
	 * If we did not disabled registering CSS packages, register them.
295
	 */
296 5
	protected function registerCssPackagesIfEnabled() {
297
		
298 5
		if (!$this->coreCss)
299 5
			return;
300
301 5
		if (!$this->ajaxCssLoad && Yii::app()->request->isAjaxRequest)
302 5
			return;
303
304 5
		if ($this->bootstrapCss)
305 5
			$this->registerBootstrapCss();
306
307 5
		if ($this->fontAwesomeCss)
308 5
			$this->registerFontAwesomeCss();
309
310 5
		if ($this->responsiveCss)
311 5
			$this->registerMetadataForResponsive();
312
313 5
		if ($this->yiiCss !== false){
314 5
			$this->registerYiiCss();
315 5
		}elseif($this->customTheme !== false && !is_null($this->customThemeName)){
316
            $this->registerCustomTheme();
317
        }
318
319 5
		if ($this->jqueryCss !== false)
320 5
			$this->registerJQueryCss();
321 5
	}
322
    
323
    /*
324
     *  Register Themes
325
     */
326
    public function registerCustomTheme(){
327
        Yii::app()->clientScript->registerCssFile( Yii::app()->baseUrl.'/themes/'.$this->customThemeName );
328
    }
329
    
330
	/**
331
	 * Register our overrides for jQuery UI + Twitter Bootstrap 2.3 combo
332
	 *
333
	 * @since 0.9.11
334
	 */
335 5
	public function registerYiiCss() {
336
		
337 5
		$this->registerPackage('bootstrap-yii');
338 5
	}
339
340
	/**
341
	 * Register the compatibility layer for jQuery UI + Twitter Bootstrap 2.3 combo
342
	 */
343 5
	public function registerJQueryCss() {
344
		
345 5
		$this->registerPackage('jquery-css')->scriptMap['jquery-ui.css'] = $this->getAssetsUrl(
346 5
		) . '/css/jquery-ui-bootstrap.css';
347 5
	}
348
349
	/**
350
	 * If `enableJS` is not `false`, register our Javascript packages
351
	 */
352 5
	protected function registerJsPackagesIfEnabled() {
353
		
354 5
		if (!$this->enableJS)
355 5
			return;
356
357 5
		if (!$this->ajaxJsLoad && Yii::app()->request->isAjaxRequest)
358 5
			return;
359
360 5
		$this->registerPackage('bootstrap.js');
361 5
        $this->registerPackage('bootstrap-noconflict');
362
363 5
		if ($this->enableBootboxJS)
364 5
			$this->registerPackage('bootbox');
365
366 5
		if ($this->enableNotifierJS)
367 5
			$this->registerPackage('notify');
368
		
369 5
		if($this->enablePopover)
370 5
			$this->registerPopoverJs();
371
		
372 5
		if($this->enableTooltip)
373 5
			$this->registerTooltipJs();
374 5
	}
375
376
377
	/**
378
	 * Returns the extension version number.
379
	 * @return string the version
380
	 */
381
	public function getVersion() {
382
		
383
		return '4.0.1';
384
	}
385
386
	/**
387
	 * Registers a script package that is listed in {@link packages}.
388
	 *
389
	 * @param string $name the name of the script package.
390
	 *
391
	 * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
392
	 * @see CClientScript::registerPackage
393
	 * @since 1.0.7
394
	 */
395 6
	public function registerPackage($name) {
396
		
397 6
		return $this->cs->registerPackage($name);
398
	}
399
400
	/**
401
	 * Registers a CSS file in the asset's css folder
402
	 *
403
	 * @param string $name the css file name to register
404
	 * @param string $media media that the CSS file should be applied to. If empty, it means all media types.
405
	 *
406
	 * @see CClientScript::registerCssFile
407
	 */
408 1
	public function registerAssetCss($name, $media = '') {
409
		
410 1
		$this->cs->registerCssFile($this->getAssetsUrl() . "/css/{$name}", $media);
411 1
	}
412
413
	/**
414
	 * Register a javascript file in the asset's js folder
415
	 *
416
	 * @param string $name the js file name to register
417
	 * @param int $position the position of the JavaScript code.
418
	 *
419
	 * @see CClientScript::registerScriptFile
420
	 */
421 1
	public function registerAssetJs($name, $position = CClientScript::POS_END) {
422
		
423 1
		$this->cs->registerScriptFile($this->getAssetsUrl() . "/js/{$name}", $position);
424 1
	}
425
426
	/**
427
	 * Returns the URL to the published assets folder.
428
	 * @return string an absolute URL to the published asset
429
	 */
430 6
	public function getAssetsUrl() {
431
		
432 6
		if (isset($this->_assetsUrl)) {
433 6
			return $this->_assetsUrl;
434
		} else {
435 1
			return $this->_assetsUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('booster.assets'), false, -1, $this->forceCopyAssets);
436
		}
437
	}
438
439 5
	protected function setAssetsRegistryIfNotDefined() {
440
		
441 5
		if (!$this->cs) {
442 1
            $this->cs = Yii::app()->getClientScript();
443 1
        }
444 5
	}
445
446 5
	public function registerBootstrapCss() {
447
		
448 5
		$this->cs->registerPackage('bootstrap.css');
449 5
	}
450
451
	/**
452
	 * We use the values of $this->responsiveCss, $this->fontAwesomeCss,
453
	 * $this->minify and $this->enableCdn to construct the proper package definition
454
	 * and install and register it.
455
	 * @return array
456
	 */
457 5
	protected function createBootstrapCssPackage() {
458
		
459
		return array('bootstrap.css' => array(
460 5
			'baseUrl' => $this->enableCdn ? '//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/' : $this->getAssetsUrl() . '/bootstrap/',
461 5
			'css' => array($this->minify ? 'css/bootstrap.min.css' : 'css/bootstrap.css'),
462 5
		));
463
	}
464
465
	/**
466
	 * Make chosen package definition
467
	 * @return array
468
	 */
469 5
	protected function createChosenPackage() {
470
		
471
		return array('chosen' => array(
472 5
			'baseUrl' => $this->getAssetsUrl() . '/chosen/',
473 5
			'js' => array('chosen.jquery.min.js'),
474 5
			'css' => array('chosen.min.css'),
475 5
			'depends' => array('jquery'),
476 5
		));
477
	}
478
        
479
	/**
480
	 * Make select2 package definition
481
	 * @return array
482
	 */
483 5
	protected function createSelect2Package() {
484
		
485 5
		$jsFiles = array($this->minify ? 'select2.min.js' : 'select2.js');
486
487 5
		if (strpos(Yii::app()->language, 'en') !== 0) {
488
			$locale = 'select2_locale_'. substr(Yii::app()->language, 0, 2). '.js';
489
			if (@file_exists(Yii::getPathOfAlias('booster.assets.select2') . DIRECTORY_SEPARATOR . $locale )) {
490
				$jsFiles[] = $locale;
491
			} else {
492
				$locale = 'select2_locale_'. Yii::app()->language . '.js';
493
				if (@file_exists(Yii::getPathOfAlias('booster.assets.select2') . DIRECTORY_SEPARATOR . $locale )) {
494
					$jsFiles[] = $locale;
495
				}else{
496
					$locale = 'select2_locale_'. substr(Yii::app()->language, 0, 2) . '-' . strtoupper(substr(Yii::app()->language, 3, 2)) . '.js';
497
					if (@file_exists(Yii::getPathOfAlias('booster.assets.select2') . DIRECTORY_SEPARATOR . $locale )) {
498
						$jsFiles[] = $locale;
499
					}
500
				}
501
			}
502
		}
503
504
		return array('select2' => array(
505 5
			'baseUrl' => $this->getAssetsUrl() . '/select2/',
506 5
			'js' => $jsFiles,
507 5
			'css' => array('select2.css', 'select2-bootstrap.css'),
508 5
			'depends' => array('jquery'),
509 5
		));
510
	}
511
512
	/**
513
	 * Required metadata for responsive CSS to work.
514
	 */
515 5
	protected function registerMetadataForResponsive() {
516 5
		if($this->disableZooming)
517 5
			$this->cs->registerMetaTag('width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no', 'viewport');
518
		else
519 5
			$this->cs->registerMetaTag('width=device-width, initial-scale=1', 'viewport');
520 5
	}
521
522
	/**
523
	 * Registers the Font Awesome CSS.
524
	 * @since 1.0.6
525
	 */
526
	public function registerFontAwesomeCss() {
527
		
528
        $this->registerPackage('font-awesome');
529
	}
530
	
531 5
	public function registerPopoverJs() {
532 5
		$this->cs->registerScript($this->getUniqueScriptId(), "jQuery('[data-toggle=popover]').popover();");
533 5
	}
534
	
535 5
	public function registerTooltipJs() {
536 5
		$this->cs->registerScript($this->getUniqueScriptId(), "jQuery('[data-toggle=tooltip]').tooltip();");
537 5
	}
538
539
	/**
540
	 * Generates a "somewhat" random id string.
541
	 * @return string
542
	 * @since 1.1.0
543
	 */
544 5
	public function getUniqueScriptId() {
545 5
		return uniqid(__CLASS__ . '#', true);
546
	}
547
548
    /**
549
     * @param Booster $value
550
     * @since 2.1.0
551
     */
552 5
    public static function setBooster($value) {
553
    	
554 5
        if ($value instanceof Booster) {
555 5
            self::$_instance = $value;
556 5
        }
557 5
    }
558
559
    /**
560
     * @return Booster
561
     * @since 2.1.0
562
     */
563 1
    public static function getBooster() {
564
    	
565 1
        if (null === self::$_instance) {
566
            // Lets find inside current module
567
            $module = Yii::app()->getController()->getModule();
568
            if ($module) {
569
                if ($module->hasComponent('booster')) {
570
                    self::$_instance = $module->getComponent('booster');
571
                }
572
            }
573
            // Still nothing?
574
            if (null === self::$_instance) {
575
                if (Yii::app()->hasComponent('booster')) {
576
                    self::$_instance = Yii::app()->getComponent('booster');
577
                }
578
            }
579
        }
580 1
        return self::$_instance;
581
    }
582
}
583