| 1 |
<?php |
| 2 |
/** |
| 3 |
* Scss Compiler |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* GhostKit_Scss_Compiler |
| 10 |
*/ |
| 11 |
class GhostKit_Scss_Compiler { |
| 12 |
/** |
| 13 |
* Arguments for compile scss file |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
private $args; |
| 18 |
|
| 19 |
/** |
| 20 |
* Information about folders and name input scss file |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private $input_file_info; |
| 25 |
|
| 26 |
/** |
| 27 |
* Information about folders and name output css file |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private $output_file_info; |
| 32 |
|
| 33 |
/** |
| 34 |
* Input SCSS file content. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $scss_in; |
| 39 |
|
| 40 |
/** |
| 41 |
* GhostKit_Scss_Compiler constructor. |
| 42 |
* |
| 43 |
* @param array $args - Arguments for compile scss file. |
| 44 |
*/ |
| 45 |
public function __construct( $args = null ) { |
| 46 |
$this->args = $args; |
| 47 |
|
| 48 |
if ( $this->validation_of_arguments() ) { |
| 49 |
$this->input_file_info = pathinfo( $this->args['input_file'] ); |
| 50 |
$this->output_file_info = pathinfo( $this->args['output_file'] ); |
| 51 |
$this->scss_in = $this->get_input_file_contents( $this->args['input_file'] ); |
| 52 |
$is_dir = $this->check_and_create_non_exist_output_folder(); |
| 53 |
|
| 54 |
if ( |
| 55 |
$this->scss_in && |
| 56 |
$is_dir && |
| 57 |
isset( $this->input_file_info['dirname'] ) && |
| 58 |
! empty( $this->input_file_info['dirname'] ) |
| 59 |
) { |
| 60 |
$this->scss(); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Compilation scss and save css output file. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
private function scss() { |
| 71 |
require_once ghostkit()->plugin_path . 'composer-libraries/vendor/autoload.php'; |
| 72 |
|
| 73 |
$scss = new ScssPhp\ScssPhp\Compiler(); |
| 74 |
$scss->setOutputStyle( ScssPhp\ScssPhp\OutputStyle::COMPRESSED ); |
| 75 |
$scss->addImportPath( $this->input_file_info['dirname'] ); |
| 76 |
$scss->setVariables( $this->args['variables'] ); |
| 77 |
|
| 78 |
// Compile scss. |
| 79 |
$result = $scss->compile( $this->scss_in ); |
| 80 |
|
| 81 |
// Prepare RTL. |
| 82 |
$parser = new Sabberworm\CSS\Parser( $result ); |
| 83 |
$tree = $parser->parse(); |
| 84 |
$rtlcss = new MoodleHQ\RTLCSS\RTLCSS( $tree ); |
| 85 |
$rtlcss->flip(); |
| 86 |
|
| 87 |
// phpcs:ignore |
| 88 |
file_put_contents( trailingslashit( $this->output_file_info['dirname'] ) . '/' . $this->output_file_info['basename'], $result ); |
| 89 |
|
| 90 |
// phpcs:ignore |
| 91 |
file_put_contents( |
| 92 |
trailingslashit( $this->output_file_info['dirname'] ) . '/' . $this->input_file_info['filename'] . '-rtl.min.' . $this->output_file_info['extension'], |
| 93 |
$tree->render() |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Validation of compile arguments. |
| 99 |
* |
| 100 |
* @return bool |
| 101 |
*/ |
| 102 |
private function validation_of_arguments() { |
| 103 |
$validation = false; |
| 104 |
|
| 105 |
if ( |
| 106 |
! empty( $this->args ) && |
| 107 |
is_array( $this->args ) && |
| 108 |
isset( $this->args['input_file'] ) && |
| 109 |
isset( $this->args['output_file'] ) && |
| 110 |
isset( $this->args['variables'] ) |
| 111 |
) { |
| 112 |
$validation = true; |
| 113 |
} |
| 114 |
|
| 115 |
return $validation; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get content of input scss file. |
| 120 |
* |
| 121 |
* @param string $file_name - Absolute path to file. |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
private function get_input_file_contents( $file_name ) { |
| 125 |
$contents = false; |
| 126 |
if ( file_exists( $file_name ) ) { |
| 127 |
// phpcs:ignore |
| 128 |
$contents = file_get_contents( $file_name ); |
| 129 |
} |
| 130 |
|
| 131 |
return $contents; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Check and Create non exist output folder. |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
private function check_and_create_non_exist_output_folder() { |
| 140 |
$is_dir = false; |
| 141 |
|
| 142 |
if ( |
| 143 |
isset( $this->output_file_info['dirname'] ) |
| 144 |
) { |
| 145 |
$dirname = $this->output_file_info['dirname']; |
| 146 |
$is_dir = is_dir( $dirname ); |
| 147 |
if ( ! $is_dir ) { |
| 148 |
$is_dir = mkdir( $dirname, 0777, true ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $is_dir; |
| 153 |
} |
| 154 |
} |
| 155 |
|