Assets.php
См. документацию.
1 <?php
2 
3 /**
4  * @file
5  * @brief преобразование относительных путей в шаблонах
6  *
7  */
8 
9 namespace Wrong\Html;
10 
11 /**
12  * @brief Assets класс преобразует относительные пути в шаблонах
13  *
14  */
15 
16 class Assets
17 {
18  /**
19  * если в шаблоне была указана константа ASSETS_PATH то добавляет её ко всем атрибутам src тегов img, script, и атрибутам href тегов link на странице
20  * преобразуя тем самым относительные пути к ресурсам в абсолютные
21  * @param string $str код страницы на выходе
22  *
23  * @return string $str код с заменами атрибутов
24  */
25  public static function path($str)
26  {
27  $assets_path = '/' . trim(USE_ASSETS_PATH, '/') . '/';
28  $str = preg_replace_callback('#<script[^<>]+src\s*=\s*(\'|")([^/]+[^\'"]+)(\'|")[^>]*>#', function ($matches) use ($assets_path) {
29  if (preg_match('#^http(s|):#', $matches[2]) || strlen($matches[0]) > 500) return $matches[0];
30  return str_replace($matches[2], $assets_path . $matches[2] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[2]), $matches[0]);
31  }, $str);
32  $str = preg_replace_callback('#<img[^<>]+src\s*=\s*(\'|")([^/]+[^\'"]+)(\'|")[^>]*>#', function ($matches) use ($assets_path) {
33  if (preg_match('#^http(s|):#', $matches[2]) || strlen($matches[0]) > 500) return $matches[0];
34  return str_replace($matches[2], $assets_path . $matches[2] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[2]), $matches[0]);
35  }, $str);
36  $str = preg_replace_callback('#<link[^<>]+href\s*=\s*(\'|")([^/]+[^\'"]+)(\'|")[^>]*>#', function ($matches) use ($assets_path) {
37  if (preg_match('#^http(s|):#', $matches[2]) || strlen($matches[0]) > 500) return $matches[0];
38  return str_replace($matches[2], $assets_path . $matches[2] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[2]), $matches[0]);
39  }, $str);
40  $str = preg_replace_callback('#<a[^<>]+href\s*=\s*(\'|")([^/]+[^\'"]+\.(jpg|png|jpeg))(\'|")[^>]*>#', function ($matches) use ($assets_path) {
41  if (preg_match('#^http(s|):#', $matches[2]) || strlen($matches[0]) > 500) return $matches[0];
42  return str_replace($matches[2], $assets_path . $matches[2] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[2]), $matches[0]);
43  }, $str);
44  $str = preg_replace_callback('#<[^<>]+background-image:\s*url\‍(([^\‍(\‍)]+)\‍);[^<>]*>#', function ($matches) use ($assets_path) {
45  if (preg_match('#^http(s|):#', $matches[1]) || strlen($matches[0]) > 500) return $matches[0];
46  return str_replace($matches[1], $assets_path . $matches[1] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[1]), $matches[0]);
47  }, $str);
48  $str = preg_replace_callback('#<[^<>]+background-image:\s*url\‍("([^\‍(\‍)"]+)"\‍);[^<>]*>#', function ($matches) use ($assets_path) {
49  if (preg_match('#^http(s|):#', $matches[1]) || strlen($matches[0]) > 500) return $matches[0];
50  return str_replace($matches[1], $assets_path . $matches[1] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[1]), $matches[0]);
51  }, $str);
52  $str = preg_replace_callback('#<[^<>]+background-image:\s*url\‍(\'([^\‍(\‍)\']+)\'\‍);[^<>]*>#', function ($matches) use ($assets_path) {
53  if (preg_match('#^http(s|):#', $matches[1]) || strlen($matches[0]) > 500) return $matches[0];
54  return str_replace($matches[1], $assets_path . $matches[1] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[1]), $matches[0]);
55  }, $str);
56  $str = preg_replace_callback('#<[^<>]+data-setbg="([^\‍(\‍)"]+)"[^<>]*>#', function ($matches) use ($assets_path) {
57  if (preg_match('#^http(s|):#', $matches[1]) || strlen($matches[0]) > 500) return $matches[0];
58  return str_replace($matches[1], $assets_path . $matches[1] . '?' . filemtime($_SERVER['DOCUMENT_ROOT'] . $assets_path . $matches[1]), $matches[0]);
59  }, $str);
60  return $str;
61  }
62 }
const USE_ASSETS_PATH
Assets класс преобразует относительные пути в шаблонах
Definition: Assets.php:17
static path($str)
Definition: Assets.php:25
setcookie('FROM_UID', $uid, [ 'expires'=> time()+31536000, 'path'=> '/', 'domain'=> $_SERVER['HTTP_HOST'], 'secure'=> Wrong\Start\Env::$e->IS_SECURE, 'httponly'=> false, 'samesite'=> Wrong\Start\Env::$e->IS_SECURE ? 'None' :'Lax']) or setcookie('FROM_UID' $_SERVER['HTTP_HOST']
Definition: from-user.php:36