forked from artem-frolov/yii-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SassHandler.php
869 lines (786 loc) · 26.6 KB
/
SassHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
<?php
/**
* Sass Handler
*
* Compiles SCSS file(s) on-the-fly
* and publishes and/or registers output .css file
*
* @property ExtendedScssc $compiler
*
* @author Artem Frolov <[email protected]>
* @link https://github.com/artem-frolov/yii-sass
*/
class SassHandler extends CApplicationComponent
{
const PATH_TYPE_FULL = 0;
const PATH_TYPE_RELATIVE = 1;
/**
* Path for cache files. Will be used if Yii caching is not enabled.
* Will be chmod'ed to become writable, see "writableDirectoryPermissions"
* parameter.
* Yii aliases can be used.
*
* Defaults to 'application.runtime.sass-cache'
*
* @var string
*/
public $cachePath = 'application.runtime.sass-cache';
/**
* Path and filename of scss.inc.php
*
* Defaults to the relative location in Composer's vendor directory:
* __DIR__ . "/../../ScssPhp/ScssPhp/scss.inc.php"
*
* @var string
*/
public $compilerPath;
/**
* Path and filename of compass.inc.php
*
* Defaults to the relative location in Composer's vendor directory:
* __DIR__ . "/../../leafo/scssphp-compass/compass.inc.php"
*
* @var string
*/
public $compassPath;
/**
* Enable Compass support.
* Automatically add required import paths and functions.
*
* Defaults to false
*
* @var boolean
*/
public $enableCompass = false;
/**
* Path to a directory with compiled CSS files.
* Will be created automatically if it doesn't exist.
* Will be chmod'ed to become writable, see "writableDirectoryPermissions"
* parameter.
* Yii aliases can be used.
*
* Defaults to 'application.runtime.sass-compiled'
*
* @var string
*/
public $sassCompiledPath = 'application.runtime.sass-compiled';
/**
* relative path for compiled css files, used in asset bundles
* @var string
*/
public $sassCompiledPathRelativeDirectory;
/**
* Force compilation/recompilation on each request.
*
* False value means that compilation will be done only if
* source SCSS file or related imported files have been
* changed after previous compilation.
*
* Defaults to false
*
* @var boolean
*/
public $forceCompilation = false;
/**
* Turn on/off overwriting of already compiled CSS files.
* Will be ignored if "forceCompilation" parameter's value is true.
*
* True value means that compiled CSS file will be overwritten
* if the source SCSS file or related imported files have
* been changed after previous compilation.
*
* False value means that compilation will be done only if
* an output CSS file doesn't exist.
*
* Defaults to true
*
* @var boolean
*/
public $allowOverwrite = true;
/**
* Automatically add directory containing SCSS file being processed
* as an import path for the @import Sass directive.
*
* Defaults to true
*
* @var boolean
*/
public $autoAddCurrentDirectoryAsImportPath = true;
/**
* List of import paths.
* Can be a list of strings or callable functions:
* function($searchPath) {return $targetPath;}
*
* Defaults to an empty array
*
* @var string[]|callable[]
*/
public $importPaths = array();
/**
* Chmod permissions used for creating/updating of writable
* directories for cache files and compiled CSS files.
* Mind the leading zero for octal values.
*
* Defaults to 0777
*
* @var integer
*/
public $writableDirectoryPermissions = 0777;
/**
* Chmod permissions used for creating/updating of writable
* cache files and compiled CSS files.
* Mind the leading zero for octal values.
*
* Defaults to 0666
*
* @var integer
*/
public $writableFilePermissions = 0776;
/**
* Default value for $hashByName parameter in extension's methods.
* $hashByName value determines whether the published file should be named
* as the hashed basename. If false, the name will be the hash taken
* from dirname of the path being published and path mtime.
* Set to true if the path being published is shared
* among different extensions.
*
* Defaults to false
*
* @see CAssetManager::publish()
* @var bool
*/
public $defaultHashByName = false;
/**
* Customize the formatting of the output CSS.
* Use one of the SassHandler::OUTPUT_FORMATTING_* constants
* to set the formatting type.
* @link https://github.com/scssphp/scssphp/blob/main/docs/docs/README.md#output-formatting
*
* Default is OUTPUT_FORMATTING_EXPANDED
*
* @var string
*/
public $compilerOutputFormatting = self::OUTPUT_FORMATTING_EXPANDED;
/**
* Id of the cache application component.
* Defaults to 'cache' (the primary cache application component)
*
* @var string
*/
public $cacheComponentId = 'cache';
const OUTPUT_FORMATTING_COMPRESSED = 'compressed';
// Keep using "simple" string for the backward compatibility
const OUTPUT_FORMATTING_EXPANDED = 'simple';
/**
* Please use OUTPUT_FORMATTING_EXPANDED instead of this constant.
* This constant is kept only for the backward compatibility.
*
* @deprecated deprecated since version 1.2.0
*/
const OUTPUT_FORMATTING_SIMPLE = self::OUTPUT_FORMATTING_EXPANDED;
/**
* Compiler object
*
* @var ExtendedScssc
*/
protected $scssc;
public $parsedFiles = [];
/**
* Initialize component
*/
public function init()
{
$vendorPath = Yii::getPathOfAlias('vendor').'/';
if (!$this->compilerPath) {
$this->compilerPath = $vendorPath . 'ScssPhp/ScssPhp/scss.inc.php';
}
if (!$this->compassPath) {
$this->compassPath = $vendorPath
. "leafo/scssphp-compass/compass.inc.php";
}
// $this->sassCompiledPathRelativeDirectory = EO::app()->getBaseUrl(false);
parent::init();
}
/**
* Publish and register compiled CSS file.
* Compile/recompile source SCSS file if needed.
*
* Optionally can publish compiled CSS file inside
* specific published directory.
* It's helpful when CSS code has relative references to other
* resources (images/fonts) and when these resources are also published
* using Yii asset manager. This method allows to publish compiled CSS files
* along with other resources to make relative references work.
*
* E.g.:
* "image.jpg" is stored inside path alias "application.files.images"
* Somewhere in the code the following is called during page generation:
* Yii::app()->assetManager->publish(
* Yii::getPathOfAlias('application.files')
* );
* SCSS file has the following code:
* background-image: url(../images/image.jpg);
* Then the correct call of the method will be:
* Yii::app()->sass->register(
* 'path-to-scss-file.scss',
* '',
* 'application.files',
* 'css_compiled'
* );
*
* @param string $sourcePath Path to the source SCSS file
* @param string $media Media that the CSS file should be applied to.
* If empty, it means all media types
* @param string $insidePublishedDirectory Path to the directory with
* resource files which is published somewhere in the application
* explicitly.
* Default is null which means that CSS file will be published
* separately.
* @param string $subDirectory Subdirectory for the CSS file within publicly
* available location. Default is null
* @param boolean $hashByName Must be the same
* as in the CAssetManager::publish() call
* for $insidePublishedDirectory.
* See CAssetManager::publish() for details.
* "defaultHashByName" plugin parameter's value is used by default.
* @see CAssetManager::publish()
* @return array|string
* @throws CException
*/
public function register(
$sourcePath,
$media = '',
$insidePublishedDirectory = null,
$subDirectory = null,
$hashByName = null
) {
$publishedPath = $this->publish(
$sourcePath,
$insidePublishedDirectory,
$subDirectory,
$hashByName
);
Yii::app()->clientScript->registerCssFile($publishedPath, $media);
}
/**
* Publish compiled CSS file.
* Compile/recompile source SCSS file if needed.
*
* Optionally can publish compiled CSS file inside
* specific published directory.
* It's helpful when CSS code has relative references to other
* resources (images/fonts) and when these resources are also published
* using Yii asset manager. This method allows to publish compiled CSS files
* along with other resources to make relative references work.
*
* E.g.:
* "image.jpg" is stored inside path alias "application.files.images"
* Somewhere in the code the following is called during page generation:
* Yii::app()->assetManager->publish(
* Yii::getPathOfAlias('application.files')
* );
* SCSS file has the following code:
* background-image: url(../images/image.jpg);
* Then the correct call of the method will be:
* Yii::app()->sass->publish(
* 'path-to-scss-file.scss',
* 'application.files',
* 'css_compiled'
* );
*
* @param string $sourcePath Path to the source SCSS file
* @param string $insidePublishedDirectory Path to the directory
* with resource files which is published somewhere
* in the application explicitly.
* Default is null which means that CSS file
* will be published separately.
* @param string $subDirectory Subdirectory for the CSS file
* within publicly available location. Default is null
* @param boolean $hashByName Must be the same
* as in the CAssetManager::publish() call
* for $insidePublishedDirectory.
* See CAssetManager::publish() for details.
* "defaultHashByName" plugin parameter's value is used by default.
* @return array|string URL of the published CSS file
* @see CAssetManager::publish()
* @throws CException
*/
public function publish(
$sourcePath,
$insidePublishedDirectory = null,
$subDirectory = null,
$hashByName = null
) {
$compiledFile = $this->getCompiledFile($sourcePath);
if (empty($insidePublishedDirectory)) {
return Yii::app()->assetManager->publish(
$compiledFile,
is_null($hashByName) ? $this->defaultHashByName : $hashByName
);
} else {
return $this->publishInside(
$compiledFile,
$insidePublishedDirectory,
$subDirectory,
$hashByName
);
}
}
/**
* Get path to the compiled CSS file,
* compile/recompile source file if needed.
*
* @param string $sourcePath Path to the source SCSS file
* @param bool $justName
* @return string
* @throws CException
*/
public function getCompiledFile($sourcePath, $justName = false) {
$cssPath = $this->getCompiledCssFilePath($sourcePath);
if ($this->isCompilationNeeded($sourcePath)) {
$compiledCssCode = $this->compile($sourcePath);
if (file_put_contents($cssPath, $compiledCssCode, LOCK_EX) === false) {
throw new CException(
'Can not write the compiled CSS file: ' . $cssPath
);
}
if (!chmod($cssPath, $this->writableFilePermissions)) {
throw new CException(
'Can not chmod('
. decoct($this->writableFilePermissions)
. ') the compiled CSS file: ' . $cssPath
);
}
$this->saveParsedFilesInfoToCache($sourcePath);
}
if ($justName)
{
$cssPath = $this->sassCompiledPathRelativeDirectory. '/' .pathinfo($cssPath,PATHINFO_BASENAME);
}
return $cssPath;
}
/**
* Compile SCSS file
*
* @param string $sourcePath
* @throws CException
* @return string Compiled CSS code
*/
public function compile($sourcePath) {
$this->compiler->addImportPath(__DIR__.'/../../../');
$sourceCode = file_get_contents($sourcePath);
if ($sourceCode === false) {
throw new CException(
'Can not read the source SCSS file: ' . $sourcePath
);
}
$compiledCssCode = $this->compiler->compileString(ltrim($sourceCode));
$this->parsedFiles = $compiledCssCode->getIncludedFiles();
return $compiledCssCode->getCss();
}
/**
* Get compiler
* Loads required files on initial request
*
* @return ExtendedScssc
* @throws CException
*/
public function getCompiler()
{
if (!$this->scssc) {
if (is_readable($this->compilerPath)) {
require_once $this->compilerPath;
}
require_once dirname(__FILE__) . '/ExtendedScssc.php';
$this->scssc = new ExtendedScssc();
$this->setImportPaths($this->importPaths);
$this->setupOutputFormatting($this->scssc);
}
return $this->scssc;
}
/**
* Publish compiled CSS file inside specific published directory
* Helpful when CSS code has relative references to other
* resources (images/fonts) and when these resources are also published
* using Yii asset manager
*
* @param string $compiledFile Path to the already compiled CSS file
* @param string $insidePublishedDirectory Path to the directory
* with resource files which is published somewhere
* in the application explicitly.
* Default is null which means that CSS file
* will be published separately.
* @param string $subDirectory Subdirectory for the CSS file within
* publicly available location.
* Default is null
* @param boolean $hashByName Must be the same
* as in the CAssetManager::publish() call
* for $insidePublishedDirectory.
* See CAssetManager::publish() for details.
* "defaultHashByName" plugin parameter's value is used by default.
* @throws CException
* @return string URL of the published CSS file
* @see CAssetManager::publish()
*/
protected function publishInside(
$compiledFile,
$insidePublishedDirectory = null,
$subDirectory = null,
$hashByName = null
) {
$hashByName = is_null($hashByName)
? $this->defaultHashByName
: $hashByName;
$insidePublishedDirectory = trim($insidePublishedDirectory, '/\\');
$insidePublishedDirectoryRealPath = Yii::getPathOfAlias(
$insidePublishedDirectory
);
$targetPath = Yii::app()->assetManager
->getPublishedPath($insidePublishedDirectoryRealPath, $hashByName)
. DIRECTORY_SEPARATOR;
if (!$targetPath) {
throw new CException(
'Directory with alias "' . $insidePublishedDirectory
. '" doesn\'t exist. ' . 'Path with converted aliases: "'
. $insidePublishedDirectoryRealPath . '"'
);
}
$subDirectoryUrlSection = '';
if (!empty($subDirectory)) {
$subDirectory = trim($subDirectory, '/\\');
$targetPath = $this->getWritableDirectoryPath(
$targetPath . $subDirectory
);
$subDirectoryUrlSection = $subDirectory . '/';
}
$basename = basename($compiledFile);
$targetFile = $targetPath . $basename;
if (
!file_exists($targetFile)
or filemtime($compiledFile) !== filemtime($targetFile)
) {
if (!copy($compiledFile, $targetFile)) {
throw new CException(
'Can not copy the compiled file "' . $compiledFile
. '" to the "' . $targetPath . '" directory'
);
}
if (!chmod($targetFile, $this->writableFilePermissions)) {
throw new CException(
'Can not chmod(' . decoct($this->writableFilePermissions)
. ') the copied file with a compiled CSS code: '
. $targetFile
);
}
}
return Yii::app()->assetManager->getPublishedUrl(
$insidePublishedDirectoryRealPath,
$hashByName
) . '/' . $subDirectoryUrlSection . $basename;
}
/**
* Setup compiler output formatting
* @param ExtendedScssc $compiler
* @throws CException
*/
protected function setupOutputFormatting($compiler) {
$formatters = array(
self::OUTPUT_FORMATTING_COMPRESSED
=> ScssPhp\ScssPhp\OutputStyle::COMPRESSED,
self::OUTPUT_FORMATTING_EXPANDED
=> ScssPhp\ScssPhp\OutputStyle::EXPANDED,
);
if (isset($formatters[$this->compilerOutputFormatting])) {
$compiler->setOutputStyle(
$formatters[$this->compilerOutputFormatting]
);
} else {
throw new CException(
'Unknown output formatting: ' . $this->compilerOutputFormatting
);
}
}
/**
* Set import paths for compiler.
* Paths will be used for @import Sass method.
* Each path can be a filesystem path.
* Or an Yii path with application aliases (like "application").
*
* @param array|string $paths Single import path or a list of paths
*/
protected function setImportPaths($paths)
{
$paths = (array) $paths;
$preparedPaths = array();
foreach ($paths as $originalPath) {
$preparedPath = Yii::getPathOfAlias($originalPath);
if ($preparedPath !== false) {
$preparedPaths[] = $preparedPath;
} else {
$preparedPaths[] = $originalPath;
}
}
$this->scssc->setImportPaths($preparedPaths);
}
/**
* Save a list of parsed files to the cache
* with the time files were last modified.
* Must be called right after the compilation.
*
* @param string $sourcePath Path to the source SCSS file
* @throws CException
*/
protected function saveParsedFilesInfoToCache($sourcePath)
{
$parsedFiles = $this->parsedFiles;
//getParsedFiles is deprecated, daarom $this->>parseFiles erbij gemaakt
//$parsedFiles = $this->compiler->getParsedFiles();
$parsedFiles[$sourcePath] = filemtime($sourcePath);
$info = array(
'compiledFiles' => $parsedFiles,
'autoAddCurrentDirectoryAsImportPath' => $this->autoAddCurrentDirectoryAsImportPath,
'enableCompass' => $this->enableCompass,
'importPaths' => $this->compiler->getImportPaths(),
'compilerOutputFormatting' => $this->compilerOutputFormatting,
);
$this->cacheSet($this->getCacheCompiledPrefix() . $sourcePath, $info);
}
/**
* Get path to a compiled CSS file
*
* @param string $sourcePath Path to a source SCSS file
* @return string
* @throws CException
*/
protected function getCompiledCssFilePath($sourcePath)
{
// Add 8 last characters from the hash string
// to prevent overwriting of previously compiled files
// with the same basename but from another source directory
return $this->getWritableDirectoryPath($this->sassCompiledPath)
. basename($sourcePath, '.scss')
. '-'
. substr(md5($sourcePath), -8)
. '.css';
}
/**
* Does source SCSS file need to be compiled/recompiled
*
* @param string $path Path to a source SCSS file
* @return boolean
* @throws CException
*/
protected function isCompilationNeeded($path)
{
if ($this->forceCompilation) {
return true;
}
if (!file_exists($this->getCompiledCssFilePath($path))) {
return true;
}
if (!$this->allowOverwrite) {
return false;
}
if ($this->isLastCompilationEnvironmentChanged($path)) {
return true;
}
return false;
}
/**
* Is the previous compilation environment changed for specified SCSS file.
* Check component's settings and modification time of imported files.
*
* @param string $path Path to a source SCSS file
* @return boolean
* @throws CException
*/
protected function isLastCompilationEnvironmentChanged($path)
{
$compiledInfo = $this->cacheGet(
$this->getCacheCompiledPrefix() . $path
);
$fieldsToCheckForChangedValue = array(
'autoAddCurrentDirectoryAsImportPath',
'enableCompass',
'compilerOutputFormatting',
);
foreach ($fieldsToCheckForChangedValue as $field) {
if (!isset($compiledInfo[$field]) or
$compiledInfo[$field] !== $this->$field) {
return true;
}
}
// if (
// !isset($compiledInfo['importPaths']) or
// $compiledInfo['importPaths'] !== $this->compiler->getImportPaths()
// ) {
// return true;
// }
if (
empty($compiledInfo['compiledFiles'])
or !is_array($compiledInfo['compiledFiles'])
) {
return true;
}
foreach ($compiledInfo['compiledFiles'] as $compiledFile => $previousModificationTime) {
if (!empty($compiledFile)) {
if (substr($compiledFile, -5) !== '.scss') {
$compiledFile .= '.scss';
}
if (file_exists($compiledFile) && filemtime($compiledFile) > $previousModificationTime) {
return true;
}
}
}
return false;
}
/**
* Get prefix for cache entries
*
* @return string
*/
protected function getCacheCompiledPrefix()
{
return 'sass-compiled-';
}
/**
* Get path of the writable directory
* Create/chmod directory if needed
*
* @param string $path
* @throws CException
* @return string
*/
protected function getWritableDirectoryPath($path)
{
$parsedAlias = YiiBase::getPathOfAlias($path);
if ($parsedAlias !== false) {
$path = $parsedAlias;
}
if (!is_dir($path)) {
if (!mkdir($path, $this->writableDirectoryPermissions, true)) {
throw new CException('Can not create directory: ' . $path);
}
}
if (!chmod($path, $this->writableDirectoryPermissions)) {
throw new CException(
'Can not chmod(' . decoct($this->writableDirectoryPermissions)
. ') directory: ' . $path
);
}
return rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
}
/**
* Save a value to the cache.
* Uses Yii cache if available.
* Writes to a yii-sass cache file otherwise.
*
* @param string $name
* @param mixed $value
* @throws CException
* @return boolean
*/
protected function cacheSet($name, $value)
{
if ($this->getCacheComponent()) {
return $this->getCacheComponent()->set($name, $value, 604800);
}
$path = $this->getCachePathForName($name);
if (!file_put_contents($path, serialize($value), LOCK_EX)) {
throw new CException('Can not write to the cache file: ' . $path);
}
if (!chmod($path, $this->writableFilePermissions)) {
throw new CException(
'Can not chmod(' . decoct($this->writableFilePermissions)
. ') the cache file: ' . $path
);
}
return true;
}
/**
* Get a value from the cache.
* Uses Yii cache if available.
* Looks for a yii-sass cache file otherwise.
*
* @param string $name
* @return mixed Cached value or false if entry is not found
* @throws CException
*/
protected function cacheGet($name)
{
if ($this->getCacheComponent()) {
return $this->getCacheComponent()->get($name);
}
$path = $this->getCachePathForName($name);
if (is_readable($path)) {
return unserialize(file_get_contents($path));
}
return false;
}
/**
* Get path for an yii-sass cache entry
*
* @param string $name
* @return string
* @throws CException
*/
protected function getCachePathForName($name)
{
$maxFileLength = 255;
$suffix = md5($name) . '.bin';
$convertedName = basename($name);
$convertedName = preg_replace(
'/[^A-Za-z0-9\_\.]+/',
'-',
$convertedName
);
$convertedName = trim($convertedName, '-');
$convertedName = substr(
$convertedName,
0,
$maxFileLength - strlen('-' . $suffix)
);
$convertedName = strtolower($convertedName);
if ($convertedName) {
$convertedName .= '-';
}
return $this->getWritableDirectoryPath($this->cachePath)
. $convertedName . $suffix;
}
/**
* @return ICache Yii caching component
*/
protected function getCacheComponent()
{
return $this->cacheComponentId ? Yii::app()->getComponent($this->cacheComponentId) : null;
}
/**
* Publishes the compiled file and returns the path to it.
* @param $sourcePath
* @param int $type
* @return string path to compiled css file
* @throws Exception
*/
public function publishAndGetPath($sourcePath, $type = self::PATH_TYPE_RELATIVE)
{
$path = $this->publish($sourcePath);
return $path[$type];
}
/**
* Gets a list of scss files, and if necessary compiles them, if no, return an array of compiled css files
* @param $sources
* @param $baseUrl
* @param int $type
* @return array
* @throws Exception
*/
public function publishAndGetPathArray($sources,$baseUrl, $type = self::PATH_TYPE_RELATIVE)
{
$tmpArray = [];
foreach ($sources as $source)
{
$tmpArray[] = $this->getCompiledFile($baseUrl . $source,true);;
}
return $tmpArray;
}
}