コード例 #1
0
 public function setFile($file = null)
 {
     $return = false;
     if (!empty($file)) {
         if (!file_exists($file)) {
             if ($pathFile = Torpor::getFileInPath($file)) {
                 trigger_error('Specified file "' . $file . '" not found, using "' . $pathFile . '" found in include path', E_USER_WARNING);
                 $file = $pathFile;
             } else {
                 trigger_error('Specified file "' . $file . '" does not exist; attempting to create an empty SQLite database', E_USER_WARNING);
             }
         }
         if ($file !== $this->getFile()) {
             if ($this->isConnected()) {
                 $this->disconnect();
             }
             $this->_file = $file;
             $return = true;
         }
     } else {
         if ($this->getFile()) {
             trigger_error('No file specified; maintaining prior connection.');
         } else {
             $this->throwException('Cannot connect to or create empty SQLite database');
         }
     }
     return $return;
 }
コード例 #2
0
 public function _getTorpor()
 {
     if (is_null($this->_torpor)) {
         $this->_setTorpor(Torpor::getInstance());
     }
     return $this->_torpor;
 }
コード例 #3
0
 function checkColumnName($columnName)
 {
     $columnName = Torpor::containerKeyName($columnName);
     if (!$this->hasColumn($columnName)) {
         throw new TorporException($columnName . ' is not a valid member of this Grid');
     }
     return $columnName;
 }
コード例 #4
0
ファイル: Column.php プロジェクト: codegooglecom/torpor-php
 public function initialize(SimpleXMLElement $xmlDef)
 {
     // TODO: Loaded primary keys should be read only?
     // TODO: Build a local attributes interface in order to keep settings conveniently
     // accessible to inheriting classes.
     $this->_attributes = array();
     $type = Torpor::makeKeyName((string) $xmlDef->attributes()->type);
     if (!in_array($type, $this->getValidTypes())) {
         $this->throwException('Unrecognized type "' . $type . '"');
     }
     $this->setType($type);
     switch ($type) {
         case self::TYPE_BINARY:
             if ((int) $xmlDef->attributes()->length) {
                 $this->setMaxLength((int) $xmlDef->attributes()->length);
             }
             break;
         case self::TYPE_BOOL:
             break;
         case self::TYPE_CLASS:
             break;
         case self::TYPE_DATE:
         case self::TYPE_DATETIME:
         case self::TYPE_TIME:
             break;
         case self::TYPE_FLOAT:
         case self::TYPE_INTEGER:
         case self::TYPE_UNSIGNED_INTEGER:
             if ((int) $xmlDef->attributes()->precision) {
                 $this->setMaxLength((int) $xmlDef->attributes()->precision);
             }
             if ((int) $xmlDef->attributes()->length) {
                 $this->setMaxLength((int) $xmlDef->attributes()->length);
             }
             break;
         case self::TYPE_CHAR:
         case self::TYPE_VARCHAR:
         case self::TYPE_TEXT:
             if ((string) $xmlDef->attributes()->encoding) {
                 $this->setEncoding((string) $xmlDef->attributes()->encoding);
             }
             if ((int) $xmlDef->attributes()->length) {
                 $this->setMaxLength((int) $xmlDef->attributes()->length);
             }
             break;
     }
     if (!isset($xmlDef->attributes()->dataName)) {
         $this->throwException('Required attribute dataName not found');
     }
     $this->_dataName = (string) $xmlDef->attributes()->dataName;
     if (isset($xmlDef->attributes()->generatedOnPublish) && (string) $xmlDef->attributes()->generatedOnPublish == Torpor::VALUE_TRUE) {
         $this->setGeneratedOnPublish();
     }
     if (isset($xmlDef->attributes()->nullable) && (string) $xmlDef->attributes()->nullable == Torpor::VALUE_FALSE) {
         $this->setNullable(false);
     }
     if (isset($xmlDef->attributes()->precision)) {
         $this->setPrecision((string) $xmlDef->attributes()->precision);
     }
     // Set any default data prior to configuring as readOnly
     if (isset($xmlDef->attributes()->default)) {
         // This way we reset to this for originalData, and are still considered
         // dirty
         // WARNING: validating content from the dataStore; which can throw some
         // strange warnings if the XML and the repository definitions don't agree.
         $this->_defaultData = (string) $xmlDef->attributes()->default;
         $this->_data = $this->validatePersistData($this->_defaultData);
         $this->setOriginalData($this->_data);
         $this->_setDirty();
     }
     if (isset($xmlDef->attributes()->readOnly) && (string) $xmlDef->attributes()->readOnly == Torpor::VALUE_TRUE) {
         $this->setReadOnly();
     }
     // TODO: options for navigating attributes
     foreach ($xmlDef->attributes() as $key => $val) {
         $this->_attributes[$key] = (string) $val;
     }
 }
