public function run()
 {
     for ($i = 0; $i < 5; $i++) {
         $c = new \App\Classes();
         $c->name = 'Class' . $i;
         $c->start_date = time();
         $c->end_date = time();
         $c->save();
     }
 }
Esempio n. 2
0
 /**
  * The constructor
  * @throws \Exception
  * @param array $config
  */
 function __construct($config = [])
 {
     if (self::$instance === null) {
         if (version_compare(phpversion(), '5.6.0', '<')) {
             ini_set('default_charset', 'UTF-8');
             ini_set('mbstring.internal_encoding', 'UTF-8');
         }
         ini_set('mbstring.func_overload', 7);
         ini_set("pcre.backtrack_limit", 100000000);
         ini_set("pcre.recursion_limit", 100000000);
         self::$instance =& $this;
     } else {
         throw new \Exception('App already constructed');
     }
     $this->config = new \App\Config($config);
     if ($this->config->handleErrors) {
         // @codeCoverageIgnoreStart
         error_reporting(E_ALL | E_STRICT);
         ini_set('display_errors', 0);
         ini_set('display_startup_errors', 0);
         $handleError = function ($message, $file, $line, $trace) {
             $data = "Error:";
             $data .= "\nMessage: " . $message;
             $data .= "\nFile: " . $file;
             $data .= "\nLine: " . $line;
             $data .= "\nTrace: " . $trace;
             $data .= "\nGET: " . print_r($_GET, true);
             $data .= "\nPOST: " . print_r($_POST, true);
             $data .= "\nSERVER: " . print_r($_SERVER, true);
             if ($this->config->logErrors && strlen($this->config->logsDir) > 0 && strlen($this->config->errorLogFilename) > 0) {
                 try {
                     $this->log->write($this->config->errorLogFilename, $data);
                 } catch (\Exception $e) {
                 }
             }
             if ($this->config->displayErrors) {
                 ob_clean();
                 $response = new \App\Response\TemporaryUnavailable($data);
                 $response->disableHooks = true;
             } else {
                 $response = new \App\Response\TemporaryUnavailable();
             }
             $this->respond($response);
         };
         set_exception_handler(function ($exception) use($handleError) {
             $handleError($exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString());
         });
         register_shutdown_function(function () use($handleError) {
             $errorData = error_get_last();
             if (is_array($errorData)) {
                 $messageParts = explode(' in ' . $errorData['file'] . ':' . $errorData['line'], $errorData['message'], 2);
                 $handleError(trim($messageParts[0]), $errorData['file'], $errorData['line'], isset($messageParts[1]) ? trim(str_replace('Stack trace:', '', $messageParts[1])) : '');
             }
         });
         set_error_handler(function ($errorNumber, $errorMessage, $errorFile, $errorLine) {
             throw new \ErrorException($errorMessage, 0, $errorNumber, $errorFile, $errorLine);
         }, E_ALL | E_STRICT);
         // @codeCoverageIgnoreEnd
     }
     spl_autoload_register(function ($class) {
         $this->classes->load($class);
     });
     $this->request = new \App\Request();
     if (isset($_SERVER)) {
         if (isset($_SERVER['REQUEST_METHOD'])) {
             $this->request->method = $_SERVER['REQUEST_METHOD'];
         }
         $path = isset($_SERVER['REQUEST_URI']) && strlen($_SERVER['REQUEST_URI']) > 0 ? urldecode($_SERVER['REQUEST_URI']) : '/';
         $position = strpos($path, '?');
         if ($position !== false) {
             $this->request->query = new \App\Request\Query(substr($path, $position + 1));
             $path = substr($path, 0, $position);
         }
         $basePath = '';
         if (isset($_SERVER['SCRIPT_NAME'])) {
             $scriptName = $_SERVER['SCRIPT_NAME'];
             if (strpos($path, $scriptName) === 0) {
                 $basePath = $scriptName;
                 $path = substr($path, strlen($scriptName));
             } else {
                 $pathInfo = pathinfo($_SERVER['SCRIPT_NAME']);
                 $dirName = $pathInfo['dirname'];
                 if ($dirName === DIRECTORY_SEPARATOR) {
                     $basePath = '';
                     $path = $path;
                 } else {
                     $basePath = $dirName;
                     $path = substr($path, strlen($dirName));
                 }
             }
         }
         $scheme = isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https' || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' || isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) && $_SERVER['HTTP_X_FORWARDED_PROTOCOL'] === 'https' || isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
         $host = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'unknownhost';
         $this->request->path = new \App\Request\Path(isset($path[0]) ? $path : '/');
         $this->request->base = $scheme . '://' . $host . $basePath;
     }
     $this->routes = new \App\Routes();
     $this->log = new \App\Log();
     $this->components = new \App\Components();
     $this->addons = new \App\Addons();
     $this->hooks = new \App\Hooks();
     $this->assets = new \App\Assets();
     $this->data = new \App\Data();
     $this->cache = new \App\Cache();
     $this->classes = new \App\Classes();
 }