|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakeCMS Core |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Core |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
|
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
|
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Core\Less; |
|
17
|
|
|
|
|
18
|
|
|
use JBZoo\Utils\FS; |
|
19
|
|
|
use JBZoo\Less\Exception; |
|
20
|
|
|
use JBZoo\Less\Less as JBLess; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Less |
|
24
|
|
|
* |
|
25
|
|
|
* @package Core\Lib\Less |
|
26
|
|
|
*/ |
|
27
|
|
|
class Less extends JBLess |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Compile less file. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $lessFile |
|
34
|
|
|
* @param null|string $basePath |
|
35
|
|
|
* @return array |
|
36
|
|
|
* @throws Exception |
|
37
|
|
|
*/ |
|
38
|
|
|
public function compile($lessFile, $basePath = null) |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
|
|
$basePath = $this->_prepareBasepath($basePath, dirname($lessFile)); |
|
42
|
|
|
|
|
43
|
|
|
$cache = new Cache($this->_options); |
|
44
|
|
|
$cache->setFile($lessFile, $basePath); |
|
45
|
|
|
|
|
46
|
|
|
$isExpired = $cache->isExpired(); |
|
47
|
|
|
$isForce = $this->_options->get('force', false, 'bool'); |
|
48
|
|
|
|
|
49
|
|
|
if ($isForce || $cache->isExpired()) { |
|
50
|
|
|
$result = $this->_driver->compile($lessFile, $basePath); |
|
51
|
|
|
$cache->save($result); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$isExpired = ($isForce) ? true : $isExpired; |
|
55
|
|
|
$return = [FS::clean($cache->getFile(), '/'), $isExpired]; |
|
56
|
|
|
|
|
57
|
|
|
} catch (\Exception $e) { |
|
58
|
|
|
$message = 'Less error: ' . $e->getMessage(); |
|
59
|
|
|
throw new Exception($message); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $return; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.