コード例 #5
0
 public function addParameter($parameterName, $placeholder = null)
 {
     $paramterName = Torpor::makeKeyName($parameterName);
     if (empty($parameterName)) {
         throw new TorporException('Invalid parameterName');
     }
     $this->_parameters[] = $parameterName . (!empty($placeholder) ? Torpor::VALUE_SEPARATOR . $placeholder : '');
     return count($this->_parameters);
 }
コード例 #6
0
ファイル: Grid.php プロジェクト: codegooglecom/torpor-php
 public function __construct()
 {
     $args = func_get_args();
     $torpor = null;
     foreach ($args as $index => $arg) {
         if ($arg instanceof Torpor) {
             $torpor = $arg;
             unset($args[$index]);
             break;
         }
     }
     $this->_setTorpor($torpor instanceof Torpor ? $torpor : Torpor::getInstance());
     $gridName = get_class($this);
     if ($prefix = $this->Torpor()->typedGridClassesPrefix()) {
         $gridName = substr($gridName, strlen($prefix));
     }
     if (!$this->Torpor()->supportedGrid($gridName)) {
         $this->throwException('Could not resolve grid type from class name "' . get_class($this) . '"');
     }
     $this->_setObjName($gridName);
     // $gridPrototype = $this->Torpor()->_newGrid( $gridName, Torpor::DEFAULT_GRID_CLASS );
     $this->Torpor()->_newGridColumns($this);
     $this->Torpor()->_newGridParameters($this);
     // Look at the remaining arguments, and see if they match the primary key length of this
     // grid, populating in order.
     $primaryKey = $this->primaryKey();
     if (!is_array($primaryKey)) {
         $primaryKey = array($primaryKey);
     }
     // Doing this via array_values resets any index gaps resuting from the Torpor object
     // search and subsequent unset() calls above.
     // This pattern of invocation assumes that all keys will be passed in the order in
     // which they are defined in the XML (and/or that there's only 1, which will be the
     // most common case).
     foreach (array_values($args) as $index => $arg) {
         $primaryKey[$index]->setData($arg);
     }
     $this->OnNew();
 }
コード例 #7
0
ファイル: GridSet.php プロジェクト: codegooglecom/torpor-php
 public function __construct()
 {
     $args = func_get_args();
     $torpor = null;
     foreach ($args as $index => $arg) {
         if ($arg instanceof Torpor) {
             $torpor = $arg;
             unset($args[$index]);
             break;
         }
     }
     $this->_setTorpor($torpor instanceof Torpor ? $torpor : Torpor::getInstance());
     $gridTypeName = get_class($this);
     if ($prefix = $this->Torpor()->typedGridClassesPrefix()) {
         $gridTypeName = substr($gridTypeName, strlen($prefix));
     }
     $gridTypeName = substr($gridTypeName, 0, -1 * strlen(Torpor::OPERATION_GET_SET));
     if (!$this->Torpor()->supportedGrid($gridTypeName)) {
         $this->throwException('Could not resolve grid type from class name "' . get_class($this) . '"');
     }
     $this->setType($gridTypeName);
     foreach ($args as $arg) {
         if ($arg instanceof CriteriaBase) {
             $this->addCriteria($arg);
         } else {
             if ($arg instanceof Grid) {
                 $this->setSourceGrid($arg);
             }
         }
     }
 }
