コード例 #1
0
ファイル: Scaffold.php プロジェクト: wouterrr/mangoutils
 public static function build($document, array $post)
 {
     if (is_string($document)) {
         $document = Mango::factory($document);
     }
     foreach ($document->fields() as $name => $data) {
         switch ($data['type']) {
             case 'boolean':
                 $post[$name] = isset($post[$name]);
                 break;
         }
     }
     foreach ($post as $name => $value) {
         if ($field = $document->field($name)) {
             switch ($field['type']) {
                 case 'set':
                     $value = is_string($value) && $value !== '' ? explode(',', $value) : NULL;
                     break;
             }
         } else {
             continue;
         }
         $document->__set($name, $value);
     }
     return $document;
 }
コード例 #2
0
ファイル: Worker.php プロジェクト: wouterrr/mangotask
 public function loop(array $config)
 {
     if ($this->_auto_terminate && memory_get_usage() > $this->_auto_terminate) {
         Kohana::$log->add(Log::WARNING, "Queue autoterminating. Memory usage (:usage bytes) higher than limit (:limit bytes).", array(":usage" => memory_get_usage(), ":limit" => $this->_auto_terminate));
         return FALSE;
     } else {
         if (count($this->_pids) < $this->_max_tasks) {
             try {
                 // find next task
                 $task = Mango::factory('task')->get_next();
             } catch (MongoException $e) {
                 Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e));
                 Kohana::$log->add(Log::ERROR, 'Error loading next task. Exiting');
                 return FALSE;
             }
             if ($task->loaded()) {
                 // Write log to prevent memory issues
                 Kohana::$log->write();
                 // close all database connections before forking
                 foreach (MangoDB::$instances as $instance) {
                     $instance->disconnect();
                 }
                 $pid = pcntl_fork();
                 if ($pid == -1) {
                     // error forking
                     Kohana::$log->add(Log::ERROR, 'Queue. Could not spawn child task process.');
                     Kohana::$log->write();
                     exit;
                 } else {
                     if ($pid) {
                         // parent process
                         $this->_pids[$pid] = time();
                     } else {
                         // child process
                         $task->execute();
                         exit(0);
                     }
                 }
             }
         }
     }
     return TRUE;
 }
コード例 #3
0
ファイル: mangodemo.php プロジェクト: nanodocumet/MangoDemo
 public function action_demo12()
 {
     $this->template->bind('content', $content);
     $content = '';
     // Create blog
     $blog = Mango::factory('blog', array('title' => 'title1', 'text' => 'text1', 'time_written' => time()))->create();
     // Unset a field
     unset($blog->title);
     echo Kohana::debug('Unset title', $blog->changed(true));
     // Save
     $blog->update();
     // Reload blog
     $blog->reload();
     // Check if title is really missing
     echo Kohana::debug('Title should be missing', $blog->as_array());
     // Add 2 comments
     $blog->add(Mango::factory('comment', array('name' => 'Name1', 'comment' => 'Text1', 'time' => time())));
     $blog->add(Mango::factory('comment', array('name' => 'Name2', 'comment' => 'Text2', 'time' => time())));
     // This should use pushAll
     echo Kohana::debug('Adding comments using $pushAll', $blog->changed(TRUE));
     $blog->update();
     $blog->reload();
     $blog->comments[0]->comment = 'New Text';
     echo Kohana::debug('Update using array indices', $blog->changed(TRUE));
     $blog->update();
     $blog->reload();
     echo Kohana::debug('New text in first comment', $blog->as_array());
     // Clean up
     $blog->delete();
 }
コード例 #4
0
ファイル: arrayobject.php プロジェクト: memakeit/MangoDB
 /**
  * Fetch value
  */
 public function offsetGet($index)
 {
     if (!$this->offsetExists($index)) {
         // implicit set ($array[1][2] = 3)
         switch ($this->_type_hint) {
             case 'array':
             case 'set':
             case 'counter':
                 // counter also defaults to array, to support multidimensional counters
                 // (Mango_Array can act as a counter itself aswell, so leaving all options available)
                 $value = array();
                 break;
             case NULL:
                 // implicit set is only possible when we know the array type.
                 throw new Mango_Exception('Set typehint to \'set\', \'array\', \'counter\' or model name (now: :typehint) to support implicit array creation', array(':typehint' => $this->_type_hint ? '\'' . $this->_type_hint . '\'' : 'not set'));
                 break;
             default:
                 $value = Mango::factory($this->_type_hint);
                 break;
         }
         // secretly set value (via parent::offsetSet, so no change is recorded)
         parent::offsetSet($index, $this->load_type($value));
     }
     return parent::offsetGet($index);
 }
