Beispiel #1
0
 /**
  * Get the column name for a token.
  * @param ValueToken $token
  * @param NamingConvention $namingConvention
  * @param ReflectionClass $class
  * @return string
  * @throws DslSyntaxException when no matching property could be found
  */
 private function tokenToColumn($token, $namingConvention, $class)
 {
     // Find a matching property
     $property = null;
     foreach ($class->getProperties() as $prop) {
         if (strcasecmp($token->getSource(), $prop->getName()) === 0) {
             $property = $prop;
             break;
         }
     }
     if ($property === null) {
         throw new DslSyntaxException('No matching property found for ' . $token->getSource());
     }
     return $namingConvention->propertyToColumnName($property);
 }
Beispiel #2
0
 /**
  * This method will run some analysis on the correctness of your configuration. It will be exported to the screen.
  * @param boolean $showAllData set this to false if you want to hide the entities row
  */
 public function checkDatabase($showAllData = true)
 {
     switch ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)) {
         case 'mysql':
             $sth = $this->pdo->prepare('SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = ?');
             $sth->execute(array($this->tableName));
             $columnNameColumn = 'COLUMN_NAME';
             break;
         case 'sqlite':
             $sth = $this->pdo->prepare("PRAGMA table_info({$this->tableName})");
             $sth->execute();
             $columnNameColumn = 'name';
             break;
         default:
             echo '<p>SQL Driver: ' . $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) . ' is not supported by the checking tool... sorry</p>' . PHP_EOL;
             return;
     }
     $tableProperties = $sth->fetchAll();
     $classProperties = $this->entityClass->getProperties();
     $properties = [];
     foreach ($tableProperties as $prop) {
         $newProp = new stdClass();
         $newProp->actualColumnName = $prop[$columnNameColumn];
         $properties[$newProp->actualColumnName] = $newProp;
     }
     foreach ($classProperties as $prop) {
         $expectedColumnName = $this->namingContention->propertyToColumnName($prop);
         if (array_key_exists($expectedColumnName, $properties)) {
             $newProp = $properties[$expectedColumnName];
         } else {
             $newProp = new stdClass();
             $properties[$expectedColumnName] = $newProp;
         }
         $newProp->propertyName = $prop->getName();
         $newProp->expectedColumnName = $expectedColumnName;
     }
     $issetOr = function (&$value, $default = '') {
         return isset($value) ? $value : $default;
     };
     $pdoAtt = function ($att) {
         try {
             return $this->pdo->getAttribute($att);
         } catch (PDOException $e) {
             return 'Not supported by driver';
         }
     };
     $getClass = function (stdClass $prop) use($issetOr) {
         $classes = $issetOr($prop->expectedColumnName) === $issetOr($prop->actualColumnName) ? 'correct' : 'error';
         if ($issetOr($prop->propertyName) === $this->idProperty->getName()) {
             $classes .= ' primary-key';
         }
         return $classes;
     };
     $entitiesError = false;
     $entities = [];
     if ($showAllData) {
         try {
             $entities = $this->findAll();
         } catch (\Exception $e) {
             $entitiesError = $e;
         }
     }
     include 'Check/checkDatabaseTable.php';
 }