on_page) $result .= $component->render($kind); return $result; } protected $dependencies = array(); protected $js = array(); protected $css = array(); protected $on_page = FALSE; protected $printed = array('css' => FALSE, 'js' => FALSE); public function __construct($name) { self::add($name, $this); } public function add_to_page() { $this->on_page = TRUE; } public function requires($name) { if (!isset($this->dependencies[$name])) $this->dependencies[$name] = drupal_component($name); return $this; } public function add_js($path, $options = array()) { if (!isset($this->js[$path])) $this->js[$path] = $options; else $this->js[$path] = array_merge($this->js[$path], $options); return $this; } public function add_css($path, $options = array()) { if (!isset($this->css[$path])) $this->css[$path] = $options; else $this->css[$path] = array_merge($this->css[$path], $options); return $this; } public function reset() { $this->css = $this->js = $this->dependencies = array(); $this->on_page = FALSE; $this->printed = array('css' => FALSE, 'js' => FALSE); return $this; } public function render($kind) { $result = ''; if ($this->printed[$kind]) return $result; foreach ($this->dependencies as $dependency) $result .= $dependency->render($kind); foreach ($this->{$kind} as $path => $options) { $result .= $this->{'render_' . $kind}($path, $options); } $this->printed[$kind] = TRUE; return $result; } protected function render_css($path, $options) { return '' . "\n"; } protected function render_js($path, $options) { return '' . "\n"; } } drupal_component('farbtastic') ->requires('drupal') ->add_js('misc/farbtastic.js') ->add_css('misc/farbtastic.css') ->add_to_page(); drupal_component('drupal') ->requires('jquery') ->add_js('misc/drupal.js'); drupal_component('jquery') ->add_js('misc/jquery.js'); drupal_component('jquery') ->reset() ->add_js('sites/all/modules/mymodule/jquery-1.5.js'); print Drupal_Component::output('js'); // Outputs: // // //