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

Source for file Mai.php

Documentation is available at Mai.php

  1. <?php
  2. /**
  3.  * Mai
  4.  * 
  5.  * Copyright (C) 2009  Mai (Me Artificial Intelligence)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  * 
  20.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  21.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  22.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  23.  * @link http://www.launchpad.net/mai
  24.  * 
  25.  * @category Mai
  26.  * @package Base
  27.  * 
  28.  */
  29.  
  30. /**
  31.  * Mai is an overall class for Mai.
  32.  * 
  33.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  34.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  35.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  36.  * @link http://www.launchpad.net/mai
  37.  * 
  38.  * @category Mai
  39.  * @package Base
  40.  * 
  41.  */
  42.  
  43. class Mai
  44. {
  45.     /**
  46.      * Text types
  47.      */
  48.     const MSG_TYPE_NORMAL 1;
  49.     const MSG_TYPE_COMMAND 2;
  50.     const MSG_TYPE_LOGOUT 3;
  51.  
  52.     /**
  53.      * Environments
  54.      */
  55.     const ENV_PRODUCTION 1;
  56.     const ENV_DEVELOPMENT 2;
  57.     const ENV_STAGING 3;
  58.  
  59.     /**
  60.      * Word types (Dutch)
  61.      * @link http://nl.wikipedia.org/wiki/Woordsoort
  62.      *
  63.      *  Note: if you change these values synchronising with Mai-Ai.org will give
  64.      *  incorrect data. Also your database will be out of sync.
  65.      * 
  66.      *
  67.      */
  68.     const WT_BIJVOEGELIJK_NAAMWOORD 1;
  69.     const WT_LIDWOORD 2;
  70.     const WT_HOOFDTELWOORD 3;
  71.     const WT_RANGTELWOORD 4;
  72.     const WT_VOEGWOORD 5;
  73.     const WT_VRAGEND_VOORNAAMWOORD 6;
  74.     const WT_PERSOONLIJK_VOORNAAMWOORD 7;
  75.     const WT_PERSOONLIJK_VOORNAAMWOORD_VOORWERP 8;
  76.     const WT_AANWIJZEND_VOORNAAMWOORD 9;
  77.     const WT_BEZITTELIJK_VOORNAAMWOORD 10;
  78.     const WT_VOORZETSEL 11;
  79.     const WT_WERKWOORD 12;
  80.     const WT_ZELFSTANDIG_NAAMWOORD 13;
  81.     const WT_NAAM 14;
  82.     const WT_BEGROETING 15;
  83.     const WT_AFSCHEID 16;
  84.     const WT_PLAATSNAAM 17;
  85.     
  86.     /**
  87.      * Verbs (times)
  88.      */
  89.     const WVT_TEGENWOORDIGE_TIJD 1;
  90.     const WVT_VERLEDEN_TIJD 2;
  91.     
  92.     /**
  93.      * Word numbers (one or multiple)
  94.      */
  95.     const WN_ENKELVOUD 1;
  96.     const WN_MEERVOUD 2;
  97.     
  98.     /**
  99.      * Persons (ik, jij, etc.)
  100.      */
  101.     const WP_1_ENKELVOUD 1//Ik
  102.     const WP_1_MEERVOUD 2//Wij
  103.     
  104.     const WP_2_ENKELVOUD 3//Jij, u
  105.     const WP_2_MEERVOUD 4//Jullie, u
  106.     
  107.     const WP_3_ENKELVOUD 5//Hij, zij, het
  108.     const WP_3_MEERVOUD 6//Zij
  109.     
  110.     /**
  111.      * Analyzer response types
  112.      */
  113.     const AN_RESPONSE_FULL_PATTERN 1;
  114.     
  115.     /**
  116.      * Storage types
  117.      */
  118.     const STORAGE_TYPE_ARRAY 1;
  119.     const STORAGE_TYPE_CLASS 2;
  120.     
  121.     /**
  122.      * @var Mai Singleton instance
  123.      */
  124.     protected static $_instance;
  125.     
  126.     /**
  127.      * @var integer The enviroment of Mai.
  128.      */
  129.     protected static $_env self::ENV_PRODUCTION;
  130.     
  131.     /**
  132.      * @var boolean Define TRUE when using an autoloader. This disables the require_once statements.
  133.      */
  134.     protected static $_autoloader false;
  135.     
  136.     /**
  137.      * @var string Directory of the Mai class.
  138.      */
  139.     protected static $_baseDir;
  140.     
  141.     /**
  142.      * @var array Allowed text types.
  143.      */
  144.     public static $allowedTextTypes array(self::MSG_TYPE_NORMALself::MSG_TYPE_COMMANDself::MSG_TYPE_LOGOUT);
  145.  
  146.     /**
  147.      * @var array The names of the environments
  148.      */
  149.        public static $_envNames array(
  150.         Mai::ENV_DEVELOPMENT     => 'development',
  151.         Mai::ENV_PRODUCTION        => 'production',
  152.         Mai::ENV_STAGING        => 'staging'
  153.     );
  154.     
  155.     /**
  156.      * @var array Allowed environments.
  157.      */
  158.     protected static $_envAllowed array(self::ENV_DEVELOPMENTself::ENV_PRODUCTIONself::ENV_STAGING);
  159.     
  160.     /**
  161.      * @var array Allowed word types.
  162.      */
  163.     public static $allowedWordTypes array(
  164.         self::WT_AANWIJZEND_VOORNAAMWOORDself::WT_AFSCHEIDself::WT_BEGROETING,
  165.         self::WT_BEZITTELIJK_VOORNAAMWOORDself::WT_BIJVOEGELIJK_NAAMWOORDself::WT_HOOFDTELWOORD,
  166.         self::WT_LIDWOORDself::WT_NAAMself::WT_PERSOONLIJK_VOORNAAMWOORD,
  167.         self::WT_PERSOONLIJK_VOORNAAMWOORD_VOORWERPself::WT_PLAATSNAAMself::WT_RANGTELWOORD,
  168.         self::WT_VOEGWOORDself::WT_VOORZETSELself::WT_VRAGEND_VOORNAAMWOORD,
  169.         self::WT_WERKWOORDself::WT_ZELFSTANDIG_NAAMWOORD
  170.     );
  171.     
  172.     /**
  173.      * @var array Allowed times.
  174.      */
  175.     public static $allowedTimes array(
  176.         self::WVT_TEGENWOORDIGE_TIJD,
  177.         self::WVT_VERLEDEN_TIJD
  178.     );
  179.     
  180.     /**
  181.      * @var array Allowed numbers (for words).
  182.      */
  183.     public static $allowedNumbers array(
  184.         self::WN_ENKELVOUD,
  185.         self::WN_MEERVOUD
  186.     );
  187.     
  188.     /**
  189.      * @var array Allowed persons (for words).
  190.      */
  191.     public static $allowedPersons array(
  192.         self::WP_1_ENKELVOUD,
  193.         self::WP_1_MEERVOUD,
  194.         self::WP_2_ENKELVOUD,
  195.         self::WP_2_MEERVOUD,
  196.         self::WP_3_ENKELVOUD,
  197.         self::WP_3_MEERVOUD
  198.     );
  199.     
  200.     /**
  201.      * @var Boolean Whether Mai has been setup.
  202.      */
  203.     protected static $_init false;
  204.     
  205.     /**
  206.      * Private constructor to prevent creation of this object.
  207.      * @return void 
  208.      */
  209.     private function __construct(){}
  210.     
  211.     /**
  212.      * Retrieve singleton instance
  213.      * 
  214.      * @return Mai_Config 
  215.      */
  216.     public static function getInstance()
  217.     {
  218.         if (null === self::$_instance{
  219.             //Start up
  220.             self::$_instance new self();
  221.             //Define the location of this class
  222.             self::$_baseDir realpath(dirname(__FILE__));
  223.         }
  224.         return self::$_instance;
  225.     }
  226.  
  227.     /**
  228.      * Start Mai.
  229.      * @return void 
  230.      */
  231.     public static function init()
  232.     {
  233.         //Start up the Mai class
  234.         self::getInstance();
  235.         
  236.         //Load required file for config
  237.         if(!self::getAutoloader()){    require_once 'Mai/Config.php'}
  238.         //Let the Mai_Config class know that Mai is using it 
  239.         Mai_Config::getInstance(self::getInstance());        
  240.         //Define whether the scripts uses an autoloader or not
  241.         if(is_bool(Mai_Config::getVar()->application->autoloader))
  242.         {
  243.             self::$_autoloader Mai_Config::getVar()->application->autoloader;
  244.         }
  245.         
  246.         //Load required field for Registry
  247.            if(!self::getAutoloader()){    require_once 'Mai/Registry.php'}
  248.         //Start registry
  249.         Mai_Registry::getInstance(self::getInstance());
  250.         
  251.         //Load required field for stat
  252.         if(!self::getAutoloader()){    require_once 'Mai/Stat.php'}
  253.         
  254.         //Start the Mai_Stat class
  255.         Mai_Stat::start();
  256.  
  257.         if(Mai_Config::getVar()->log->enabled)
  258.         {
  259.             //Start logger
  260.             Mai_Log::init(self::$_instance);
  261.         }
  262.         
  263.         Mai_Log::info("Mai: Mai started.");
  264.         
  265.         //Define innit
  266.         self::$_init true;
  267.     }
  268.     
  269.     /**
  270.      * End Mai
  271.      * @return void 
  272.      */
  273.     public static function end()
  274.     {
  275.         //End Mai_Stat
  276.         Mai_Stat::end();
  277.  
  278.         Mai_Log::info('Mai: Mai ended.');
  279.         Mai_Log::info('Mai: Execution time: '.Mai_Stat::getInstance()->executionTime());
  280.         Mai_Log::info('Mai: Memory end: '.number_format(memory_get_usage()0'.'',').' bytes.');
  281.         Mai_Log::info('Mai: Memory peak: '.number_format(memory_get_peak_usage()0'.'',').' bytes.');
  282.         
  283.         if(Mai_Config::getVar()->log->enabled && self::$_env === self::ENV_DEVELOPMENT)
  284.         {
  285.             //Flush log
  286.             Mai_Log::flush();
  287.         }
  288.     }
  289.  
  290.     /**
  291.      * Returns the value of $_init.
  292.      * @return boolean 
  293.      */
  294.     public static function isInit()
  295.     {
  296.         return self::$_init;
  297.     }
  298.     
  299.     /**
  300.      * Set the environment type.
  301.      * @param $env 
  302.      * @return void 
  303.      */
  304.     public static function setEnv($env)
  305.     {
  306.         if(Mai::isInit())
  307.         {
  308.             trigger_error("Can't set enviroment, Mai has already been started."E_USER_ERROR);
  309.         }
  310.         
  311.         if(!isset($env))
  312.         {
  313.             throw new UnexpectedValueException('Environment must be set.');
  314.         }
  315.         
  316.         if(!in_array($envself::$_envAllowed))
  317.         {
  318.             throw new UnexpectedValueException('Environment is not valid.');
  319.         }
  320.         
  321.         self::$_env $env;
  322.     }
  323.     
  324.     /**
  325.      * Return the environment type.
  326.      * @return string 
  327.      */
  328.     public static function getEnv()
  329.     {
  330.         return self::$_env;
  331.     }
  332.     
  333.     /**
  334.      * Set whether classes are loaded using a autoloader or not.
  335.      * @param boolean $value 
  336.      * @return void 
  337.      */
  338.     public static function setAutoloader($value)
  339.     {
  340.         if(!is_bool($value))
  341.         {
  342.             throw new UnexpectedValueException('Value can only be a boolean.');
  343.         }
  344.         
  345.         self::$_autoloader $value;
  346.     }
  347.     
  348.     /**
  349.      * Return the value of $_autoloader.
  350.      * @return boolean 
  351.      */
  352.     public static function getAutoloader()
  353.     {
  354.         return self::$_autoloader;
  355.     }
  356.     
  357.     /**
  358.      * Return the location of the Mai class.
  359.      * @return string 
  360.      */
  361.     public static function baseDir()
  362.     {
  363.         self::getInstance();
  364.         
  365.         return self::$_baseDir;
  366.     }
  367.     
  368.     /**
  369.      * Cloning of a singleton class is not allowed.
  370.      * @return void 
  371.      */
  372.     final public function __clone()
  373.     {
  374.         trigger_error('Clone is not allowed.'E_USER_ERROR);
  375.     }
  376. }

Documentation generated on Mon, 27 Jul 2009 19:55:09 +0200 by phpDocumentor 1.4.1