コード例 #5
0
ファイル: mangodemo.php プロジェクト: Wouterrr/MangoDemo
 public function action_demo13()
 {
     $user = Mango::factory('user', array('role' => 'viewer', 'email' => '*****@*****.**'))->create();
     $group = Mango::factory('group', array('name' => 'wouter'))->create();
     $user->add($group, 'circle');
     echo Debug::vars($user->changed(TRUE), $group->changed(TRUE));
     $user->update();
     $group->update();
     $group->reload();
     $user->reload();
     foreach ($group->users as $user) {
         echo Debug::vars($user->as_array(FALSE));
     }
     foreach ($user->circles as $circle) {
         echo Debug::vars($circle->as_array(FALSE));
     }
     $group->remove($user);
     echo Debug::vars($user->changed(TRUE), $group->changed(TRUE));
     $user->delete();
     $group->delete();
 }
コード例 #6
0
ファイル: Mango.php プロジェクト: tscms/mangodb
 /**
  * Formats a value into correct a valid value for the specific field
  *
  * @param   string  field name
  * @param   mixed   field value
  * @param   boolean is value clean (from Db)
  * @return  mixed   formatted value
  */
 protected function load_field($name, $value, $clean = FALSE)
 {
     // Load field data
     $field = $this->_fields[$name];
     if (!$clean) {
         // Apply filters
         $value = $this->run_filters($name, $value);
         // Empty string
         if (is_string($value) && $value === '') {
             $value = NULL;
         }
     }
     if ($value !== NULL || $clean === TRUE) {
         switch ($field['type']) {
             case 'MongoId':
                 if ($value !== NULL and !$value instanceof MongoId) {
                     $value = new MongoId($value);
                 }
                 break;
             case 'date':
                 if (!$value instanceof MongoDate) {
                     $value = new MongoDate(is_int($value) ? $value : strtotime($value));
                 }
                 break;
             case 'enum':
                 if ($clean) {
                     $value = isset($field['values'][$value]) ? $value : NULL;
                 } else {
                     $value = ($key = array_search($value, $field['values'])) !== FALSE ? $key : NULL;
                 }
                 break;
             case 'int':
                 if ((double) $value > PHP_INT_MAX) {
                     // This number cannot be represented by a PHP integer, so we convert it to a float
                     $value = (double) $value;
                 } else {
                     $value = (int) $value;
                 }
                 break;
             case 'float':
                 $value = (double) $value;
                 break;
             case 'timestamp':
                 if (!is_int($value)) {
                     $value = ctype_digit($value) ? (int) $value : strtotime($value);
                 }
                 break;
             case 'boolean':
                 $value = (bool) $value;
                 break;
             case 'email':
                 if (!$clean) {
                     $value = strtolower(trim((string) $value));
                 }
                 break;
             case 'string':
                 $value = trim((string) $value);
                 if (!$clean && strlen($value)) {
                     $max_size = Arr::get($field, 'max_size', Mango::MAX_SIZE_STRING);
                     if (UTF8::strlen($value) > $max_size) {
                         $value = UTF8::substr($value, 0, $max_size);
                     }
                     if (Arr::get($field, 'xss_clean') && !empty($value)) {
                         $value = Security::xss_clean($value);
                     }
                 }
                 break;
             case 'has_one':
                 if (is_array($value)) {
                     $value = Mango::factory($field['model'], $value, $clean ? Mango::CLEAN : Mango::CHANGE);
                 }
                 if (!$value instanceof Mango) {
                     $value = NULL;
                 } else {
                     $value->set_parent($this);
                 }
                 break;
             case 'has_many':
                 $value = new Mango_Set($value, $field['model'], Arr::get($field, 'duplicates', FALSE), $clean);
                 foreach ($value as $model) {
                     $model->set_parent($this);
                 }
                 break;
             case 'counter':
                 $value = new Mango_Counter($value);
                 break;
             case 'array':
                 $value = new Mango_Array($value, Arr::get($field, 'type_hint'), $clean);
                 break;
             case 'set':
                 $value = new Mango_Set($value, Arr::get($field, 'type_hint'), Arr::get($field, 'duplicates', TRUE), $clean);
                 break;
             case 'mixed':
                 $value = !is_object($value) ? $value : NULL;
                 break;
         }
         if (!$clean && is_string($value) && $value === '') {
             $value = NULL;
         }
     }
     return $value;
 }
コード例 #7
0
ファイル: iterator.php プロジェクト: memakeit/MangoDB
 /**
  * Iterator: current
  */
 public function current()
 {
     return Mango::factory($this->_model, $this->_cursor->current(), Mango::CLEAN);
 }
コード例 #8
0
ファイル: Mango.php プロジェクト: wouterrr/a1
 /**
  * Loads the user object from database using username
  *
  * @param   string   username
  * @return  object   User Object
  */
 protected function _load_user($username)
 {
     return Mango::factory($this->_config['user_model'], array($this->_config['columns']['username'] => $username))->load();
 }
