forked from mitrii/yii2-embedjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbedJs.php
41 lines (32 loc) · 909 Bytes
/
EmbedJs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace shqear\widgets;
use yii\web\View;
use yii\widgets\Block;
class EmbedJs extends Block
{
public $position = View::POS_END;
public $key;
/**
* @param array|integer $config array or block position constant
* @return void|static
*/
static function begin($config = [])
{
$config = is_array($config) ? $config : ['position' => $config];
parent::begin($config);
}
/**
* @return string|void
*/
public function run()
{
$block = ob_get_clean();
$block = trim($block);
$jsBlockPattern = '|^<script[^>]*>(?P<block_content>.+?)</script>$|is';
if (preg_match($jsBlockPattern, $block, $matches)) {
$block = $matches['block_content'];
}
$key = (empty($this->key)) ? md5($block) : $this->key;
$this->view->registerJs($block, $this->position, $key);
}
}