configureFacadeWithToolbox() public static method

Configures the facade, want to have a new Writer? A new Object Database or a new Adapter and you want it on-the-fly? Use this method to hot-swap your facade with a new toolbox.
public static configureFacadeWithToolbox ( ToolBox $tb ) : ToolBox
$tb ToolBox toolbox to configure facade with
return ToolBox
Example #1
0
 /**
  * Test Full fluid UUID support.
  *
  */
 public function testFullSupport()
 {
     //Rewire objects to support UUIDs.
     $oldToolBox = R::getToolBox();
     $oldAdapter = $oldToolBox->getDatabaseAdapter();
     $uuidWriter = new \UUIDWriterPostgres($oldAdapter);
     $newRedBean = new OODB($uuidWriter);
     $newToolBox = new ToolBox($newRedBean, $oldAdapter, $uuidWriter);
     R::configureFacadeWithToolbox($newToolBox);
     list($mansion, $rooms, $ghosts, $key) = R::dispenseAll('mansion,room*3,ghost*4,key');
     $mansion->name = 'Haunted Mansion';
     $mansion->xownRoomList = $rooms;
     $rooms[0]->name = 'Green Room';
     $rooms[1]->name = 'Red Room';
     $rooms[2]->name = 'Blue Room';
     $ghosts[0]->name = 'zero';
     $ghosts[1]->name = 'one';
     $ghosts[2]->name = 'two';
     $ghosts[3]->name = 'three';
     $rooms[0]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[1]);
     $rooms[1]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[2]);
     $rooms[2]->noLoad()->sharedGhostList = array($ghosts[1], $ghosts[3], $ghosts[2]);
     $rooms[2]->xownKey = array($key);
     //Can we store a bean hierachy with UUIDs?
     R::debug(1);
     $id = R::store($mansion);
     //exit;
     asrt(is_string($id), TRUE);
     asrt(strlen($id), 36);
     $haunted = R::load('mansion', $id);
     asrt($haunted->name, 'Haunted Mansion');
     asrt(is_string($haunted->id), TRUE);
     asrt(strlen($haunted->id), 36);
     asrt(is_array($haunted->xownRoomList), TRUE);
     asrt(count($haunted->ownRoom), 3);
     $rooms = $haunted->xownRoomList;
     //Do some counting...
     $greenRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Green Room') {
             $greenRoom = $room;
             break;
         }
     }
     asrt(!is_null($greenRoom), TRUE);
     asrt(is_array($greenRoom->with(' ORDER BY id ')->sharedGhostList), TRUE);
     asrt(count($greenRoom->sharedGhostList), 2);
     $names = array();
     foreach ($greenRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,zero');
     $rooms = $haunted->xownRoomList;
     $blueRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Blue Room') {
             $blueRoom = $room;
             break;
         }
     }
     asrt(!is_null($blueRoom), TRUE);
     asrt(is_array($blueRoom->sharedGhostList), TRUE);
     asrt(count($blueRoom->sharedGhostList), 3);
     $names = array();
     foreach ($blueRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,three,two');
     $rooms = $haunted->xownRoomList;
     $redRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Red Room') {
             $redRoom = $room;
             break;
         }
     }
     $names = array();
     foreach ($redRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'two,zero');
     asrt(!is_null($redRoom), TRUE);
     asrt(is_array($redRoom->sharedGhostList), TRUE);
     asrt(count($redRoom->sharedGhostList), 2);
     //Can we repaint a room?
     $redRoom->name = 'Yellow Room';
     $id = R::store($redRoom);
     $yellowRoom = R::load('room', $id);
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 2);
     //Can we throw one ghost out?
     array_pop($yellowRoom->sharedGhost);
     R::store($yellowRoom);
     $yellowRoom = $yellowRoom->fresh();
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 1);
     //can we remove one of the rooms?
     asrt(R::count('key'), 1);
     $list = $mansion->withCondition(' "name" = ? ', array('Blue Room'))->xownRoomList;
     $room = reset($list);
     unset($mansion->xownRoomList[$room->id]);
     R::store($mansion);
     asrt(R::count('room'), 2);
     //and what about its dependent beans?
     asrt(R::count('key'), 0);
     asrt(R::count('ghost_room'), 3);
     //and can we find ghosts?
     $ghosts = R::find('ghost');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id LIMIT 2');
     asrt(count($ghosts), 2);
     $ghostZero = R::findOne('ghost', ' "name" = ? ', array('zero'));
     asrt($ghostZero instanceof OODBBean, TRUE);
     //can we create link properties on existing tables?
     $blackRoom = R::dispense('room');
     $blackRoom->name = 'Black Room';
     $ghostZero->link('ghost_room', array('mood' => 'grumpy'))->room = $blackRoom;
     R::store($ghostZero);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->sharedRoomList;
     asrt(count($list), 3);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->withCondition(' ghost_room.mood = ? ', array('grumpy'))->sharedRoomList;
     asrt(count($list), 1);
     //can we load a batch?
     $ids = R::getCol('SELECT id FROM ghost');
     $ghosts = R::batch('ghost', $ids);
     asrt(count($ghosts), 4);
     //can we do an aggregation?
     $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
     asrt(count($ghosts), 2);
     //can we duplicate the mansion?
     asrt(R::count('mansion'), 1);
     asrt(R::count('room'), 3);
     asrt(R::count('ghost'), 4);
     $copy = R::dup($mansion);
     R::store($copy);
     asrt(R::count('mansion'), 2);
     asrt(R::count('room'), 5);
     //black room does not belong to mansion 1
     asrt(R::count('ghost'), 4);
     //can we do some counting using the list?
     asrt($copy->countOwn('room'), 2);
     $rooms = $copy->withCondition(' "name" = ? ', array('Green Room'))->xownRoomList;
     $room = reset($rooms);
     asrt($room->countShared('ghost'), 2);
     //Finally restore old toolbox
     R::configureFacadeWithToolbox($oldToolBox);
 }