public static function singleton($classname)
 {
     if (!isset(self::$instance)) {
         self::$instance = new $classname();
     }
     return self::$instance;
 }
 function test_find_by()
 {
     setup_sqlite_test_db();
     # PHP 5.3 needed to call magic methods statically, so
     # work around by instanciating the class as singleton
     $Test = Singleton::instance("Test");
     # create two test entries to be found
     Test::create(array("dummy" => "Test 1"), array("dummy" => "Test 2"));
     # Test find_by single id
     $dummy = $Test->find_by_id(1);
     $this->assertEqual(strtolower(get_class($dummy)), "test");
     $this->assertEqual($dummy->dummy, "Test 1");
     # Test find_by id array
     $dummies = $Test->find_by_id(array(1, 2), array("order" => "id"));
     $this->assertEqual(count($dummies), 2);
     $this->assertEqual($dummies[0]->dummy, "Test 1");
     $this->assertEqual($dummies[1]->dummy, "Test 2");
     # Test it works with strings, too
     $dummy = $Test->find_by_dummy("Test 2");
     $this->assertEqual($dummy->id, 2);
     # Test their combination
     $dummy = $Test->find_by_id_and_dummy(1, "Test 1");
     $this->assertEqual($dummy->id, 1);
     $this->assertEqual($dummies[0]->dummy, "Test 1");
     # Test their array'ed combination
     $dummies = $Test->find_by_id_and_dummy(array(1, 2), array("Test 1", "Test 2"), array("order" => "id"));
     $this->assertEqual(count($dummies), 2);
     $this->assertEqual($dummies[0]->dummy, "Test 1");
     $this->assertEqual($dummies[1]->dummy, "Test 2");
 }
示例#3
0
 function test_sti_field_is_saved_and_used_correctly()
 {
     setup_sqlite_test_db();
     # Create a dummy test entries
     $dummy = Test::create(array("dummy" => "TestDummy"));
     $this->assertEqual($dummy->type, NULL);
     $stest = new SuperTest(array("dummy" => "SuperDummy"));
     $stest->save();
     $this->assertEqual(strtolower($stest->type), "supertest");
     $utest = new UltraTest(array("dummy" => "UltraDummy"));
     $utest->save();
     $this->assertEqual(strtolower($utest->type), "ultratest");
     $atest = new AutoTest(array("dummy" => "AutoDummy"));
     $atest->save();
     $this->assertEqual(strtolower($atest->type), "autotest");
     # PHP 5.3 needed to call magic methods statically, so
     # work around by instanciating the class as singleton
     $Test = Singleton::instance("Test");
     # Load and check test entries
     $dummy2 = $Test->find_by_dummy("TestDummy");
     $this->assertEqual($dummy2->type, NULL);
     $this->assertEqual(strtolower(get_class($dummy2)), "test");
     $stest2 = $Test->find_by_dummy("SuperDummy");
     $this->assertEqual(strtolower($stest2->type), "supertest");
     $this->assertEqual(strtolower(get_class($stest2)), "supertest");
     $utest2 = $Test->find_by_dummy("UltraDummy");
     $this->assertEqual(strtolower($utest2->type), "ultratest");
     $this->assertEqual(strtolower(get_class($utest2)), "ultratest");
     $atest2 = $Test->find_by_dummy("AutoDummy");
     $this->assertEqual(strtolower($atest2->type), "autotest");
     $this->assertEqual(strtolower(get_class($atest2)), "autotest");
 }
示例#4
0
 public static function get_instance()
 {
     if (!isset(self::$instance)) {
         self::$instance = new static();
     }
     return self::$instance;
 }
