<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class KwAssetCollector
{
  private $jsIncludes = array();
  private $cssIncludes = array();

  protected function __construct()
  {
  }

  public static function getInstance()
  {
    static $cache = null;
    if (is_null($cache)) {
      $cache = new KwAssetCollector();
    }

    return $cache;
  }

  public function includeJs(string $handle, string $jsPath): void
  {
    $this->jsIncludes[$handle] = $jsPath;
  }

  public function includeCss(string $handle, string $cssPath): void
  {
    $this->cssIncludes[$handle] = $cssPath;
  }

  public function buildJsIncludes(): string
  {
    $buffer = '';
    foreach ($this->jsIncludes as $handle => $jsPath) {
      $buffer .= '<script src="layout/js/' . $jsPath . '" type="text/javascript"></script>';
      $buffer .= "\n";
    }

    return $buffer;
  }

  public function buildCssIncludes(): string
  {
    $buffer = '';
    foreach ($this->cssIncludes as $handle => $cssPath) {
      $buffer .= '<link rel="stylesheet" type="text/css" href="layout/css/' . $cssPath . '">';
      $buffer .= "\n";
    }

    return $buffer;
  }
}