Esempio n. 1
0
 function __construct($module = false, $controllerName = false, $renderer = false)
 {
     $this->setView($module, $controllerName, $renderer);
     self::$_logger = LoggerFactory::getInstance(get_class());
     if (!$this->_model) {
         $this->_model = new Model();
     }
     $this->_currentController = $controllerName;
     $this->_currentModule = $module;
     $this->_renderer = $renderer;
     $this->_modelName = StringUtilHelper::underscore($controllerName) . 's';
 }
Esempio n. 2
0
 public static function saveCache($dir, $configName, $data, $serialize = false)
 {
     if (!is_dir($dir)) {
         mkdir($dir, 0777);
     }
     $cacheFile = $dir . $configName;
     $f = fopen($cacheFile, 'w');
     $cacheString = '';
     //echo 'saving cache';
     if ($serialize == false) {
         $cacheString = StringUtilHelper::arrayToString($data);
         $cacheString = '<?php $data = ' . $cacheString . '; ?>';
     } else {
         $cacheString = serialize($data);
     }
     //echo $cacheString;
     fputs($f, $cacheString);
     fclose($f);
 }
Esempio n. 3
0
 public function getTableInfo()
 {
     try {
         $entityName = $this->{FrameworkConstants::ENTITY_NAME};
         $entityName = strtolower(StringUtilHelper::underscore($entityName));
         // pluralize it
         $lastCharacter = substr($entityName, strlen($entityName) - 1, 1);
         if (strcmp($lastCharacter, 's') != 0 && $this->{FrameworkConstants::PLURALIZE}) {
             $entityName .= 's';
         } else {
             // check last 2 characters and pluralize accordingly
             $lastTwoCharacters = substr($entityName, strlen($entityName) - 2, 2);
             if (strcasecmp($lastTwoCharacters, 'ss') == 0) {
                 $entityName .= 'es';
             }
         }
         self::$_logger->debug('Entity name -> ' . $entityName);
         self::$_logger->debug('Caching within: ' . CACHE_DIR . APPLICATION_NAME . '/database_tables/');
         $entityFile = $this->{FrameworkConstants::DATABASE_NAME} . '_' . $entityName;
         // check from cache first
         $fields = CacheHelper::getCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile, self::ENTITY_CACHE_AGE, true);
         if (empty($fields)) {
             $fields = array();
             $this->getConnection();
             $sql = 'SHOW COLUMNS FROM ' . $entityName;
             $stmt = self::$_datasourceRegistry[$this->_currentDataSourceName]->prepare($sql);
             if ($stmt->execute()) {
                 while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                     $fields[$row->Field] = $row;
                 }
             }
             // cache the fields
             CacheHelper::saveCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile, $fields, true);
             $this->close();
         }
         $this->columns = $fields;
         $createSql = CacheHelper::getCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_create_statement', self::ENTITY_CACHE_AGE);
         if (empty($createSql)) {
             // create crud prepared statements
             $createSql = 'insert into ' . $entityName;
             $sqlFields = '';
             $sqlValues = '';
             $count = 0;
             foreach ($fields as $field) {
                 $primaryKey = $field->Key;
                 $extra = $field->Extra;
                 $fieldName = $field->Field;
                 if (isset($primaryKey) && strcmp($extra, 'auto_increment') == 0) {
                     $fieldName = null;
                 }
                 if ($fieldName) {
                     if ($count < 1) {
                         $sqlFields = $fieldName;
                         $sqlValues = '?';
                     } else {
                         $sqlFields .= ', ' . $fieldName;
                         $sqlValues .= ', ?';
                     }
                     $count++;
                 }
             }
             $createSql = $createSql . '(' . $sqlFields . ') values (' . $sqlValues . ')';
             // cache this prepared statement
             CacheHelper::saveCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_create_statement', $createSql);
         }
         $this->create_statement = $createSql;
         self::$_logger->debug('Caching: ' . $createSql);
         // create read/select prepared statement
         $readSql = CacheHelper::getCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_read_statement', self::ENTITY_CACHE_AGE);
         if (empty($readSql)) {
             $readSql = 'select * from ' . $entityName;
             CacheHelper::saveCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_read_statement', $readSql);
         }
         $this->read_statement = $readSql;
         self::$_logger->debug('Caching: ' . $readSql);
         // create update prepared statement
         $updateSql = CacheHelper::getCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_update_statement', self::ENTITY_CACHE_AGE);
         if (empty($updateSql)) {
             $updateSql = 'update ' . $entityName;
             CacheHelper::saveCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_update_statement', $updateSql);
         }
         $this->update_statement = $updateSql;
         self::$_logger->debug('Caching: ' . $updateSql);
         // create delete prepared statement
         $deleteSql = CacheHelper::getCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_delete_statement', self::ENTITY_CACHE_AGE);
         if (empty($deleteSql)) {
             $deleteSql = 'delete from ' . $entityName;
             CacheHelper::saveCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $entityFile . '_delete_statement', $deleteSql);
         }
         $this->delete_statement = $deleteSql;
         self::$_logger->debug($deleteSql);
         if ($this->joins) {
             $from = $this->joins['from'];
             $where = $this->joins['where'];
             //$sql = CacheHelper::getCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/', $from . '_' . $where . '_statement', self::ENTITY_CACHE_AGE);
             //if(empty($sql)){
             $sql = 'select * from ' . $from . ' where ' . $where;
             //CacheHelper::saveCache(CACHE_DIR . APPLICATION_NAME . '/database_tables/' , $from . '_' . $where . '_statement', $sql);
             //}
             $this->joined_query = $sql;
             self::$_logger->debug('Caching: ' . $sql);
         }
     } catch (Exception $ex) {
         $this->close();
         self::$_logger->error('Could not delete record', $ex);
     }
 }