/** Authenticate against Jig-based flat-file database; AUTH global array elements: db:<Jig-database> (default:'DB'), table:<table-name>, id:<userID-field>, pw:<password-field> @return mixed @param $id string @param $pw string @public **/ static function jig($id, $pw) { $auth = self::$vars['AUTH']; foreach (array('table', 'id', 'pw') as $param) { if (!isset($auth[$param])) { trigger_error(self::TEXT_AuthSetup); return FALSE; } } if (!isset($auth['db'])) { $auth['db'] = self::ref('DB'); } $jig = new Jig($auth['table'], $auth['db']); $jig->load(array(self::ref('AUTH.id') => $id, self::ref('AUTH.pw') => $pw)); return $jig->dry() ? FALSE : $jig; }
/** Custom session handler @param $table string @public **/ function session($table = 'sessions') { session_set_save_handler(function ($path, $name) { register_shutdown_function('session_commit'); return TRUE; }, function () { return TRUE; }, function ($id) use($table) { $jig = new Jig($table); $jig->load(array('id' => $id)); return $jig->dry() ? FALSE : $jig->data; }, function ($id, $data) use($table) { $jig = new Jig($table); $jig->load(array('id' => $id)); $jig->id = $id; $jig->data = $data; $jig->stamp = time(); $jig->save(); return TRUE; }, function ($id) use($table) { $jig = new Jig($table); $jig->erase(array('id' => $id)); return TRUE; }, function ($max) use($table) { $jig = new Jig($table); $jig->erase(array('_PHP_' => array('stamp' => function ($stamp) use($max) { return $stamp + $max < time(); }))); return TRUE; }); }
function jig() { $this->set('title', 'Jig Mapper'); $this->expect(is_null($this->get('ERROR')), 'No errors expected at this point', 'ERROR variable is set: ' . $this->get('ERROR.text')); @unlink('db/products'); $this->set('DB', new FileDB('db/', FileDB::FORMAT_GZip)); $product = new Jig('products'); $this->expect(is_object($product), 'Jig created', 'Unable to instantiate Jig'); unset($product); $product = Jig::instance('products'); $this->expect(is_a($product, 'Jig'), 'Jig instance created', 'Unable to instantiate Jig'); unset($product); $product = new Jig('products'); $this->expect(is_object($product), 'Jig created (case-insensitive)', 'Unable to instantiate Jig (case-insensitive)'); $this->expect($product->dry(), 'Jig in dry state', 'Jig is in hydrated state'); $product->item = 111; $product->description = 'Coca Cola'; $product->quantity = 3; $this->expect(!$product->dry(), 'Jig hydrated manually', 'Jig should be hydrated by now'); $product->save(); $this->expect(!$product->dry(), 'Jig expected to remain hydrated', 'Jig should be dry'); $this->expect($product->_id, 'Last insert ID available: ' . $product->_id, 'No last insert ID available'); $product->load(array('item' => 111)); $this->expect($product->item == 111 && $product->description == 'Coca Cola' && $product->quantity == 3, 'Auto-hydration succeeded', 'Auto-hydration failed'); $result = $product->findOne(array('item' => 111)); $this->expect($result->item == 111 && $result->description == 'Coca Cola' && $result->quantity == 3, 'findOne returned the correct record', 'findOne return value is incorrect'); $result = $product->find(array('item' => 111)); $this->expect(get_class($result[0]) == 'Jig' && $result[0]->item == 111 && $result[0]->description == 'Coca Cola' && $result[0]->quantity == 3, 'find returned an array of Jig objects', 'find return type is incorrect'); $product->quantity++; $product->save(); $product->load(array('item' => 111)); $this->expect($product->item == 111 && $product->description == 'Coca Cola' && $product->quantity == 4, 'Jig saved - database update succeeded', 'Database update failed'); $product->copyTo('POST'); $this->expect($this->get('POST.item') == 111 && $this->get('POST.description') == 'Coca Cola' && $this->get('POST.quantity') == 4, 'Jig properties copied to framework variable', 'Unable to copy Jig properties to framework variable'); $_POST['description'] = 'Pepsi'; $product->copyFrom('POST'); $this->expect($product->item == 111 && $product->description == 'Pepsi' && $product->quantity == 4, 'Jig properties populated by framework variable', 'Unable to fill Jig properties with contents of framework variable'); $this->set('POST.item', 999); $this->set('POST.description', 'Pepsi'); $this->set('POST.quantity', 11); $product->copyFrom('POST', 'item|quantity'); $this->expect($product->item == 999 && $product->description == 'Pepsi' && $product->quantity == 11, 'Jig properties populated by selected fields in framework variable', 'Unable to fill Jig properties with contents of framework variable'); $product->reset(); $this->expect($product->dry(), 'Jig reset completed', 'Jig should be dry'); $product->item = 222; $product->description = 'Mobile Phone'; $product->quantity = 9; $this->expect(!$product->dry(), 'Jig rehydrated manually', 'Jig should hydrated by now'); $product->save(); $this->expect(!$product->dry(), 'Jig expected to remain hydrated', 'Jig should not be dry'); $product->load(array('item' => 111)); $this->expect($product->item == 111 && $product->description == 'Coca Cola' && $product->quantity == 4, 'First record still there', 'First record is missing'); $product->load(array('item' => 222)); $this->expect($product->item == 222 && $product->description == 'Mobile Phone' && $product->quantity == 9, 'Second record found', 'Second record is missing'); $product->load(array('item' => 111)); $product->erase(); $product->load(array('item' => 111)); $this->expect($product->dry(), 'First record deleted', 'First record still exists'); $product->load(array('item' => 222)); $this->expect($product->item == 222 && $product->description == 'Mobile Phone' && $product->quantity == 9, 'Second record still there', 'Second record is missing'); $product->reset(); $product->item = 111; $product->description = 'Lots of dough'; $product->quantity = 666; $product->save(); $product->load(array('quantity' => array('gt' => 0))); $this->expect($product->found() == 2, 'New record added - multirecord criteria specified for loading', 'New record was not added'); $product->skip(1); $this->expect(!$product->dry(), 'One more record expected to be retrieved', 'Jig is dry'); $this->expect($product->item == 111 && $product->description == 'Lots of dough' && $product->quantity == 666, 'Backward navigation', 'Backward navigation failed'); $product->skip(-1); $this->expect($product->item == 222 && $product->description == 'Mobile Phone' && $product->quantity == 9, 'Forward navigation', 'Forward navigation failed'); $product->skip(-1); $this->expect($product->dry(), 'Jig is dry when navigating before the start of the record set', 'Navigation failure'); $this->set('QUIET', TRUE); $product->skip(-1); $this->expect(!is_null($this->get('ERROR')), 'Navigating past dry state triggers an error', 'Navigation error handling issue'); $this->set('QUIET', FALSE); $this->clear('ERROR'); $product->load(array('quantity' => array('gt' => 0))); $product->skip(2); $this->expect($product->dry(), 'Jig is dry when navigating beyond the end of the record set', 'Navigation failure'); $this->set('QUIET', TRUE); $product->skip(); $this->expect(!is_null($this->get('ERROR')), 'Navigating past dry state triggers an error', 'Navigation error handling issue'); $this->set('QUIET', FALSE); $this->clear('ERROR'); $product->load(array('item' => 111)); $product->description = 'quoted "string"'; $product->save(); $result = $product->findOne(array('item' => 111)); $this->expect($result->description == 'quoted "string"', 'Double-quoted strings are left untouched', 'Double-quoted strings altered'); $this->get('DB')->convert(FileDB::FORMAT_Serialized); echo $this->render('basic/results.htm'); }