Exemplo n.º 1
0
 protected static function printer($return_html)
 {
     $exception_output = '';
     $main_output = '';
     $failing_methods_output = '';
     if ($return_html) {
         $main_output = '<table class="unit_tests"><tr class="header"><td>Class Name</td><td>Passed</td><td>Failed</td><td>Exceptions</td></tr>';
         foreach (self::$classes as $class_name) {
             $main_output .= '<tr class="' . (self::$fails[$class_name] > 0 || count(self::$exceptions[$class_name]) || 0 ? 'fail' : 'pass') . '"><td class="class_name">' . $class_name . '</td><td>' . self::$passes[$class_name] . '</td><td>' . self::$fails[$class_name] . '</td><td>' . count(self::$exceptions[$class_name]) . '</td></tr>';
             foreach (self::$exceptions[$class_name] as $method_name => $exception) {
                 $exception_output .= '<h2>' . $class_name . '::' . $method_name . ' threw this exception:</h2><hr/>' . PicoraSupport::exceptionHandler($exception, true) . '<hr/>';
             }
         }
         $main_output .= '</table>';
         foreach (self::$failing_methods as $class_name => $fail_info) {
             foreach ($fail_info as $method_name => $fails) {
                 foreach ($fails as $item) {
                     $failing_methods_output .= '<li>' . $class_name . '::' . $method_name . ' ' . $item['method'] . ' failed on line ' . $item['line'] . ' in file ' . $item['file'] . '</li>';
                 }
             }
         }
         if ($failing_methods_output != '') {
             $failing_methods_output = '<ul>' . $failing_methods_output . '</ul>';
         }
     } else {
         $main_output = 'PicoraTest::run()' . chr(10);
         foreach (self::$classes as $class_name) {
             $main_output .= chr(9) . $class_name . ' ' . self::$fails[$class_name] . ' passed, ' . self::$fails[$class_name] . ' failed, ' . count(self::$exceptions[$class_name]) . ' exceptions.' . chr(10);
             foreach (self::$exceptions[$class_name] as $method_name => $exception) {
                 $exception_output .= $class_name . '::' . $method_name . ' threw this exception:' . chr(10) . picora_exception_handler($exception, true, false) . chr(10) . chr(10);
             }
             foreach (self::$failing_methods[$class_name] as $method_name => $fails) {
                 foreach ($fails as $info) {
                     $failing_methods_output .= ' - ' . $class_name . '::' . $method_name . ' ' . $info['method'] . ' failed on line ' . $info['line'] . ' in file ' . $info['file'] . chr(10);
                 }
             }
         }
     }
     return $main_output . $failing_methods_output . $exception_output;
 }
Exemplo n.º 2
0
 protected static function generateResponse($response)
 {
     if ($response === null) {
         $method_name = strtolower(preg_replace('/([a-z])([A-Z])/e', "'\\1_'.strtolower('\\2')", self::$status['current_method']));
         $controller_name = strtolower(preg_replace('/([a-z])([A-Z])/e', "'\\1_'.strtolower('\\2')", self::$status['current_controller']));
         $try_one = self::getDefaultViewDirectory() . $controller_name . '/' . $method_name . '.php';
         $try_two = self::getDefaultViewDirectory() . PicoraSupport::pluralize($controller_name) . '/' . $method_name . '.php';
         if (file_exists($try_one)) {
             $response = PicoraController::render($try_one);
         } elseif (file_exists($try_two)) {
             $response = PicoraController::render($try_two);
         } else {
             throw new Exception(self::$status['current_controller'] . 'Controller->' . self::$status['current_method'] . '() returned null, and no matching view was found.');
         }
     }
     return self::getRequestMethod() == 'ajax' ? $response : call_user_func(self::$layout_handler, $response instanceof PicoraView ? $response->display() : $response, self::getCurrentController());
 }
