System_ProcWatch
[ class tree: System_ProcWatch ] [ index: System_ProcWatch ] [ all elements ]

Source for file Config.php

Documentation is available at Config.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: ProcWatch :: Config                                |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2003-2004 Michael Wallner <mike@iworks.at>             |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Config.php 304228 2010-10-10 03:05:12Z clockwerx $
  15.  
  16. /**
  17. * Requires PEAR.
  18. */
  19. require_once 'PEAR.php';
  20.  
  21. /** 
  22. * System_ProcWatch_Config
  23. * Build a configuration array for System_ProcWatch
  24. * Usage:
  25. * <code>
  26. * $cf = System_ProcWatch_Config::fromXmlFile('/etc/procwatch.xml');
  27. * $pw = &new System_ProcWatch($cf);
  28. * </code>
  29. @author       Michael Wallner <mike@php.net>
  30. @package      System_ProcWatch
  31. @category     System
  32. @version      $Revision: 304228 $
  33. @access       public
  34. */
  35. {
  36.     /**
  37.     * Get config array from XML file
  38.     *
  39.     * @throws   PEAR_Error
  40.     * @static
  41.     * @access   public
  42.     * @return   mixed 
  43.     * @param    string  $file   path to XML file
  44.     */
  45.     function fromXmlFile($file)
  46.     {
  47.         if (!is_file($file)) {
  48.             return PEAR::raiseError("File '$file' doesn't exist");
  49.         }
  50.         
  51.         return System_ProcWatch_Config::fromXml(implode(''file($file)));
  52.     }
  53.  
  54.     /**
  55.     * Get config array from XML string
  56.     *
  57.     * @throws   PEAR_Error
  58.     * @static
  59.     * @access   public
  60.     * @return   mixed 
  61.     * @param    string  $xml    XML string
  62.     */
  63.     function fromXml($xml)
  64.     {
  65.         include_once('System/ProcWatch/Config/Parser.php');
  66.         
  67.         $config &new System_ProcWatch_Config_Parser();
  68.         $result $config->getConf($xml);
  69.  
  70.         unset($config);
  71.  
  72.         return  $result;
  73.     }
  74.     
  75.     /**
  76.     * Get config array from INI file
  77.     *
  78.     * @throws   PEAR_Error
  79.     * @static
  80.     * @access   public
  81.     * @return   mixed 
  82.     * @param    string  $file   path to INI file
  83.     */
  84.     function fromIniFile($file)
  85.     {
  86.         if (!is_file($file)) {
  87.             return PEAR::raiseError("File '$file' doesn't exist");
  88.         }
  89.         
  90.         $jobs   parse_ini_file($filetrue);
  91.         $result = array();
  92.         
  93.         foreach ($jobs as $job => $c{
  94.  
  95.             $result[$job= array();
  96.             
  97.             $result[$job]['pattern'][$c['match']] $c['pattern'];
  98.             $result[$job]['condition'][$c['condition']] = array();
  99.             
  100.             // set a refernce for easier use
  101.             if ($c['condition'== 'attr'{
  102.                 $result[$job]['condition']['attr'= array();
  103.                 $reference &$result[$job]['condition']['attr'][$c['attr']];
  104.             elseif($c['condition'== 'presence'{
  105.                 $reference &$result[$job]['condition'][$c['condition']];
  106.             else {
  107.                 continue;
  108.             }
  109.             
  110.             // (min|max|sum|is|isnot) conditions
  111.             if (isset($c['min'])) {
  112.                 $reference['min'$c['min'];
  113.             }
  114.             if (isset($c['max'])) {
  115.                 $reference['max'$c['max'];
  116.             }
  117.             if (isset($c['sum'])) {
  118.                 $reference['sum'$c['sum'];
  119.             }
  120.             if (isset($c['is'])) {
  121.                 $reference['is'$c['is'];
  122.             }
  123.             if (isset($c['isnot'])) {
  124.                 $reference['isnot'$c['isnot'];
  125.             }
  126.             
  127.             $result[$job]['execute'= array('shell' => array($c['execute']));
  128.         }
  129.         unset($jobs);
  130.         
  131.         return $result;
  132.     }
  133.     
  134.     /**
  135.     * Get config array from an array :)
  136.     *
  137.     * This method in fact does a sanity check on the supplied config array
  138.     * and should only be used for testing purposes.
  139.     * 
  140.     * @throws   PEAR_Error
  141.     * @static
  142.     * @access   public
  143.     * @return   mixed 
  144.     * @param    array   $array  config array to check
  145.     */
  146.     function fromArray($array)
  147.     {
  148.         foreach ($array as $job{
  149.             if (
  150.                 !isset($job['pattern'])                         OR
  151.                 !isset($job['condition'])                       OR
  152.                 !isset($job['execute'])                         OR
  153.                 
  154.                 !is_array($job['pattern'])                      OR
  155.                 !is_array($job['condition'])                    OR
  156.                 !is_array($job['execute'])                      OR
  157.                 
  158.                 (   (
  159.                     !isset($job['execute']['shell'])            AND
  160.                     !isset($job['execute']['php'])
  161.                     )                                           OR
  162.                     (
  163.                     !is_array(@$job['execute']['shell'])        AND
  164.                     !is_array(@$job['execute']['php'])
  165.                     )
  166.                 )                                               OR
  167.                 (   (
  168.                     !isset($job['condition']['presence'])       AND
  169.                     !isset($job['condition']['attr'])
  170.                     )                                           OR
  171.                     (
  172.                     !is_array(@$job['condition']['presence'])   AND
  173.                     !is_array(@$job['condition']['attr'])
  174.                     )
  175.                 )
  176.             {
  177.                 return PEAR::raiseError('Invalid configuration array supplied');
  178.             }
  179.         }
  180.         return $array;
  181.     }
  182. }
  183. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.