public function importModels(ViewEvent $event) { $parents = $event->getView()->get('_parents'); $jsClasses = $this->modelToJsClasses($parents); $modelAsJson = $this->modelToJsonData($parents, $event->getView()->get('_models')); $template = ''; if (!empty($jsClasses)) { $template = sprintf("\n(function() { \n\t%s })();\n", $jsClasses); } if (!empty($modelAsJson)) { $template .= sprintf("\nMinute.Loader = function(\$scope) { \n\t%s };\n", $modelAsJson); } $event->setContent($template ? sprintf('<scr' . 'ipt>%s</script>', $template) : ''); }
/** * Get all <event name="$eventName"></event> tags and fire $eventName events, * then replace the <event> tag with the response generated by event handlers. * */ public function render() { $html = !empty($this->layout) ? $this->compiler->replaceContentTagInLayout($this->content ?? '', $this->layout) : $this->content ?? ''; $event = new ViewParserEvent($this, $html); $this->dispatcher->fire(ViewParserEvent::VIEWPARSER_RENDER, $event); $output = preg_replace_callback('~(<minute-event.*?</minute-event>)~mi', function ($matches) { $tag = $matches[1]; $attrs = (array) new SimpleXMLElement($tag); $attrs = current($attrs); $event = new ViewEvent($this, $tag, $attrs); $eventName = strtolower(strtr($attrs['name'], ['_' => '.'])); if (preg_match('/^(user|import)\\./i', $eventName)) { if (!empty($attrs['as'])) { $importEvent = new ImportEvent($event, $tag, $attrs); $this->dispatcher->fire($eventName, $importEvent); foreach ($attrs as $k => $v) { if ($k !== 'name' && $k !== 'as') { $values[] = sprintf('%s="%s"', $k, htmlentities($v)); } } $data = $importEvent->getContent(); $import = sprintf('<minute-importer into="%s" data="%s" %s></minute-importer>', $attrs['as'], htmlentities(json_encode($data ?? [])), join(' ', $values ?? [])); $event->setContent($import); } else { $this->dispatcher->fire($eventName, $event); } } else { throw new ViewError("Server side event '{$eventName}' failed (server side events may start with USER_ or IMPORT_ only)"); } return $event->getContent(); }, $event->getHtml()); /** @var Helper $helper */ foreach ($this->helpers ?? [] as $helper) { if ($content = $helper->getTemplate()) { $tagName = $helper->getPosition() === Helper::POSITION_BODY ? '</body>' : '</head>'; $output = $this->tagUtils->insertBeforeTag($tagName, $content, $output); } } $output = preg_replace_callback('~<title></title>~', function () { $url = $this->request->getPath(); $event = new SeoEvent($url); $this->dispatcher->fire(SeoEvent::SEO_GET_TITLE, $event); $result = sprintf('<title>%s</title>%s', $event->getTitle() ?: ucwords(trim(join(' ', preg_split('/\\W+/', $url)))), PHP_EOL); foreach ($event->getMeta() as $meta) { $result .= sprintf('<meta name="%s" content="%s" />%s', $meta['name'], $meta['content'], PHP_EOL); } return $result; }, $output); return $output; }
public function importSession(ViewEvent $event) { if (!$this->database->isConnected()) { return; } /** @var RouteEx $route */ $view = $event->getView(); $vars = $view->getVars(); $route = $view->get('_route'); $data = $this->getCachedSessionData(false); $data['request'] = array_merge(['url' => getenv('REQUEST_URI')], $this->request->getParameters()); $data['params'] = array_diff_key($route->getDefaults(), array_flip(['controller', 'auth', 'models', '_route'])); foreach ($vars as $key => $value) { if ($key[0] !== '_' && (is_scalar($value) || is_array($value))) { $data['vars'][$key] = $value; } } if (!empty($data['site'])) { $data['site']['version'] = $this->database->hasRdsAccess() ? 'production' : 'debug'; } $printer = sprintf('<script' . '>Minute.setSessionData(%s)</script>', json_encode($data)); $event->setContent($printer); }