示例#5
0
 public static function getInstance()
 {
     if (!self::$instance instanceof self) {
         self::$instance = new Singleton();
     }
     return self::$instance;
 }
 public static function get_instance()
 {
     if (!self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
示例#7
0
 public static function getInstance()
 {
     if (self::$instance == null) {
         self::$instance = new self();
     }
     return self::$instance;
 }
示例#8
0
 public static function getInstance()
 {
     if (empty(self::$instance)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
示例#9
0
 static function getInstance()
 {
     if (empty(self::$instance)) {
         self::$instance = new Singleton();
     }
     return self::$instance;
 }
示例#10
0
 public static function getInstance()
 {
     if (is_null(self::$instance)) {
         self::$instance = new Singleton();
     }
     return self::$instance;
 }
示例#11
0
 public static function GetInstance()
 {
     if (Singleton::$instance == null) {
         Singleton::$instance = new Singleton();
     }
     return Singleton::$instance;
 }
示例#12
0
 public static function getInstance()
 {
     if (!isset(self::$instance)) {
         $c = __CLASS__;
         self::$instance = new $c();
     }
     return self::$instance;
 }
示例#13
0
 public static function getInstance()
 {
     if (empty($instance)) {
         self::$instance = new Singleton();
         self::$instance->loadValues();
     }
     return self::$instance;
 }
示例#14
0
 static function instance()
 {
     if (!isset(self::$instance)) {
         $c = get_called_class();
         self::$instance = new $c();
     }
     return self::$instance;
 }
示例#15
0
 public static function getInstance()
 {
     if (!self::$instance) {
         self::$instance = new self();
         echo 1;
         return self::$instance;
     }
 }
示例#16
0
 public static function singleton()
 {
     if (!isset(self::$instance)) {
         echo 'Creating new instance.';
         $className = __CLASS__;
         self::$instance = new $className();
     }
     return self::$instance;
 }
示例#17
0
 public static function getInstance()
 {
     if (!isset(self::$instance)) {
         self::$instance = new self();
         echo "<p>First initialization</p>";
     } else {
         echo "<p>initialization</p>";
     }
     return self::$instance;
 }
示例#18
0
 public function connection($dbName)
 {
     if (self::$instance == null) {
         self::$instance = new \mysqli(DBHOST, DBUSER, DBPASSWD, $dbName);
         if (self::$instance->connect_errno > 0) {
             die('Unable to connect to database [' . self::$instance->connect_error . ']');
         }
     }
     return self::$connection;
 }
 function &find()
 {
     # instantiate model singleton for scoping
     $model =& Singleton::instance($this->_source);
     # wrap finder into model scope
     $options = func_get_args();
     $model->with_find_scope($this->_wrap_scope["find"]);
     $result = $this->_source->find($options);
     $model->end_scope();
     return $result;
 }
 public function __construct()
 {
     if (!self::$instance) {
         self::$instance = $this;
         echo 'New Instance<br>';
         return self::$instance;
     } else {
         echo 'Old Instance';
         return self::$instance;
     }
 }
示例#21
0
 public static function getInstance()
 {
     if (self::$instance === null) {
         self::$instance = new self();
         $xmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors/>";
         if (self::$xml === null) {
             self::$xml = new \SimpleXMLElement($xmlstr);
             self::$xml->addChild("error", "500");
             self::$xml->addChild("description", "Internal Server Error");
             self::$stacktraces = self::$xml->addChild("stacktraces");
         }
     }
     return self::$instance;
 }
示例#22
0
 public function testSingleton()
 {
     $singleton = Singleton::instance();
     $singleton2 = Singleton::instance();
     $this->assertTrue($singleton === $singleton2);
 }
示例#23
0
 function has_one($what, $options = array())
 {
     $table = Inflector::tableize($what);
     $assoc_class = Inflector::classify($table);
     $assoc_model = Singleton::instance($assoc_class);
     $default_options = array("class_name" => $assoc_class, "primary_key" => $assoc_model->_primary_key, "foreign_key" => Inflector::underscore($this->_base_class) . "_id", "dependent" => "nullify", "select" => "*", "validate" => true);
     $options = array_merge($default_options, $options);
     $this->_has_one = array_merge($this->_has_one, array($what => $options));
 }
示例#24
0
 function &find($arguments)
 {
     $conditions = array();
     # Be sure to have arguments in an array as needed below
     if (!is_array($arguments)) {
         $arguments = array($arguments);
     }
     # Instantiate model singleton to access scope
     $model =& Singleton::instance();
     $scope = $model->_scope["find"];
     $where = $order = $limit = $offset = NULL;
     $options = array();
     # preset scoped options
     if (isset($scope["order"])) {
         $order = $scope["order"];
     }
     # flatten all arguments if their key is numeric
     # this resolves e.g. array(1, 2, array("conditions" => "foo")) into array(1, 2, "conditions" => "foo")
     while (true) {
         $break = true;
         foreach ($arguments as $key => $value) {
             if (is_numeric($key) && is_array($value)) {
                 unset($arguments[$key]);
                 $arguments = array_merge_recursive($arguments, $value);
                 $break = false;
                 break;
             }
         }
         if ($break) {
             break;
         }
     }
     # parse arguments into query parameters
     foreach ($arguments as $key => $arg) {
         if (is_numeric($key)) {
             switch ($arg) {
                 case "all":
                     $limit = $offset = NULL;
                     $options[] = $arg;
                     continue 2;
                 case "first":
                     $limit = 1;
                     $offset = NULL;
                     $options[] = $arg;
                     continue 2;
                 default:
                     $key = $this->_primary_key;
             }
         }
         switch ($key) {
             case "conditions":
                 # FIXME make this DRY (see also AdoDBRecord_AssociationProxy)
                 if (!is_array($arg)) {
                     $arg = array($arg);
                 }
                 $conditions = array_merge_recursive($conditions, $arg);
                 break;
             case "limit":
             case "offset":
             case "order":
                 ${$key} = $arg;
                 break;
             default:
                 if (is_array($arg)) {
                     $conditions[] = sprintf("{$key} IN (?)", $arg);
                 } else {
                     $conditions[] = array("{$key} = ?", $arg);
                 }
         }
     }
     # parse conditions
     $parsed_conditions = array();
     $parsed_params = array();
     AdoDBRecord_Tools::parse_conditions($conditions, $parsed_conditions, $parsed_params);
     # join scoped conditions
     if (!empty($scope["conditions"])) {
         $parsed_conditions[] = $scope["conditions"];
     }
     # convert parsed options to sql
     if ($order !== NULL) {
         $order = " ORDER BY {$order}";
     }
     if (!empty($parsed_conditions)) {
         $where = sprintf(" WHERE (%s)", join($parsed_conditions, ") AND ("));
         AdoDBRecord_Tools::convert_sql_params($where, $parsed_params);
     }
     $objs = array();
     $conn =& _adodb_conn();
     # FIXME re-add table and column quotes again later
     if ($limit === NULL) {
         $limit = -1;
     }
     if ($offset === NULL) {
         $offset = -1;
     }
     if ($rs =& $conn->SelectLimit("SELECT * FROM {$this->_table_name}{$where}{$order}", $limit, $offset, $parsed_params)) {
         $rows =& $rs->GetRows();
         foreach ($rows as $row) {
             $class = empty($row["type"]) ? get_class($this) : $row["type"];
             $obj = new $class($row);
             $obj->_new_record = false;
             $objs[] = $obj;
         }
         if (in_array("all", $options)) {
             return $objs;
         }
         if (in_array("first", $options)) {
             return $objs[0];
         }
         if (count($objs) == 1) {
             return $objs[0];
         }
         return $objs;
     }
     $objs = NULL;
     return $objs;
 }
示例#25
0
 public static function getInstance()
 {
     if (!self::$instance) {
         self::$instance = new self();
     }
 }
示例#26
0
 public function instance()
 {
     return Singleton::instance('process');
 }
示例#27
0
 function &inflections()
 {
     return Singleton::instance("Inflections");
 }
示例#28
0
 function &registration()
 {
     return Singleton::instance(__CLASS__);
 }
示例#29
0
 public static function getInstance()
 {
     self::$instance !== null ?: (self::$instance = new self());
     return self::$instance;
 }
示例#30
0
            $this->plural("/{$s01d}(?i){$s1rest}\$/", $p01d . $p1rest);
            $this->singular("/{$p01u}(?i){$p1rest}\$/", $s01u . $s1rest);
            $this->singular("/{$p01d}(?i){$p1rest}\$/", $s01d . $s1rest);
        }
    }
    function uncountable($words)
    {
        if (!is_array($words)) {
            $words = array($words);
        }
        foreach ($words as $word) {
            $this->uncountables[] = $word;
        }
    }
}
$inflect =& Singleton::instance("Inflections");
$inflect->plural('/$/', 's');
$inflect->plural('/s$/i', 's');
$inflect->plural('/(ax|test)is$/i', '\\1es');
$inflect->plural('/(octop|vir)us$/i', '\\1i');
$inflect->plural('/(alias|status)$/i', '\\1es');
$inflect->plural('/(bu)s$/i', '\\1ses');
$inflect->plural('/(buffal|tomat)o$/i', '\\1oes');
$inflect->plural('/([ti])um$/i', '\\1a');
$inflect->plural('/sis$/i', 'ses');
$inflect->plural('/(?:([^f])fe|([lr])f)$/i', '\\1\\2ves');
$inflect->plural('/(hive)$/i', '\\1s');
$inflect->plural('/([^aeiouy]|qu)y$/i', '\\1ies');
$inflect->plural('/(x|ch|ss|sh)$/i', '\\1es');
$inflect->plural('/(matr|vert|ind)(?:ix|ex)$/i', '\\1ices');
$inflect->plural('/([m|l])ouse$/i', '\\1ice');