コード例 #8
0
ファイル: Torpor.php プロジェクト: codegooglecom/torpor-php
 public static function typedGridClassCreate($className, $checkInitialization = true, $extends = 'TypedGrid')
 {
     $return = !$checkInitialization;
     $setClass = false;
     if ($checkInitialization) {
         $torpor = isset($this) ? $this : Torpor::getInstance();
         $gridName = substr($className, strlen($torpor->typedGridClassesPrefix()));
         if (!$torpor->supportedGrid($gridName) && ($gridSetName = strtolower(substr($gridName, -1 * strlen(self::OPERATION_GET_SET)))) == self::OPERATION_GET_SET) {
             $setClass = true;
             $gridName = substr($gridName, 0, -1 * strlen(self::OPERATION_GET_SET));
         }
         $return = $torpor->supportedGrid($gridName) && $torpor->typedGridClasses();
         if ($return) {
             $className = $torpor->typedGridClassesPrefix() . $gridName;
         }
     }
     if ($return) {
         // Should be done in 2 passes: determine if we're looking for a set, and if so create
         // that definition.  If not, create the grid class only.  This allows for more flexible
         // end user implementation (can extend one without affecting the other)
         // Using class_exists() causes some initialization recursion if typedGridClassCheck
         // is hooked into __autoload, so we use the slightly heavier get_declared_classes
         // instead which saves us running the same portions of code over and over.
         $declaredClasses = array_map('strtoupper', get_declared_classes());
         if ($setClass) {
             $setClassName = $className . self::OPERATION_GET_SET;
             if (!in_array(strtoupper($setClassName), $declaredClasses)) {
                 eval('class ' . $setClassName . ' extends ' . $extends . self::OPERATION_GET_SET . ' {}');
             }
         } else {
             if (!in_array(strtoupper($className), $declaredClasses)) {
                 eval('class ' . $className . ' extends ' . $extends . ' {}');
             }
         }
     }
     return $return;
 }
