Пример #1
0
 /**
  * Proceses every property before being setted
  *
  * @param string $name
  * @param mixed $value
  */
 public function __set ( $name, $value )
 {
   if ( $name != 'locale' ) {
     $value = tlalokes_core_get_type( $value );
     $this->{$name} = self::recursiveSet( $name, $value );
   }
 }
Пример #2
0
/**
 * Parses a DocComment string
 *
 * @author Basilio Briceno <*****@*****.**>
 * @param mixed $doc_comment
 */
function tlalokes_parser_annotations(&$ref)
{
    // find annotations and its values
    preg_match_all('/@(\\w*)\\s*\\(\\s*(.*)\\s*\\)/', $ref->getDocComment(), $match);
    // iterate annotations to get its contents
    $count = count($match[1]);
    for ($i = 0; $i < $count; $i++) {
        if (!class_exists($match[1][$i])) {
            tlalokes_error_msg('Annotations: Class ' . $match[1][$i] . ' not found.');
        }
        $class[$i][0] = $match[1][$i];
        $class[$i][1] = new $match[1][$i]();
        // check if annotation contains more than one property
        $properties = explode(',', $match[2][$i]);
        if (count($properties) > 1) {
            // iterate multiple properties
            $count_prop = count($properties);
            for ($iprop = 0; $iprop < $count_prop; ++$iprop) {
                // validate property and get its value
                $p = tlalokes_parser_annotation_valid($properties[$iprop], $class[$i][1]);
                // set value on class
                $class[$i][1]->{$p[0]} = tlalokes_core_get_type($p[1]);
                unset($p);
            }
            unset($count_prop);
            // process single property
        } else {
            // validate property and get its value
            $p = tlalokes_parser_annotation_valid($properties[0], $class[$i][1]);
            // set value on class
            $class[$i][1]->{$p[0]} = tlalokes_core_get_type($p[1]);
            unset($p);
        }
        unset($properties);
    }
    unset($count);
    if (isset($class) && count($class) >= 1) {
        // set annotations for return
        foreach ($class as $c) {
            // get an instance of the class
            $in = new $c[0]();
            //$count = count( $properties );
            foreach (get_object_vars($in) as $k => $v) {
                if (isset($c[1]->{$k})) {
                    $in->{$k} = $c[1]->{$k};
                }
            }
            // set instances in an annotations array
            $annotations[$c[0]] =& $in;
            unset($in);
        }
        unset($class);
    }
    return isset($annotations) ? $annotations : false;
}
Пример #3
0
 /**
  * Actions before setting properties
  *
  * @param string $name
  * @param mixed $value
  */
 public function __set($name, $value)
 {
     // get type
     $value = tlalokes_core_get_type($value);
     // sanitize value
     $this->{$name} = is_string($value) ? tlalokes_str_sanitize($value) : $value;
 }
 /**
  * Creates a schema.xml file based in Database Definition objects
  *
  * @param TlalokesRegistry $reg
  */
 private static function buildSchemaFromDefs(TlalokesRegistry &$reg)
 {
     // static flags
     static $uniques = 0;
     static $indexes = 0;
     // build database xml dom object
     $dom = new DOMDocument('1.0', 'utf-8');
     $dom->formatOutput = true;
     $db = $dom->appendChild(new DOMElement('database'));
     $db->setAttribute('name', $reg->conf['dsn']['name']);
     // find table definitions
     foreach (glob($reg->conf['path']['app'] . $reg->conf['path']['def'] . '*Def.php') as $def) {
         // get class name
         $class_name = preg_replace('/.*\\/(\\w*Def).php$/', '$1', $def);
         // reflect annotated class
         $ref = new ReflectionAnnotatedClass($class_name);
         // check if @DefinitonObject is set
         if (!$ref->hasAnnotation('DefinitionObject')) {
             tlalokes_error_msg('PropelFactory: There is no DefinitionObject in ' . $class_name);
         }
         // check if object is marked for build
         if ($ref->getAnnotation('DefinitionObject')->build) {
             // build xml table node
             $table = $db->appendChild(new DOMElement('table'));
             // set table name attribute
             $table_name = $ref->getAnnotation('DefinitionObject')->table;
             $table->setAttribute('name', $table_name);
             unset($table_name);
             $table->setAttribute('idMethod', 'native');
             // find columns
             foreach ($ref->getProperties() as $property) {
                 // reflect column
                 $column = $property->getAnnotation('DefinitionObject');
                 // build xml column nodes
                 $col = $table->appendChild(new DOMElement('column'));
                 // name
                 $col->setAttribute('name', $column->column);
                 // phpName
                 if ($property->getName() != $column->column) {
                     $col->setAttribute('phpName', (string) $property->getName());
                 }
                 // type
                 $col->setAttribute('type', $column->type);
                 // size
                 if ($column->size) {
                     $col->setAttribute('size', $column->size);
                 }
                 // scale
                 if ($column->scale) {
                     $col->setAttribute('scale', $column->scale);
                 }
                 // required
                 if ($column->required) {
                     $col->setAttribute('required', $column->required ? 'true' : 'false');
                 }
                 // autoIncrement
                 if ($column->autoIncrement) {
                     $col->setAttribute('autoIncrement', $column->autoIncrement ? 'true' : 'false');
                     // If RBDMS is PgSQL and there is no default set the default
                     // WARNING: It needs to be tested with Oracle
                     if ($reg->conf['dsn']['type'] == 'pgsql') {
                         // build id-method-parameter
                         $imp_name = 'id-method-parameter';
                         $imp = $table->appendChild(new DOMElement($imp_name));
                         $imp->setAttribute('value', $table_name . '_seq');
                         // set default
                         if (!isset($column->default)) {
                             $col->setAttribute('default', 'nextval(\'' . $table_name . '_seq\'::regclass)');
                         }
                     }
                 }
                 // primaryKey
                 if ($column->primaryKey) {
                     $col->setAttribute('primaryKey', $column->primaryKey ? 'true' : 'false');
                 }
                 // default
                 if ($column->default || $column->default === 0) {
                     $col->setAttribute('default', tlalokes_core_get_type($column->default));
                 }
                 // find unique
                 if (isset($column->unique) && $column->unique) {
                     if (isset($uniques)) {
                         $uniques++;
                     } else {
                         $uniques = 1;
                     }
                     $unique_column_name[] = $column->column;
                 }
                 // find index
                 if (isset($column->index) && $column->index) {
                     $indexes = isset($indexes) ? $indexes + 1 : 1;
                     $index_column_name[] = $column->column;
                 }
                 // find reference
                 $reference = $property->getAnnotation('ReferenceDef');
                 if ($reference) {
                     // build foreign-key xml node
                     $fk = $table->appendChild(new DOMElement('foreign-key'));
                     $fk->setAttribute('foreignTable', $reference->table);
                     $fk->setAttribute('onDelete', strtolower($reference->onDelete));
                     $fk->setAttribute('onUpdate', strtolower($reference->onUpdate));
                     $rf = $fk->appendChild(new DOMElement('reference'));
                     $rf->setAttribute('local', $column->column);
                     $rf->setAttribute('foreign', $reference->column);
                 }
             }
             // find uniques flag
             if (isset($uniques) && $uniques >= 1) {
                 if (isset($unique_column_name)) {
                     foreach ($unique_column_name as $ucn) {
                         // build unique xml node
                         $unique = $table->appendChild(new DOMElement('unique'));
                         // build unique-column xml node
                         $uc = $unique->appendChild(new DOMElement('unique-column'));
                         $uc->setAttribute('name', $ucn);
                     }
                     unset($unique_column_name);
                 }
                 unset($uniques);
             }
             // find indexes flag
             if (isset($indexes) && $indexes >= 1) {
                 foreach ($index_column_name as $icn) {
                     // build index xml node
                     $index = $table->appendChild(new DOMElement('index'));
                     // build index-column xml node
                     $ic = $index->appendChild(new DOMElement('index-column'));
                     $ic->setAttribute('name', $icn);
                 }
                 unset($indexes);
                 unset($index_column_name);
             }
         }
         // set file path to schema.xml
         $file = $reg->conf['path']['app'] . $reg->conf['path']['tmp'] . 'generator/schema.xml';
         // write schema.xml file or return a CoreException
         if (!@file_put_contents($file, $dom->saveXML())) {
             tlalokes_error_msg('Propel: Cannot write database schema', true);
         }
     }
 }