Exemplo n.º 3
0
 public function __toString()
 {
     try {
         return $this->display();
     } catch (Exception $e) {
         PicoraSupport::exceptionHandler($e);
         exit;
     }
 }
Exemplo n.º 4
0
 public function __call($method, $arguments)
 {
     foreach (self::getRelationshipList($this->__class_name__) as $relationship) {
         $relationship_type = $relationship[0];
         $related_class_name = $relationship[1];
         $foreign_key = isset($relationship[2]) ? $relationship[2] : PicoraSupport::singularize($related_class_name) . '_id';
         switch ($relationship_type) {
             case 'has_one':
                 switch ($method) {
                     case 'get' . $related_class_name:
                         return self::find($related_class_name, $this->{$foreign_key});
                     case 'build' . $related_class_name:
                         return self::build($related_class_name, isset($arguments[0]) ? $arguments[0] : array());
                     case 'create' . $related_class_name:
                         $record = self::create($related_class_name, isset($arguments[0]) ? $arguments[0] : array());
                         if ($this->id) {
                             $this->updateAttribute($foreign_key, $record->id);
                         }
                         return $record;
                 }
                 break;
             case 'has_many':
                 switch ($method) {
                     case 'delete' . $related_class_name:
                         $record = self::find($related_class_name, $arguments[0] instanceof PicoraActiveRecord ? $arguments[0]->{self::primaryKeyNameFromClassName(get_class($arguments[0]))} : $arguments[0]);
                         return !$record ? false : (bool) $record->delete();
                     case 'get' . $related_class_name . 'List':
                     case 'get' . $related_class_name . 'Count':
                         $params = isset($arguments[0]) ? $arguments[0] : array();
                         $params['where'] = (isset($params['where']) ? PicoraSupport::formatPropertyString($params['where'], $this) . ' AND ' : '') . $foreign_key . ' = ' . self::escape($this->{self::primaryKeyNameFromClassName($this->__class_name__)});
                         if ('get' . $related_class_name . 'Count' == $method) {
                             return self::count($related_class_name, array('where' => $params['where']));
                         }
                         if (isset($relationship['order']) && !isset($params['order'])) {
                             $params['order'] = $relationship['order'];
                         }
                         $list = self::findAll($related_class_name, $params);
                         if ('get' . $related_class_name . 'List' == $method) {
                             return $list;
                         }
                         foreach ($list as $item) {
                             if (!isset($arguments[0]) || isset($arguments[0]) && $arguments[0] == $item->{self::primaryKeyNameFromClassName(get_class($item))}) {
                                 $item->delete();
                             }
                         }
                         break;
                     case 'create' . $related_class_name:
                         return self::create($related_class_name, array_merge(isset($arguments[0]) ? $arguments[0] : array(), array($foreign_key => $this->{self::primaryKeyNameFromClassName($this->__class_name__)})));
                     case 'build' . $related_class_name:
                         return self::build($related_class_name, array_merge(isset($arguments[0]) ? $arguments[0] : array(), array($foreign_key => $this->{self::primaryKeyNameFromClassName($this->__class_name__)})));
                 }
                 break;
             case 'belongs_to':
                 switch ($method) {
                     case 'get' . $related_class_name:
                         return self::find($related_class_name, $this->{$foreign_key});
                     case 'build' . $related_class_name == $method:
                     case 'create' . $related_class_name == $method:
                         $record = self::build($related_class_name, isset($arguments[0]) ? $arguments[0] : array());
                         if (isset($relationship['counter'])) {
                             $record->{$relationship['counter']} = 1;
                         }
                         if ('build' . $related_class_name == $method) {
                             return $record;
                         }
                         if ($record->save() && $this->{self::primaryKeyNameFromClassName($this->__class_name__)}) {
                             $this->updateAttribute($foreign_key, $record->{self::primaryKeyNameFromClassName(get_class($record))});
                         }
                         return $record;
                 }
                 break;
             case 'has_and_belongs_to_many':
                 $source_class_name = $this->__class_name__;
                 $source_table_name = self::tableNameFromClassName($this->__class_name__);
                 $target_class_name = $related_class_name;
                 $target_table_name = self::tableNameFromClassName($related_class_name);
                 $link_class_name = $relationship[2];
                 $link_table_name = self::tableNameFromClassName($relationship[2]);
                 $source_key = $relationship[3];
                 $target_key = $relationship[4];
                 $params = isset($arguments[0]) ? $arguments[0] : array();
                 switch ($method) {
                     case 'set' . $related_class_name . 'List':
                         $links = array();
                         self::executeQuery('DELETE FROM ' . $link_table_name . ' WHERE ' . $source_key . ' = ' . self::escape($this->id));
                         foreach (isset($arguments[0]) ? $arguments[0] : array() as $id) {
                             $links[] = self::create($link_class_name, array($source_key => $this->{self::primaryKeyNameFromClassName($this->__class_name__)}, $target_key => $id instanceof PicoraActiveRecord ? $id->{self::primaryKeyNameFromClassName(get_class($id))} : $id));
                         }
                         return $links;
                     case 'add' . $related_class_name:
                         return self::create($link_class_name, array($source_key => $this->{self::primaryKeyNameFromClassName($this->__class_name__)}, $target_key => $arguments[0] instanceof PicoraActiveRecord ? $arguments[0]->{self::primaryKeyNameFromClassName(get_class($arguments[0]))} : $arguments[0]));
                     case 'remove' . $related_class_name:
                         self::executeQuery('DELETE FROM ' . $link_table_name . ' WHERE ' . $source_key . ' = ' . self::escape($this->{self::primaryKeyNameFromClassName($this->__class_name__)}) . ' AND ' . $target_key . ' = ' . self::escape($arguments[0] instanceof PicoraActiveRecord ? $arguments[0]->{self::primaryKeyNameFromClassName(get_class($argument[0]))} : $arguments[0]));
                         break;
                     case 'has' . $related_class_name:
                         return (bool) self::find($link_class_name, array('where' => array($source_key => $this->id, $target_key => $arguments[0] instanceof PicoraActiveRecord ? $arguments[0]->{self::primaryKeyNameFromClassName(get_class($arguments[0]))} : $arguments[0])));
                     case 'get' . $related_class_name . 'List':
                     case 'get' . $related_class_name . 'Count':
                         $params['tables'] = $link_table_name;
                         $params['join'] = 'LEFT JOIN ' . $target_table_name . ' ON (' . $link_table_name . '.' . $target_key . ' = ' . $target_table_name . '.' . self::primaryKeyNameFromClassName($target_class_name) . ')';
                         $condition = $link_table_name . '.' . $source_key . ' = ' . $this->{self::primaryKeyNameFromClassName($this->__class_name__)};
                         $params['where'] = (isset($params['where']) ? $params['where'] . ' AND ' : '') . (isset($relationship['where']) ? $relationship['where'] . ' AND ' . $condition : $condition);
                         if (isset($relationship['order']) && !isset($params['order'])) {
                             $params['order'] = $relationship['order'];
                         }
                         if ('get' . $related_class_name . 'Count' == $method) {
                             return self::count($target_class_name, array('where' => $params['where'], 'join' => $params['join']));
                         }
                         $instance_list = self::findAll($target_class_name, $params);
                         foreach ($instance_list as $instance) {
                             unset($instance->{$target_key}, $instance->{$source_key});
                         }
                         if ('get' . $related_class_name . 'List' == $method) {
                             return $instance_list;
                         }
                         break;
                 }
                 break;
         }
     }
     foreach (PicoraEvent::getObserverList('PicoraActiveRecord.call') as $callback) {
         $response = call_user_func($callback, $this, $method, $arguments);
         if (!is_null($response)) {
             return $response;
         }
     }
     throw new Exception('Method "' . $method . '" of "' . $this->__class_name__ . '" does not exist.');
 }