1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-01 03:02:43 +01:00
phorge-phorge/src/infratructure/celerity/response/CelerityStaticResourceResponse.php

146 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class CelerityStaticResourceResponse {
private $symbols = array();
private $needsResolve = true;
private $resolved;
2011-01-25 20:57:47 +01:00
private $metadata = array();
private $metadataBlock = 0;
private $behaviors = array();
public function __construct() {
if (isset($_REQUEST['__metablock__'])) {
$this->metadataBlock = (int)$_REQUEST['__metablock__'];
}
}
public function addMetadata($metadata) {
$id = count($this->metadata);
$this->metadata[$id] = $metadata;
return $this->metadataBlock.'_'.$id;
}
public function getMetadataBlock() {
return $this->metadataBlock;
}
public function initBehavior($behavior, array $config = array()) {
$this->requireResource('javelin-behavior-'.$behavior);
$this->behaviors[$behavior][] = $config;
return $this;
}
public function requireResource($symbol) {
$this->symbols[$symbol] = true;
$this->needsResolve = true;
return $this;
}
private function resolveResources() {
if ($this->needsResolve) {
$map = CelerityResourceMap::getInstance();
$this->resolved = $map->resolveResources(array_keys($this->symbols));
$this->needsResolve = false;
}
return $this;
}
public function renderResourcesOfType($type) {
$this->resolveResources();
$output = array();
foreach ($this->resolved as $resource) {
if ($resource['type'] == $type) {
$output[] = $this->renderResource($resource);
}
}
return implode("\n", $output);
}
private function renderResource(array $resource) {
switch ($resource['type']) {
case 'css':
$path = phutil_escape_html($resource['path']);
return '<link rel="stylesheet" type="text/css" href="'.$path.'" />';
case 'js':
$path = phutil_escape_html($resource['path']);
2011-01-25 20:57:47 +01:00
return '<script type="text/javascript" src="'.$path.'">'.
'</script>';
}
throw new Exception("Unable to render resource.");
}
2011-01-25 20:57:47 +01:00
public function renderHTMLFooter() {
$data = array();
if ($this->metadata) {
$json_metadata = json_encode($this->metadata);
$this->metadata = array();
} else {
$json_metadata = '{}';
}
// Even if there is no metadata on the page, Javelin uses the mergeData()
// call to start dispatching the event queue.
$data[] = 'JX.Stratcom.mergeData('.$this->metadataBlock.', '.
$json_metadata.');';
$onload = array();
if ($this->behaviors) {
$behavior = json_encode($this->behaviors);
$onload[] = 'JX.initBehaviors('.$behavior.')';
$this->behaviors = array();
}
if ($onload) {
foreach ($onload as $func) {
$data[] = 'JX.onload(function(){'.$func.'});';
}
}
if ($data) {
$data = implode("\n", $data);
return '<script type="text/javascript">//<![CDATA['."\n".
$data.'//]]></script>';
} else {
return '';
}
}
public function renderAjaxResponse($payload, $error = null) {
$response = array(
'error' => $error,
'payload' => $payload,
);
if ($this->metadata) {
$response['javelin_metadata'] = $this->metadata;
$this->metadata = array();
}
if ($this->behaviors) {
$response['javelin_behaviors'] = $this->behaviors;
$this->behaviors = array();
}
$response = 'for (;;);'.json_encode($response);
return $response;
}
}