Skip to content

Commit e7ad3a0

Browse files
First working sample
1 parent 2be861c commit e7ad3a0

File tree

4 files changed

+207
-36
lines changed

4 files changed

+207
-36
lines changed

LICENCE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) Sergey Gripinskiy web-architect@mail.ru
2+
3+
The above copyright notice and this permission notice shall be included
4+
in all copies or substantial portions of the Software.
5+
6+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
8+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
10+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
11+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
12+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
14+
USAGE OF THIS SOFTWARE OR ANY PART OF IT FOR COMMERCIAL PURPOSES
15+
OR AS PART OF COMMERCIAL PRODUCTS IS STRONGLY PROHIBITED FOR
16+
COMPANIES "1C", "1C-Bitrix" AND ANY OTHER RELATED OR CHILD COMPANIES
17+
OF MENTIONED ABOVE.
18+
19+
ИСПОЛЬЗОВАНИЕ НАСТОЯЩЕГО ПРОДУКТА ИЛИ ЛЮБОЙ ЕГО ЧАСТИ В КОММЕРЧЕСКИХ ЦЕЛЯХ
20+
ИЛИ КАК ЧАСТЬ КОММЕРЧЕСКОГО ПРОДУКТА КАТЕГОРИЧЕСКИ ЗАПРЕЩАЕТСЯ ДЛЯ
21+
КОМПАНИЙ "1С", "1С-БИТРИКС" И ДЛЯ ЛЮБОЙ ДРУГОЙ СВЯЗАННОЙ ИЛИ ДОЧЕРНЕЙ
22+
КОМПАНИИ УПОМЯНУТЫХ ВЫШЕ ОРГАНИЗАЦИЙ.

composer.json

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
{
2-
"name": "webarchitect609/bitrix-neverinclude",
3-
"description": "Bitrix automatic module including",
4-
"authors": [
5-
{
6-
"name": "Sergey Gripinskiy",
7-
"email": "web-architect@mail.ru"
8-
}
9-
],
10-
"require": {}
2+
"name": "webarchitect609/bitrix-neverinclude",
3+
"description": "Automatic Bitrix modules loader helps you forget about CModule::IncludeModule and Loader::includeModule FOREVER!",
4+
"homepage": "https://github.com/webarchitect609/bitrix-neverinclude",
5+
"keywords": [
6+
"bitrix",
7+
"module",
8+
"autoloading"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Sergey Gripinskiy",
14+
"email": "web-architect@mail.ru"
15+
}
16+
],
17+
"require": {
18+
// "php": ">=5.3.0",
19+
// "composer/installers": "~1"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"WebArch\\BitrixNeverInclude\\": "src/main"
24+
}
25+
}
1126
}

src/main/BitrixNeverInclude.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
4+
namespace WebArch\BitrixNeverInclude;
5+
6+
use Bitrix\Main\Loader;
7+
use RuntimeException;
8+
9+
class BitrixNeverInclude
10+
{
11+
12+
/**
13+
* @var BitrixNeverInclude
14+
*/
15+
protected static $instance;
16+
17+
/**
18+
* @return array
19+
*/
20+
protected function getModulePrefixMap()
21+
{
22+
//TODO Наполнить карту
23+
return [
24+
25+
'iblock' => [
26+
'CIBlock',
27+
'_CIB',
28+
'CAllIBlock',
29+
'CEventIblock',
30+
'CRatingsComponentsIBlock',
31+
],
32+
33+
];
34+
}
35+
36+
protected function __construct()
37+
{
38+
39+
}
40+
41+
/**
42+
* @return BitrixNeverInclude
43+
*/
44+
protected function getInstance()
45+
{
46+
if (is_null(static::$instance)) {
47+
static::$instance = new static();
48+
}
49+
50+
return static::$instance;
51+
}
52+
53+
/**
54+
* Зарегистрировать автолоадер модулей Битрикс
55+
* и **НАВСЕГДА ЗАБЫТЬ** про CModule::IncludeModule и Loader::includeModule
56+
*
57+
* @throws RuntimeException
58+
*/
59+
public static function registerModuleAutoload()
60+
{
61+
if (!spl_autoload_register([static::getInstance(), 'autoloadModule'])) {
62+
throw new RuntimeException('Error register autoloader ' . __CLASS__);
63+
}
64+
}
65+
66+
/**
67+
* @param string $class
68+
*/
69+
protected function autoloadModule($class)
70+
{
71+
$moduleName = $this->recognizeOldModule($class);
72+
73+
if (!$moduleName) {
74+
$moduleName = $this->recognizeNewModule($class);
75+
}
76+
77+
if (!$moduleName) {
78+
return;
79+
}
80+
81+
Loader::includeModule($moduleName);
82+
Loader::autoLoad($class);
83+
84+
}
85+
86+
/**
87+
* Определение имени модуля по префиксу класса
88+
*
89+
* @param string $class
90+
*
91+
* @return string Пустая строка, если не удалось определить имя модуля
92+
*/
93+
protected function checkPrefix($class)
94+
{
95+
96+
foreach (static::getModulePrefixMap() as $moduleName => $prefixList) {
97+
foreach ($prefixList as $prefix) {
98+
if (strpos($class, $prefix) === 0) {
99+
return (string)$moduleName;
100+
}
101+
}
102+
}
103+
104+
return '';
105+
}
106+
107+
/**
108+
* Определение модуля для старых классов из глобальной области
109+
*
110+
* @param string $class
111+
*
112+
* @return string Пустая строка, если не удалось определить имя модуля
113+
*/
114+
protected function recognizeOldModule($class)
115+
{
116+
/*
117+
* Если содержит обратный слеш или не начинается с 'C',
118+
* то это не старый класс из глобальной области
119+
*/
120+
if (
121+
strpos($class, 'C') !== 0
122+
|| strpos($class, '\\') !== false
123+
) {
124+
return '';
125+
}
126+
127+
return $this->checkPrefix($class);
128+
}
129+
130+
/**
131+
* Определение модуля по namespace класса
132+
*
133+
* @param string $class
134+
*
135+
* @return string Пустая строка, если не удалось определить имя модуля
136+
*/
137+
protected function recognizeNewModule($class)
138+
{
139+
$chunks = explode('\\', $class);
140+
141+
/*
142+
* Это стандартный битриксовый модуль
143+
*/
144+
if ('Bitrix' === $chunks[0] && isset($chunks[1])) {
145+
return strtolower($chunks[1]);
146+
}
147+
148+
/*
149+
* Иной кастомный модуль
150+
*/
151+
if (isset($chunks[0], $chunks[1])) {
152+
return sprintf(
153+
'%s.%s',
154+
strtolower($chunks[0]),
155+
strtolower($chunks[1])
156+
);
157+
}
158+
159+
return '';
160+
}
161+
}

src/main/lib/NeverInclude.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)