home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-includes / class.wp-dependencies.php < prev    next >
Encoding:
PHP Script  |  2008-05-21  |  5.4 KB  |  194 lines

  1. <?php
  2.  
  3. class WP_Dependencies {
  4.     var $registered = array();
  5.     var $queue = array();
  6.     var $to_do = array();
  7.     var $done = array();
  8.     var $args = array();
  9.  
  10.     function WP_Dependencies() {
  11.         $args = func_get_args();
  12.         call_user_func_array( array(&$this, '__construct'), $args );
  13.     }
  14.  
  15.     function __construct() {}
  16.  
  17.     /**
  18.      * Do the dependencies
  19.      *
  20.      * Process the items passed to it or the queue.  Processes all dependencies.
  21.      *
  22.      * @param mixed handles (optional) items to be processed.  (void) processes queue, (string) process that item, (array of strings) process those items
  23.      * @return array Items that have been processed
  24.      */
  25.     function do_items( $handles = false ) {
  26.         // Print the queue if nothing is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
  27.         $handles = false === $handles ? $this->queue : (array) $handles;
  28.         $this->all_deps( $handles );
  29.  
  30.         foreach( $this->to_do as $handle ) {
  31.             if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
  32.                 if ( $this->registered[$handle]->src ) { // Else it defines a group.
  33.                     $this->do_item( $handle );
  34.                 }
  35.                 $this->done[] = $handle;
  36.             }
  37.         }
  38.  
  39.         $this->to_do = array();
  40.         return $this->done;
  41.     }
  42.  
  43.     function do_item( $handle ) {
  44.         return isset($this->registered[$handle]);
  45.     }
  46.  
  47.     /**
  48.      * Determines dependencies
  49.      *
  50.      * Recursively builds array of items to process taking dependencies into account.  Does NOT catch infinite loops.
  51.      *
  52.  
  53.      * @param mixed handles Accepts (string) dep name or (array of strings) dep names
  54.      * @param bool recursion Used internally when function calls itself
  55.      */
  56.     function all_deps( $handles, $recursion = false ) {
  57.         if ( !$handles = (array) $handles )
  58.             return false;
  59.  
  60.         foreach ( $handles as $handle ) {
  61.             $handle = explode('?', $handle);
  62.             if ( isset($handle[1]) )
  63.                 $this->args[$handle[0]] = $handle[1];
  64.             $handle = $handle[0];
  65.  
  66.             if ( isset($this->to_do[$handle]) ) // Already grobbed it and its deps
  67.                 continue;
  68.  
  69.             $keep_going = true;
  70.             if ( !isset($this->registered[$handle]) )
  71.                 $keep_going = false; // Script doesn't exist
  72.             elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
  73.                 $keep_going = false; // Script requires deps which don't exist (not a necessary check.  efficiency?)
  74.             elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true ) )
  75.                 $keep_going = false; // Script requires deps which don't exist
  76.  
  77.             if ( !$keep_going ) { // Either script or its deps don't exist.
  78.                 if ( $recursion )
  79.                     return false; // Abort this branch.
  80.                 else
  81.                     continue; // We're at the top level.  Move on to the next one.
  82.             }                    
  83.  
  84.             $this->to_do[$handle] = true;
  85.         }
  86.  
  87.         if ( !$recursion ) // at the end
  88.             $this->to_do = array_keys( $this->to_do );
  89.         return true;
  90.     }
  91.  
  92.     /**
  93.      * Adds item
  94.      *
  95.      * Adds the item only if no item of that name already exists
  96.      *
  97.      * @param string handle Script name
  98.      * @param string src Script url
  99.      * @param array deps (optional) Array of script names on which this script depends
  100.      * @param string ver (optional) Script version (used for cache busting)
  101.      * @return array Hierarchical array of dependencies
  102.      */
  103.     function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
  104.         if ( isset($this->registered[$handle]) )
  105.             return false;
  106.         $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
  107.         return true;
  108.     }
  109.  
  110.     /**
  111.      * Adds extra data
  112.      *
  113.      * Adds data only if script has already been added
  114.      *
  115.      * @param string handle Script name
  116.      * @param string data_name Name of object in which to store extra data
  117.      * @param array data Array of extra data
  118.      * @return bool success
  119.      */
  120.     function add_data( $handle, $data_name, $data ) {
  121.         if ( !isset($this->registered[$handle]) )
  122.             return false;
  123.         return $this->registered[$handle]->add_data( $data_name, $data );
  124.     }
  125.  
  126.     function remove( $handles ) {
  127.         foreach ( (array) $handles as $handle )
  128.             unset($this->registered[$handle]);
  129.     }
  130.  
  131.     function enqueue( $handles ) {
  132.         foreach ( (array) $handles as $handle ) {
  133.             $handle = explode('?', $handle);
  134.             if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
  135.                 $this->queue[] = $handle[0];
  136.                 if ( isset($handle[1]) )
  137.                     $this->args[$handle[0]] = $handle[1];
  138.             }
  139.         }
  140.     }
  141.  
  142.     function dequeue( $handles ) {
  143.         foreach ( (array) $handles as $handle )
  144.             unset( $this->queue[$handle] );
  145.     }
  146.  
  147.     function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
  148.         switch ( $list ) :
  149.         case 'registered':
  150.         case 'scripts': // back compat
  151.             if ( isset($this->registered[$handle]) )
  152.                 return $this->registered[$handle];
  153.             break;
  154.         case 'to_print': // back compat
  155.         case 'printed': // back compat
  156.             if ( 'to_print' == $list )
  157.                 $list = 'to_do';
  158.             else
  159.                 $list = 'printed';
  160.         default:
  161.             if ( in_array($handle, $this->$list) )
  162.                 return true;
  163.             break;
  164.         endswitch;
  165.         return false;
  166.     }
  167.  
  168. }
  169.  
  170. class _WP_Dependency {
  171.     var $handle;
  172.     var $src;
  173.     var $deps = array();
  174.     var $ver = false;
  175.     var $args = null;
  176.  
  177.     var $extra = array();
  178.  
  179.     function _WP_Dependency() {
  180.         @list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
  181.         if ( !is_array($this->deps) )
  182.             $this->deps = array();
  183.         if ( !$this->ver )
  184.             $this->ver = false;
  185.     }
  186.  
  187.     function add_data( $name, $data ) {
  188.         if ( !is_scalar($name) )
  189.             return false;
  190.         $this->extra[$name] = $data;
  191.         return true;
  192.     }
  193. }
  194.