/**
  * @param string $filename in this case it is a class name
  * @return ezdbiSchemaChecks
  * @throws Exception
  */
 public function parseFile($filename)
 {
     if (!class_exists($filename)) {
         throw new Exception("Can not not analyze ezpo class definition for '{$filename}': class not found");
     }
     if (!is_subclass_of($filename, 'eZPersistentObject')) {
         throw new Exception("Can not not analyze class definition for '{$filename}': not an ezpo");
     }
     $def = call_user_func(array($filename, 'definition'));
     $checks = new ezdbiSchemaChecks();
     ksort($def['fields']);
     foreach ($def['fields'] as $col => $value) {
         if (!isset($value['foreign_class'])) {
             continue;
         }
         $checks->addForeignKey($def['name'], $col, $this->resolveClassToTable($value['foreign_class']), $value['foreign_attribute']);
     }
     return $checks;
 }
 /**
  * Returns the list of checks
  *
  * @return array name => description
  */
 public function getChecks()
 {
     $out = array();
     foreach ($this->checks->getForeignKeys() as $key => $value) {
         $out['FK_' . $key] = $value;
     }
     foreach ($this->checks->getQueries() as $key => $value) {
         $out['Other_' . $key] = $value;
     }
     return $out;
 }
Esempio n. 3
0
 public function writeFile($filename, ezdbiSchemaChecks $schemaDef)
 {
     $out = "<?php /*\n";
     $out .= "\n[ForeignKeys]\n";
     foreach ($schemaDef->getForeignKeys() as $def) {
         $defs = array($def['childCol'], $def['parentTable'], $def['parentCol']);
         if ($def['exceptions'] != '') {
             $defs[] = $def['exceptions'];
         }
         $out .= $def['childTable'] . '[]=' . implode($this->token, $defs) . "\n";
     }
     $out .= "\n[CustomQueries]\n";
     foreach ($schemaDef->getQueries() as $def) {
         $name = str_replace(' ', '_', $def['description']);
         $out .= $name . '[sql]=' . str_replace("\n", ' ', $def['sql']) . "\n";
         if ($def['longDesc'] != '') {
             $out .= $name . '[description]=' . str_replace("\n", ' ', $def['longDesc']) . "\n";
         }
     }
     file_put_contents($filename, $out);
 }
 *
 * @author G. Giunta
 * @copyright (C) G. Giunta 2014-2016
 * @license Licensed under GNU General Public License v2.0. See file license.txt
 */
require 'autoload.php';
// Inject our own autoloader after the std one, as this script is supposed to be
// executable even when extension has not been activated
////spl_autoload_register( array( 'autoloadHelper', 'autoload' ) );
$cli = eZCLI::instance();
$script = eZScript::instance(array('description' => "Generate FK definition from eZPersistentObjects", 'use-session' => false, 'use-modules' => true, 'use-extensions' => true));
$script->startup();
$options = $script->getOptions('[schemaformat:][extensions]', '[schemafile]', array('schemafile' => 'Name of file with db schema checks to create', 'schemaformat' => 'Format of db schema checks definition file (ini by default)', 'extensions' => 'Also parse classes from extensions'));
$script->initialize();
$parser = new ezdbiPersistentObjectFormat();
$checks = new ezdbiSchemaChecks();
$cli->output('Checking classes from kernel autoloads...');
$classes = (include 'autoload/ezp_kernel.php');
ksort($classes);
foreach ($classes as $class => $file) {
    if (is_subclass_of($class, 'eZPersistentObject')) {
        $classChecks = $parser->parseFile($class);
        $cli->output("Class: {$class}, found " . count($classChecks->getForeignKeys()) . " keys");
        $checks->merge($classChecks);
    }
}
if ($options['extensions']) {
    $cli->output('Checking classes from extension autoloads...');
    $classes = (include 'var/autoload/ezp_extension.php');
    ksort($classes);
    foreach ($classes as $class => $file) {