示例#1
0
 /**
  * Executa as configurações iniciais e prepara o a entidade responsáveç
  * da biblioteca escolhida para ORM.
  */
 public function init()
 {
     $config = Config::getInstance();
     $paths = [SYS_ROOT . 'App' . DS . 'Models' . DS];
     $dev_mode = $config->get('database.debug');
     $conn_params = $this->loadConfiguration();
     $doctrine_config = Setup::createAnnotationMetadataConfiguration($paths, $dev_mode);
     if ($config->get('cache.cache')) {
         try {
             $cache = Cache::getInstance();
             if ($cache instanceof Cache) {
                 $doctrine_config->setResultCacheImpl($cache->getDriver());
             }
         } catch (\Exception $e) {
             $error = new Error();
             $error->log($e);
         }
     }
     $proxy_dir = SYS_ROOT . 'App' . DS . 'Models' . DS . 'Proxies';
     if (!is_dir($proxy_dir)) {
         if (mkdir($proxy_dir)) {
             $doctrine_config->setProxyDir($proxy_dir);
         }
     }
     $prefix = $config->get('database.connection.table_prefix');
     if ($prefix != '') {
         $evm = new EventManager();
         $table_prefix = new DoctrineTablePrefix($prefix);
         $evm->addEventListener(Events::loadClassMetadata, $table_prefix);
         $this->entityManager = EntityManager::create($conn_params, $doctrine_config, $evm);
     } else {
         $this->entityManager = EntityManager::create($conn_params, $doctrine_config);
     }
 }
示例#2
0
 public function init()
 {
     $connData = $this->loadConfiguration();
     try {
         $pdo = new PDO('mysql:host=' . $connData['host'] . ';dbname=' . $connData['dbname'], $connData['user'], $connData['password']);
         $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
         $pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES UTF8');
         $this->conn = $pdo;
     } catch (PDOException $e) {
         $error = new Error();
         $error->log($e);
     }
 }
示例#3
0
 public function render($template, $params)
 {
     $template_path = $this->templates_path . $template . '.dolly';
     if (is_file($template_path)) {
         $template = file_get_contents($template_path);
         foreach ($params as $key => $value) {
             $template = str_replace('{{' . $key . '}}', $value, $template);
         }
         $template = '<?php' . PHP_EOL . PHP_EOL . $template;
         return $template;
     } else {
         Error::log(new \Exception('Arquivo de template nao encontrado.'));
         return false;
     }
 }
示例#4
0
 private function loadWorkers()
 {
     $path = SYS_ROOT . 'App' . DS . 'Workers' . DS;
     if (!is_dir($path)) {
         throw new \Exception("A pasta App\\Workers nao existe.");
     }
     $class_workers = $this->loadAppWorkers($path);
     foreach ($class_workers as $worker) {
         try {
             $reflection = new \ReflectionClass($worker);
         } catch (\ReflectionException $e) {
             Error::log($e);
             return;
         }
         $worker = $reflection->newInstance();
         $worker->configure();
         $this->workers[] = $this->getWorkerConfig($worker);
     }
 }
示例#5
0
 public function addGroup($config, $callback)
 {
     $router = $this->getSubRouter();
     if (is_callable($callback)) {
         $callback($router);
     } else {
         $e = new Error();
         $e->log(new \Exception('Segundo parâmetro não é uma função do tipo callable'));
     }
     $sub_collection = $router->getCollection();
     if (isset($config['defaults'])) {
         $sub_collection->addDefaults($config['defaults']);
     }
     if (isset($config['mask'])) {
         $sub_collection->addRequirements($config['mask']);
     }
     if (isset($config['options'])) {
         $sub_collection->addOptions($config['options']);
     }
     if (isset($config['host'])) {
         $sub_collection->setHost($config['host']);
     }
     if (isset($config['schemes'])) {
         $sub_collection->setSchemes($config['schemes']);
     }
     if (isset($config['methods'])) {
         $sub_collection->setMethods($config['methods']);
     }
     $this->collection->addCollection($sub_collection);
 }
示例#6
0
 /**
  * Paginador padrão, efetua a busca com base nos filtros recebidos e retorna componente de paginação.
  *
  * @param int   $page
  * @param array $filters
  *
  * @return Paginator
  */
 public function paginate($page = 1, $filters = null)
 {
     $offset = $page == 1 ? 0 : $page * $this->per_page;
     $qb = $this->manager->createQueryBuilder();
     $qb->select('a')->from(get_class($this->model), 'a');
     if (is_array($filters)) {
         foreach ($filters as $field => $value) {
             if ($value !== null) {
                 $qb->where("a.{$field} = :{$field}")->setParameter($field, $value);
             } else {
                 $qb->where("a.{$field} IS NULL");
             }
         }
     }
     $query = $qb->getQuery();
     unset($qb);
     $query->setFirstResult($offset);
     $query->setMaxResults($this->per_page);
     try {
         $paginator = new Paginator($query);
     } catch (\Exception $e) {
         Error::log($e);
         return false;
     }
     $paginator = new \Anna\Paginator($paginator, $this->per_page, $page);
     return $paginator;
 }