PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/vendor/schmunk42/yii2-giiant/src/generators/crud/ProviderTrait.php

https://gitlab.com/I-NOZex/quiz
PHP | 243 lines | 184 code | 32 blank | 27 comment | 41 complexity | d95a6c784f3091d8ef5cb118f294283e MD5 | raw file
  1. <?php
  2. namespace schmunk42\giiant\generators\crud;
  3. /*
  4. * @link http://www.diemeisterei.de/
  5. * @copyright Copyright (c) 2015 diemeisterei GmbH, Stuttgart
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Yii;
  11. use yii\helpers\FileHelper;
  12. use yii\helpers\Json;
  13. trait ProviderTrait
  14. {
  15. /**
  16. * @return array Class names of the providers declared directly under crud/providers folder.
  17. */
  18. public static function getCoreProviders()
  19. {
  20. $files = FileHelper::findFiles(
  21. __DIR__.DIRECTORY_SEPARATOR.'providers',
  22. [
  23. 'only' => ['*.php'],
  24. 'recursive' => false,
  25. ]
  26. );
  27. foreach ($files as $file) {
  28. require_once $file;
  29. }
  30. return array_filter(
  31. get_declared_classes(),
  32. function ($a) {
  33. return stripos($a, __NAMESPACE__.'\providers') !== false;
  34. }
  35. );
  36. }
  37. /**
  38. * @return array List of providers. Keys and values contain the same strings.
  39. */
  40. public function generateProviderCheckboxListData()
  41. {
  42. $coreProviders = self::getCoreProviders();
  43. return array_combine($coreProviders, $coreProviders);
  44. }
  45. protected function initializeProviders()
  46. {
  47. // TODO: this is a hotfix for an already initialized provider queue on action re-entry
  48. if ($this->_p !== []) {
  49. return;
  50. }
  51. if ($this->providerList) {
  52. foreach ($this->providerList as $class) {
  53. $class = trim($class);
  54. if (!$class) {
  55. continue;
  56. }
  57. $obj = \Yii::createObject(['class' => $class]);
  58. $obj->generator = $this;
  59. $this->_p[] = $obj;
  60. #\Yii::trace("Initialized provider '{$class}'", __METHOD__);
  61. }
  62. }
  63. \Yii::trace("CRUD providers initialized for model '{$this->modelClass}'", __METHOD__);
  64. }
  65. /**
  66. * Generates code for active field by using the provider queue.
  67. *
  68. * @param ColumnSchema $column
  69. * @param null $model
  70. *
  71. * @return mixed|string
  72. */
  73. public function activeField($attribute, $model = null)
  74. {
  75. if ($model === null) {
  76. $model = $this->modelClass;
  77. }
  78. $code = $this->callProviderQueue(__FUNCTION__, $attribute, $model, $this);
  79. if ($code !== null) {
  80. Yii::trace("found provider for '{$attribute}'", __METHOD__);
  81. return $code;
  82. } else {
  83. $column = $this->getColumnByAttribute($attribute);
  84. if (!$column) {
  85. return;
  86. } else {
  87. return parent::generateActiveField($attribute);
  88. }
  89. }
  90. }
  91. public function prependActiveField($attribute, $model = null)
  92. {
  93. if ($model === null) {
  94. $model = $this->modelClass;
  95. }
  96. $code = $this->callProviderQueue(__FUNCTION__, $attribute, $model, $this);
  97. if ($code) {
  98. Yii::trace("found provider for '{$attribute}'", __METHOD__);
  99. }
  100. return $code;
  101. }
  102. public function appendActiveField($attribute, $model = null)
  103. {
  104. if ($model === null) {
  105. $model = $this->modelClass;
  106. }
  107. $code = $this->callProviderQueue(__FUNCTION__, $attribute, $model, $this);
  108. if ($code) {
  109. Yii::trace("found provider for '{$attribute}'", __METHOD__);
  110. }
  111. return $code;
  112. }
  113. public function columnFormat($attribute, $model = null)
  114. {
  115. if ($model === null) {
  116. $model = $this->modelClass;
  117. }
  118. $code = $this->callProviderQueue(__FUNCTION__, $attribute, $model, $this);
  119. if ($code !== null) {
  120. Yii::trace("found provider for '{$attribute}'", __METHOD__);
  121. } else {
  122. $code = $this->shorthandAttributeFormat($attribute, $model);
  123. Yii::trace("using standard formatting for '{$attribute}'", __METHOD__);
  124. }
  125. return $code;
  126. }
  127. public function attributeFormat($attribute, $model = null)
  128. {
  129. if ($model === null) {
  130. $model = $this->modelClass;
  131. }
  132. $code = $this->callProviderQueue(__FUNCTION__, $attribute, $model, $this);
  133. if ($code !== null) {
  134. Yii::trace("found provider for '{$attribute}'", __METHOD__);
  135. return $code;
  136. }
  137. $column = $this->getColumnByAttribute($attribute);
  138. if (!$column) {
  139. return;
  140. } else {
  141. return $this->shorthandAttributeFormat($attribute, $model);
  142. }
  143. // don't call parent anymore
  144. }
  145. public function partialView($name, $model = null)
  146. {
  147. if ($model === null) {
  148. $model = $this->modelClass;
  149. }
  150. $code = $this->callProviderQueue(__FUNCTION__, $name, $model, $this);
  151. if ($code) {
  152. Yii::trace("found provider for partial view '{name}'", __METHOD__);
  153. }
  154. return $code;
  155. }
  156. public function relationGrid($name, $relation, $showAllRecords = false)
  157. {
  158. Yii::trace("calling provider queue for '$name'", __METHOD__);
  159. return $this->callProviderQueue(__FUNCTION__, $name, $relation, $showAllRecords);
  160. }
  161. protected function shorthandAttributeFormat($attribute, $model)
  162. {
  163. $column = $this->getColumnByAttribute($attribute, $model);
  164. if (!$column) {
  165. Yii::trace("No column for '{$attribute}' found", __METHOD__);
  166. return;
  167. } else {
  168. Yii::trace("Table column detected for '{$attribute}'", __METHOD__);
  169. }
  170. if ($column->phpType === 'boolean') {
  171. $format = 'boolean';
  172. } elseif ($column->type === 'text') {
  173. $format = 'ntext';
  174. } elseif (stripos($column->name, 'time') !== false && $column->phpType === 'integer') {
  175. $format = 'datetime';
  176. } elseif (stripos($column->name, 'email') !== false) {
  177. $format = 'email';
  178. } elseif (stripos($column->name, 'url') !== false) {
  179. $format = 'url';
  180. } else {
  181. $format = 'text';
  182. }
  183. return " '".$column->name.($format === 'text' ? '' : ':'.$format)."'";
  184. }
  185. protected function callProviderQueue($func, $args, $generator)
  186. {
  187. // TODO: should be done on init, but providerList is empty
  188. $this->initializeProviders();
  189. $args = func_get_args();
  190. unset($args[0]);
  191. // walk through providers
  192. foreach ($this->_p as $obj) {
  193. if (method_exists($obj, $func)) {
  194. $c = call_user_func_array(array(&$obj, $func), $args);
  195. // until a provider returns not null
  196. if ($c !== null) {
  197. if (is_object($args)) {
  198. $argsString = get_class($args);
  199. } elseif (is_array($args)) {
  200. $argsString = Json::encode($args);
  201. } else {
  202. $argsString = $args;
  203. }
  204. $msg = 'Using provider '.get_class($obj).'::'.$func.' '.$argsString;
  205. Yii::trace($msg, __METHOD__);
  206. return $c;
  207. }
  208. }
  209. }
  210. }
  211. }