Beispiel #1
0
    /**
     * Creates late-output <script> elements used to bootstrap the script environment.
     * @return mixed|string
     */
    public function createBootstrapScriptElements()
    {
        $model = $this->getModel();
        $serverData = $model->get('app.serverData');
        $envInitData = ['baseChain' => $serverData['baseChain']];
        $vars = [];
        foreach ($this->getJsEnvironment()->get('forwardedVars') ?: [] as $k) {
            $vars[$k] = Environment::getVars()->get($k);
        }
        if ($vars) {
            $envInitData['vars'] = $vars;
        }
        $bootInfo = ['moduleCode' => $this->getCode(), 'controllerOptions' => ['pluginRegistrations' => $serverData['pluginRegistrations']]];
        /** @var JsonView $jsonView */
        $jsonView = $this->getController()->createView('Json');
        $jsonView->setController($this->getController());
        $jsonView->init();
        $jsonView->setModel($this->getModel());
        $pendingData = $jsonView->createJsonData();
        if ($pendingData) {
            $bootInfo['controllerOptions']['pendingData'] = $pendingData;
        }
        unset($jsonView, $pendingData);
        ob_start();
        ?>
		<script type="text/javascript" class="appBootstrapScript">
			(function () {
				Promise.all(
					Array.from(document.head.querySelectorAll('script[data-src]'), function (el) {
						return System.import(el.getAttribute('data-src'));
					})
				)
				.then(function () {
					var controller;

					if (self.App && App.Environment) {
						App.DEBUG = <?php 
        echo JsonUtils::toJson(\App\DEBUG);
        ?>
;
						App.Environment.init(<?php 
        echo JsonUtils::toJson($envInitData);
        ?>
);

						if (App.Controller) {
							try {
								if ((controller = App.Controller.boot(<?php 
        echo JsonUtils::toJson($bootInfo);
        ?>
))) {
									controller.connect()
									.then(function () {
										App.controller = controller;
										controller.run();
									})
									.catch(function (e) {
										controller.handleException(e);
									});
								}
							}
							catch (e) {
								App.Controller.bail(e);
							}
						}
					}
				})
				.catch(function (e) {
					if (self.console && console.error) console.error('Bootstrap failed.', e);
					else throw e;
				});
			})();
		</script>
		<?php 
        $html = ob_get_clean();
        $html = str_replace("\n", '', $html);
        $html = preg_replace('/\\s{2,}/', '', $html);
        return $html;
    }
Beispiel #2
0
 public function createJson()
 {
     return JsonUtils::toJson($this->createJsonData());
 }