コード例 #9
0
ファイル: Criteria.php プロジェクト: codegooglecom/torpor-php
 protected function processArgs(array $args)
 {
     $flatArgs = array();
     while (count($args)) {
         $arg = array_shift($args);
         if (is_array($arg)) {
             $args = array_merge($arg, $args);
         } else {
             $flatArgs[] = $arg;
         }
     }
     $preppedArgs = array();
     for ($i = 0; $i < count($flatArgs); $i++) {
         if ($this->isColumnTarget() && $i < count($flatArgs) - 1) {
             $grid = null;
             $column = null;
             if ($flatArgs[$i] instanceof Column) {
                 $grid = Torpor::containerKeyName($flatArgs[$i]->Grid());
                 $column = Torpor::containerKeyName($flatArgs[$i]);
             } else {
                 $grid = Torpor::containerKeyName($flatArgs[$i++]);
                 if (!isset($flatArgs[$i])) {
                     throw new TorporException('Grid/Column count mismatch in criteria type ' . $this->getType());
                 }
                 $column = Torpor::containerKeyName($flatArgs[$i]);
             }
             $preppedArgs[] = array($grid, $column);
         } else {
             $preppedArgs[] = $flatArgs[$i];
         }
     }
     $preppedArgCount = count($preppedArgs);
     $argCountException = new TorporException('Invalid argument count for type ' . $this->getType());
     switch ($this->getBaseType()) {
         case self::TYPE_BETWEEN:
             if ($preppedArgCount < 2 || $preppedArgCount > 3) {
                 throw $argCountException;
             }
             $preppedArgs[] = false;
             // Default for "inclusive"
             list($rangeOne, $rangeTwo, $inclusive) = $preppedArgs;
             $this->addArgument($rangeOne);
             $this->addArgument($rangeTwo);
             $this->setInclusive($inclusive);
             break;
         case self::TYPE_STARTSWITH:
         case self::TYPE_CONTAINS:
         case self::TYPE_ENDSWITH:
         case self::TYPE_EQUALS:
             if ($preppedArgCount < 1 || $preppedArgCount > 2) {
                 throw $argCountException;
             }
             $preppedArgs[] = false;
             // Default for "case insensitive"
             list($targetValue, $caseInsensitive) = $preppedArgs;
             $this->addArgument($targetValue);
             $this->setCaseInsensitive($caseInsensitive);
             break;
         case self::TYPE_GREATERTHAN:
         case self::TYPE_LESSTHAN:
             if ($preppedArgCount < 1 || $preppedArgCount > 2) {
                 throw $argCountException;
             }
             $preppedArgs[] = false;
             // Default for "inclusive"
             list($targetValue, $inclusive) = $preppedArgs;
             $this->addArgument($targetValue);
             $this->setInclusive($inclusive);
             break;
         case self::TYPE_CUSTOM:
             $this->setCustom(array_shift($preppedArgs));
         case self::TYPE_IN:
             if ($preppedArgCount < 1) {
                 throw $argCountException;
             }
             foreach ($preppedArgs as $arg) {
                 $this->addArgument($arg);
             }
             break;
         case self::TYPE_IN_SET:
             if ($preppedArgCount > 2) {
                 throw $argCountException;
             }
             $preppedArgs[] = false;
             // Default for "inclusive"
             // "inclusive" for gridSet has special meaning.  If set
             // to true, it will be used by the data store to compute
             // the entire possible affected set and not just the loaded
             // portion (or calculated page offset) of the target gridSet
             list($set, $inclusive) = $preppedArgs;
             if (!$set instanceof GridSet) {
                 throw new TorporException('Argument for ' . $this->getType() . ' criteria type must be an instance of GridSet');
             }
             $this->setInclusive($inclusive);
             $this->addArgument($set);
             break;
         case self::TYPE_PATTERN:
             if ($preppedArgCount < 1 || $preppedArgCount > 2) {
                 throw $argCountException;
             }
             $preppedArgs[] = false;
             // Default for "case insensitive"
             list($regex, $caseInsensitive) = $preppedArgs;
             $this->addArgument($regex);
             $this->setCaseInsensitive($caseInsensitive);
             break;
         case null:
         default:
             throw new TorporException('Type invalid or not set');
             break;
     }
 }
コード例 #10
0
ファイル: test.php プロジェクト: codegooglecom/torpor-php
$user->setId(12345);
$user->setUserName('george');
try {
    $user->setPasswordHash('george');
} catch (TorporException $e) {
    print_r('Encountered exception: ' . $e->getMessage() . "\n");
}
$user->setPasswordHash('something other than george');
var_dump($user->getId());
var_dump($user->getUserName());
var_dump($user->getEmail());
var_dump($user->getPasswordHash());
var_dump($user->GET_PASSWORD_HASH());
print "\n";
// TODO: test juggling multiple instances or Torpor.
$torpor = Torpor::getInstance();
// Create a grid instance in object context
$userToo = $torpor->newUser();
// Access member methods as a simple object,
// atomically redirected to getX and setX
$userToo->ID = 54321;
$userToo->ID++;
// Redirect works for increment operators too.
$userToo->UserName = '******';
$userToo->ReferringUserId = null;
foreach ($userToo->columnNames() as $column) {
    var_dump($userToo->{$column});
}
foreach ($userToo as $columnName => $column) {
    var_dump($columnName . ' = ' . var_export($column->getData(), true));
    var_dump($columnName . ' = ' . $column);