コード例 #9
0
ファイル: Task.php プロジェクト: wouterrr/mangotask
 /**
  * Finds and activates the next task to be executed
  *
  * @return   Model_Task   task (if not loaded, there is no next task)
  */
 public function get_next()
 {
     $values = $this->db()->command(array('findAndModify' => $this->_collection, 'new' => TRUE, 'sort' => array('created' => 1), 'query' => array('status' => array_search('queued', $this->_fields['status']['values'])), 'update' => array('$inc' => array('try' => 1), '$set' => array('updated' => time(), 'status' => array_search('active', $this->_fields['status']['values'])))));
     return Mango::factory('task', Arr::get($values, 'value', array()), Mango::CLEAN);
 }
コード例 #10
0
ファイル: mango.php プロジェクト: smgladkovskiy/A1
 /**
  * Loading user by name
  *
  * @param string $username
  * @return object / NULL
  */
 public function load_user($username)
 {
     $user = Mango::factory($this->_config['user_model'], array($this->_config['columns']['username'] => $username, 'account_id' => Request::$account->_id))->load();
     // @TODO: do the right thing here
     return NULL;
 }
コード例 #11
0
ファイル: Daemon.php プロジェクト: Wouterrr/mangoQueue
 protected function daemon()
 {
     while (!$this->_sigterm) {
         if (count($this->_pids) < $this->_config['max']) {
             try {
                 // find next task
                 $task = Mango::factory('task')->get_next();
             } catch (MongoException $e) {
                 Kohana::$log->add($this->_config['log']['error'], Kohana_Exception::text($e));
                 Kohana::$log->add($this->_config['log']['error'], 'Error loading next task. Exiting');
                 Kohana::$log->write();
                 $this->clean();
                 exit;
             }
             if ($task->loaded()) {
                 if (!$task->valid()) {
                     // invalid tasks are discarded immediately
                     Kohana::$log->add($this->_config['log']['error'], strtr('Discarded invalid :type task"', array(':type' => $task->type)));
                     $task->delete();
                 } else {
                     // Write log to prevent memory issues
                     Kohana::$log->write();
                     $pid = $this->fork();
                     if ($pid == -1) {
                         Kohana::$log->add($this->_config['log']['error'], 'Queue. Could not spawn child task process.');
                         exit;
                     } elseif ($pid) {
                         // Parent - add the child's PID to the running list
                         $this->_pids[$pid] = time();
                     } else {
                         $task->execute($this->_config);
                         exit;
                     }
                 }
             } else {
                 // queue is empty
                 usleep($this->_config['sleep']);
             }
         } else {
             // daemon is busy
             usleep($this->_config['sleep']);
         }
     }
     // clean up
     $this->clean();
 }
コード例 #12
0
ファイル: daemon.php プロジェクト: praxxis/mangoQueue
 protected function daemon()
 {
     while (!$this->_sigterm) {
         // Find first task that is not being executed
         $task = Mango::factory('task')->load(1, array('_id' => 1), array(), array('e' => array('$exists' => FALSE)));
         if ($task->loaded() && count($this->_pids) < $this->_config['max']) {
             // Task found
             // Update task status
             $task->e = TRUE;
             $task->update();
             // Write log to prevent memory issues
             Kohana::$log->write();
             // Fork process to execute task
             $pid = pcntl_fork();
             if ($pid == -1) {
                 Kohana::$log->add('error', 'Queue. Could not spawn child task process.');
                 exit;
             } elseif ($pid) {
                 // Parent - add the child's PID to the running list
                 $this->_pids[$pid] = time();
             } else {
                 try {
                     // Child - Execute task
                     Request::factory(Route::get($task->route)->uri($task->uri->as_array()))->execute();
                 } catch (Exception $e) {
                     // Task failed - log message
                     Kohana::$log->add('error', strtr('Queue. Task failed - route: :route, uri: :uri, msg: :msg', array(':route' => $task->route, ':uri' => http_build_query($task->uri->as_array()), ':msg' => $e->getMessage())));
                 }
                 // Remove task from queue
                 $task->delete();
                 exit;
             }
         } else {
             // No task in queue - sleep
             usleep($this->_config['sleep']);
         }
     }
     // clean up
     $this->clean();
 }
コード例 #13
0
ファイル: MangoDB.php プロジェクト: wouterrr/mangoutils
 /**
  * Garbage Collector to delete old sessions
  */
 protected function _gc()
 {
     if ($this->_lifetime) {
         $expires = $this->_lifetime;
     } else {
         $expires = Date::MONTH;
     }
     // Delete old sessions
     Mango::factory('session')->db()->remove('sessions', array('last_active' => array('$lt' => time() - $expires)));
 }