Пример #5
0
/**
 * Returns the type of the provided $value
 *
 * @author Basilio Briceno <*****@*****.**>
 * @param mixed $value
 * @return mixed
 */
function tlalokes_core_get_type($value)
{
    if (is_numeric($value)) {
        if (preg_match('/[0-9]*[\\.][0-9]*/', $value)) {
            $value = (double) $value;
        } else {
            $value = (int) $value;
        }
    }
    if ($value) {
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                $value[$k] = tlalokes_core_get_type($v);
            }
        } else {
            if (is_bool($value)) {
                $value = $value;
            } elseif ($value == 'false' || $value == 'FALSE') {
                $value = false;
            } elseif ($value == 'true' || $value == 'TRUE') {
                $value = true;
            }
        }
    }
    return $value;
}
Пример #6
0
/**
 * Returns the type of the provided $value
 *
 * @author Basilio Briceno <*****@*****.**>
 * @copyright Copyright (c) 2011, Basilio Briceno
 * @license http://www.gnu.org/licenses/lgpl.html GNU LGPL
 * @param mixed $value
 * @return mixed
 */
function tf_cast_type($value)
{
    if (is_numeric($value)) {
        // float
        if (preg_match('/[0-9]*[\\.][0-9]*/', $value)) {
            $value = (double) $value;
            // integer
        } else {
            // Integer overflow on a 32-bit system
            if ($value < 2147483647) {
                $value = (int) $value;
            }
        }
    }
    if ($value) {
        // if array be recursive
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                $value[$k] = tlalokes_core_get_type($v);
            }
            // boolean
        } else {
            if (is_bool($value)) {
                $value = $value;
            } elseif ($value == 'false' || $value == 'FALSE') {
                $value = false;
            } elseif ($value == 'true' || $value == 'TRUE') {
                $value = true;
            }
        }
    }
    return $value;
}