1+ <?php
2+ use \Bitrix \Main ;
3+ use \Bitrix \Main \Localization \Loc as Loc ;
4+ use \Bitrix \Main \SystemException as SystemException ;
5+ use \Bitrix \Main \Page \Asset as Asset ;
6+ use \Bitrix \Main \Application as App ;
7+
8+ if (!defined ('B_PROLOG_INCLUDED ' ) || B_PROLOG_INCLUDED !== true ) {
9+ die ();
10+ }
11+
12+ class PafnutyLessComponent extends CBitrixComponent {
13+
14+ /**
15+ * Check Required Modules
16+ * @throws Exception
17+ */
18+ protected function checkModules () {
19+ if (!Main \Loader::includeModule ('pafnuty.less ' )) {
20+ throw new SystemException (Loc::getMessage ('CVP_PAFNUTY_LESS_MODULE_NOT_INSTALLED ' ));
21+ }
22+
23+ }
24+
25+ /**
26+ * Load language file
27+ */
28+ public function onIncludeComponentLang () {
29+ $ this ->includeComponentLang (basename (__FILE__ ));
30+ Loc::loadMessages (__FILE__ );
31+ }
32+
33+ /**
34+ * Prepare Component Params
35+ */
36+ public function onPrepareComponentParams ($ params ) {
37+
38+ $ params ['COMPRESS ' ] = ($ params ['COMPRESS ' ] == 'Y ' );
39+ $ params ['SOURSEMAP ' ] = ($ params ['SOURSEMAP ' ] == 'Y ' );
40+
41+ $ params ['FILES ' ] = is_array ($ params ['FILES ' ]) ? $ params ['FILES ' ] : array ('template_styles.less ' );
42+
43+ $ params ['PATH_TO_FILES ' ] = isset ($ params ['PATH ' ]) && strlen (trim ($ params ['PATH ' ]))
44+ ? preg_replace (array ('~^/~ ' , '~/$~ ' ), '/ ' , trim ($ params ['PATH ' ]))
45+ : SITE_TEMPLATE_PATH . '/less/ ' ;
46+
47+ $ params ['PATH_TO_FILES_CSS ' ] = isset ($ params ['PATH_CSS ' ]) && strlen (trim ($ params ['PATH_CSS ' ]))
48+ ? preg_replace (array ('~^/~ ' , '~/$~ ' ), '/ ' , trim ($ params ['PATH_CSS ' ]))
49+ : SITE_TEMPLATE_PATH . '/ ' ;
50+
51+ return $ params ;
52+ }
53+
54+ public function executeComponent () {
55+
56+ try {
57+
58+ $ this ->checkModules ();
59+
60+ $ context = App::getInstance ()->getContext ();
61+
62+ $ rootFolder = $ context ->getServer ()->getDocumentRoot ();
63+
64+ $ lessFolder = $ this ->arParams ['PATH_TO_FILES ' ];
65+ $ fileNames = $ this ->arParams ['FILES ' ];
66+ $ compress = $ this ->arParams ['COMPRESS ' ];
67+ $ sourceMap = $ this ->arParams ['SOURSEMAP ' ];
68+ $ cssFolder = $ this ->arParams ['PATH_TO_FILES_CSS ' ];
69+
70+ $ compile = new \Pafnuty \Less \lessCompiler ($ rootFolder , $ lessFolder , $ fileNames , $ cssFolder , $ compress , $ sourceMap );
71+
72+ $ file = $ compile ->compile ();
73+
74+ if ($ file ['error ' ]) {
75+ // Заменим виндовые слеши.
76+ $ rootFolder = str_replace ('\\' , '/ ' , $ rootFolder );
77+
78+ /**
79+ * @todo Оптимизировать надо бы по хорошему этот момент
80+ */
81+ Asset::getInstance ()->addString ('<style>.less-error-wrapper{position:fixed;z-index:1500;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,.7)}.less-error-content{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:100%}.less-error{padding:40px;color:#666;background:#fff;font:400 17px/30px Consolas,Menlo,"DejaVu Sans Mono","Courier New",monospace,serif;margin:0;word-wrap:break-word;-webkit-box-shadow:0 0 30px rgba(0,0,0,.6);box-shadow:0 0 30px rgba(0,0,0,.6)}.less-error-header{font-size:24px;font-weight:700;margin-bottom:20px;text-align:center;color:#f75b5b}.less-error-content pre{line-height:1;border:0;background-color:transparent}</style> ' );
82+
83+ // Выведем текст ошибки
84+ $ errorText = '<div class="less-error-wrapper"><div class="less-error-content"><div class="less-error"><div class="less-error-header"> ' . Loc::GetMessage ('PAF_ERROR_LESS_COMPILE ' ) . '</div><pre> ' . str_replace ($ rootFolder , '' , $ file ['error ' ]) . '</pre></div></div></div> ' ;
85+ echo $ errorText ;
86+ }
87+
88+ } catch (SystemException $ e ) {
89+ ShowError ($ e ->getMessage ());
90+ }
91+
92+ }
93+
94+ }
0 commit comments