public function testDeploymentUrls()
 {
     $package = new ResourceDeploymentPackage();
     $package->resourcesToDeploy = [__FILE__, __DIR__ . "/../../src/Deployment/Deployable.php"];
     $urls = $package->getDeployedUrls();
     $cwd = getcwd();
     $this->assertEquals(["/deployed" . str_replace("\\", "/", str_replace($cwd, "", __FILE__)), "/deployed" . str_replace("\\", "/", str_replace($cwd, "", realpath(__DIR__ . "/../../src/Deployment/Deployable.php")))], $urls);
 }
Esempio n. 2
0
    /**
     * Returns two script tags, one loading the script manager and the second executing all other
     * scripts registered with AddScript.
     *
     * This takes a new approach - instead of adding all the scripts to the <head> tag when
     * rendering full HTML, we use the JS script manager. This means the approach is the same
     * whether the request is a normal HTML page, or an AJAX post. This is borne out of frustration
     * with finding some libraries like Google maps not working if added to the page via AJAX. Any client
     * side issues with the script loader will now become a major concern and should get addressed quickly.
     *
     * @see ResourceLoader::addResource()
     * @return string
     */
    public static function getResourceInjectionHtml()
    {
        $package = new ResourceDeploymentPackage();
        $package->resourcesToDeploy[] = __DIR__ . "/../../resources/resource-manager.js";
        $urls = $package->deploy();
        $html = "<script src=\"" . $urls[0] . "\" type=\"text/javascript\"></script>";
        $context = Application::current()->context();
        $preLoadedFiles = [];
        // CSS files are safe to load immediately and might avoid 'flicker' by so doing.
        if (!$context->isXhrRequest()) {
            foreach (self::$resources as $item) {
                $dependantResources = $item[1];
                foreach ($dependantResources as $resource) {
                    if (in_array($resource, $preLoadedFiles)) {
                        continue;
                    }
                    $parts = explode(".", $resource);
                    $extension = strtolower($parts[sizeof($parts) - 1]);
                    if ($extension == "css") {
                        $html .= "\n<link type=\"text/css\" rel=\"stylesheet\" href=\"" . $resource . "\" />";
                        $preLoadedFiles[] = $resource;
                    }
                    if ($extension == "js") {
                        $html .= "\n<script type=\"text/javascript\" src=\"" . $resource . "\"></script>";
                        $preLoadedFiles[] = $resource;
                    }
                }
            }
        }
        $groupedItems = [];
        foreach (self::$resources as $index => $resource) {
            $dependantResources = $resource[1];
            $dependantResources = array_diff($dependantResources, $preLoadedFiles);
            $urls = implode("", $dependantResources);
            if (!isset($groupedItems[$urls])) {
                $groupedItems[$urls] = [$resource[0], $dependantResources];
            } else {
                $groupedItems[$urls][0] .= "\n\t" . $resource[0];
            }
        }
        $tags = "";
        array_walk($groupedItems, function ($item) use(&$tags, $preLoadedFiles) {
            $source = trim($item[0]);
            $dependantResources = $item[1];
            if (sizeof($dependantResources) > 0) {
                $resourcesArray = '[ "' . implode('", "', $dependantResources) . '" ]';
                if ($source == "") {
                    $source = 'window.resourceManager.loadResources( ' . $resourcesArray . ' );';
                } else {
                    $source = 'window.resourceManager.loadResources( ' . $resourcesArray . ', function()
{
	' . $source . '
} );';
                }
            } else {
                if ($source != "") {
                    $source = 'window.resourceManager.runWhenDocumentReady( function()
{
	' . $source . '
} );';
                }
            }
            $tags .= $source . "\n";
        });
        if (trim($tags) != "") {
            $html .= <<<HTML
<script type="text/javascript">
//<![CDATA[
{$tags}
//]]>
</script>
HTML;
        }
        return $html;
    }
Esempio n. 3
0
    /**
     * Returns two script tags, one loading the script manager and the second executing all other
     * scripts registered with AddScript.
     *
     * This takes a new approach - instead of adding all the scripts to the <head> tag when
     * rendering full HTML, we use the JS script manager. This means the approach is the same
     * whether the request is a normal HTML page, or an AJAX post. This is borne out of frustration
     * with finding some libraries like Google maps not working if added to the page via AJAX. Any client
     * side issues with the script loader will now become a major concern and should get addressed quickly.
     *
     * @see ResourceLoader::AddResource()
     * @return string
     */
    public static function getResourceInjectionHtml()
    {
        $package = new ResourceDeploymentPackage();
        $package->resourcesToDeploy[] = __DIR__ . "/../../resources/resource-manager.js";
        $urls = $package->deploy();
        $html = "<script src=\"" . $urls[0] . "\" type=\"text/javascript\"></script>";
        $context = new Context();
        $preLoadedCssFiles = [];
        // CSS files are safe to load immediately and might avoid 'flicker' by so doing.
        if (!$context->IsAjaxRequest) {
            foreach (self::$resources as $item) {
                $dependantResources = $item[1];
                foreach ($dependantResources as $resource) {
                    if (in_array($resource, $preLoadedCssFiles)) {
                        continue;
                    }
                    $parts = explode(".", $resource);
                    $extension = strtolower($parts[sizeof($parts) - 1]);
                    if ($extension == "css") {
                        $html .= "\n<link type=\"text/css\" rel=\"stylesheet\" href=\"" . $resource . "\" />";
                        $preLoadedCssFiles[] = $resource;
                    }
                }
            }
        }
        $tags = "";
        array_walk(self::$resources, function ($item) use(&$tags, $preLoadedCssFiles) {
            $source = $item[0];
            $dependantResources = $item[1];
            if ($source == "") {
                // CSS files have already been loaded, so we don't need to ask for them to be loaded again. We only
                // do this if there is no source code attached to this load. If source is attached we need to include
                // the CSS as we don't want the JS to start until the CSS is ready.
                $dependantResources = array_diff($dependantResources, $preLoadedCssFiles);
            }
            if (sizeof($dependantResources) > 0) {
                $resourcesArray = '[ "' . implode('", "', $dependantResources) . '" ]';
                if ($source == "") {
                    $source = 'window.resourceManager.loadResources( ' . $resourcesArray . ' );';
                } else {
                    $source = 'window.resourceManager.loadResources( ' . $resourcesArray . ', function()
{
	' . $source . '
} );';
                }
            } else {
                if ($source != "") {
                    $source = 'window.resourceManager.runWhenDocumentReady( function()
{
	' . $source . '
} );';
                }
            }
            $tags .= $source . "\n";
        });
        if (trim($tags) != "") {
            $html .= "\n<script type=\"text/javascript\">\n{$tags}</script>";
        }
        return $html;
    }