PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/mobileapp/lib/janative/entity/extension.php

https://gitlab.com/alexprowars/bitrix
PHP | 159 lines | 149 code | 4 blank | 6 comment | 1 complexity | 54db98d067ec3e7b8f307a21156198c2 MD5 | raw file
  1. <?php
  2. namespace Bitrix\MobileApp\Janative\Entity;
  3. use Bitrix\Main\IO\File;
  4. use Bitrix\Main\SystemException;
  5. use Bitrix\MobileApp\Janative\Manager;
  6. use Bitrix\MobileApp\Janative\Utils;
  7. class Extension extends Base
  8. {
  9. protected static $modificationDates = [];
  10. protected static $dependencies = [];
  11. /**
  12. * Extension constructor.
  13. *
  14. * @param $identifier
  15. * @throws \Exception
  16. */
  17. public function __construct($identifier)
  18. {
  19. $this->path = Manager::getExtensionPath($identifier);
  20. $this->baseFileName = 'extension';
  21. $desc = Utils::extractEntityDescription($identifier);
  22. $this->name = $desc['name'];
  23. $this->namespace = $desc['namespace'];
  24. if (!$this->path)
  25. {
  26. throw new SystemException("Extension '{$desc['fullname']}' doesn't exists");
  27. }
  28. }
  29. private function getBundleContent(): string
  30. {
  31. $files = $this->getBundleFiles();
  32. $content = "";
  33. foreach ($files as $path)
  34. {
  35. $file = new File($path);
  36. if ($file->isExists())
  37. {
  38. $content .= "\n".$file->getContents()."\n";
  39. }
  40. }
  41. return $content;
  42. }
  43. /**
  44. * Returns content of extension without depending extensions
  45. *
  46. * @return string
  47. * @throws \Bitrix\Main\IO\FileNotFoundException
  48. */
  49. public function getContent(): string
  50. {
  51. $content = "";
  52. $extensionFile = new File($this->path . '/' . $this->baseFileName . '.js');
  53. if ($extensionData = $this->getResult())
  54. {
  55. if ($extensionData !== '')
  56. {
  57. $content .= <<<JS
  58. this.jnExtensionData.set("{$this->name}", {$extensionData});
  59. JS;
  60. }
  61. }
  62. if ($extensionFile->isExists() && $extensionContent = $extensionFile->getContents())
  63. {
  64. $content .= $this->getBundleContent();
  65. $content .= $extensionContent;
  66. }
  67. $content .= "\n\n";
  68. return $content;
  69. }
  70. private function getResult(): ?string
  71. {
  72. $file = new File($this->path . '/extension.php');
  73. $result = null;
  74. if ($file->isExists())
  75. {
  76. $result = include($file->getPath());
  77. }
  78. if (is_array($result) && count($result) > 0)
  79. {
  80. return json_encode($result);
  81. }
  82. return null;
  83. }
  84. public function getIncludeExpression($callbackName = 'onExtensionsLoaded'): string
  85. {
  86. $relativePath = $this->getPath() . 'extension.js';
  87. $localizationPhrases = $this->getLangDefinitionExpression();
  88. $content = "\n//extension '{$this->name}'\n";
  89. $content .= "{$localizationPhrases}\n";
  90. $content .= "loadScript(\"{$relativePath}\", false, {$callbackName});";
  91. return $content;
  92. }
  93. /**
  94. * Returns list of dependencies by name of extensions
  95. *
  96. * @param $name
  97. * @param array $list
  98. * @param array $alreadyResolved
  99. * @return array
  100. * @throws \Exception
  101. */
  102. public static function getResolvedDependencyList($name, &$list = [], &$alreadyResolved = []): array
  103. {
  104. $baseExtension = new Extension($name);
  105. $depsList = $baseExtension->getDependencyList();
  106. $alreadyResolved[] = $name;
  107. if (count($depsList) > 0)
  108. {
  109. foreach ($depsList as $ext)
  110. {
  111. $depExtension = new Extension($ext);
  112. $extDepsList = $depExtension->getDependencyList();
  113. if (count($extDepsList) == 0)
  114. {
  115. array_unshift($list, $ext);
  116. }
  117. elseif (!in_array($ext, $alreadyResolved))
  118. {
  119. self::getResolvedDependencyList($ext, $list, $alreadyResolved);
  120. }
  121. }
  122. }
  123. $list[] = $name;
  124. return array_unique($list);
  125. }
  126. protected function onBeforeModificationDateSave(&$value)
  127. {
  128. // TODO: Implement onBeforeModificationDateSave() method.
  129. }
  130. /**
  131. * @return array
  132. * @throws \Exception
  133. */
  134. protected function resolveDependencies(): array
  135. {
  136. $name = ($this->namespace !== "bitrix" ? $this->namespace . ":" : "") . $this->name;
  137. return self::getResolvedDependencyList($name);
  138. }
  139. }