Exemple #1
0
 public static function defineClass($className, $extends = '')
 {
     //        $namespace = 'Eddmash\PowerOrm\Migration\Model';
     $namespace = '';
     $use = '';
     $extendedClass = '';
     if (empty($extends) || Model::isModelBase($extends)) {
         $extends = Model::getFullClassName();
     } else {
         $extendedClass = sprintf('%s%s', ClassHelper::getFormatNamespace($namespace, true), $extends);
         $use = sprintf('use %s;', $extendedClass);
         $extends = trim(substr($extends, strripos($extends, '\\')), '\\');
     }
     if (!StringHelper::isEmpty($extendedClass) && !ClassHelper::classExists($extendedClass, $namespace)) {
         self::$deferedClasses[$extends][] = ['class' => $className, 'extends' => $extends];
         return false;
     }
     $extends = ClassHelper::getNameFromNs($extends, $namespace);
     $class = sprintf(self::getTemplate(), $namespace, $use, $className, $extends);
     $className = sprintf('%s%s', ClassHelper::getFormatNamespace($namespace, true), $className);
     if (ArrayHelper::hasKey(self::$deferedClasses, $className)) {
         foreach (self::$deferedClasses[$className] as $deferedClass) {
             self::defineClass($deferedClass['class'], $deferedClass['extends']);
         }
     }
     if (!ClassHelper::classExists($className, $namespace)) {
         eval($class);
     }
     return $className;
 }
Exemple #2
0
 /**
  * Gets a class name from the namespace by trimming out the namespace from the classname.
  *
  * @param $className
  * @param $namespace
  *
  * @return mixed
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public static function getNameFromNs($className, $namespace)
 {
     $className = static::getFormatNamespace($className, true, true);
     $namespace = static::getFormatNamespace($namespace, true, true);
     if (StringHelper::startsWith($className, $namespace)) {
         $className = substr($className, strlen($namespace));
     }
     return trim($className, '\\');
 }
Exemple #3
0
 public function deconstruct()
 {
     $path = '';
     $alias = '';
     if (StringHelper::startsWith($this->getFullClassName(), 'Eddmash\\PowerOrm\\Migration\\Operation\\Model')) {
         $alias = 'modelOperation';
         $path = sprintf('Eddmash\\PowerOrm\\Migration\\Operation\\Model as %s', $alias);
     }
     if (StringHelper::startsWith($this->getFullClassName(), 'Eddmash\\PowerOrm\\Migration\\Operation\\Field')) {
         $alias = 'fieldOperation';
         $path = sprintf('Eddmash\\PowerOrm\\Migration\\Operation\\Field as %s', $alias);
     }
     return ['name' => sprintf('%1$s\\%2$s', $alias, $this->getShortClassName()), 'path' => $path, 'fullName' => $this->getFullClassName(), 'constructorArgs' => $this->getConstructorArgs()];
 }
Exemple #4
0
 public static function run($config)
 {
     $baseDir = $config['baseDir'];
     if (strtolower(basename($baseDir)) === 'powerorm') {
         define('ENVIRONMENT', 'POWERORM_DEV');
     }
     // bootstrap the orm.
     require_once 'bootstrap.php';
     // load doctrine DBAL
     self::loadThirdParty();
     if (!StringHelper::startsWith(ENVIRONMENT, 'POWERORM_') && is_cli()) {
         new CI_Controller();
     }
 }
Exemple #5
0
 public function getConstructorArgs()
 {
     $constructorArgs = parent::getConstructorArgs();
     if (isset($constructorArgs['meta']) && empty($constructorArgs['meta'])) {
         unset($constructorArgs['meta']);
     }
     if (isset($constructorArgs['extends'])) {
         if (StringHelper::isEmpty($constructorArgs['extends']) || Model::isModelBase($constructorArgs['extends'])) {
             unset($constructorArgs['extends']);
         } else {
             $constructorArgs['extends'] = ClassHelper::getNameFromNs($constructorArgs['extends'], BaseOrm::getModelsNamespace());
         }
     }
     return $constructorArgs;
 }
Exemple #6
0
 /**
  * Returns the migration(s) which match the given prefix.
  *
  * @param $prefix
  *
  * @return mixed
  *
  * @throws AmbiguityError
  * @throws KeyError
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function getMigrationByPrefix($prefix)
 {
     $migrations = [];
     foreach ($this->getMigrations() as $name => $migration) {
         $shortName = ClassHelper::getNameFromNs($name, BaseOrm::getMigrationsNamespace());
         if (StringHelper::startsWith($name, $prefix) || StringHelper::startsWith($shortName, $prefix)) {
             $migrations[] = $name;
         }
     }
     if (count($migrations) > 1) {
         throw new AmbiguityError(sprintf("There is more than one migration with the prefix '%s'", $prefix));
     } elseif (count($migrations) == 0) {
         throw new KeyError(sprintf("There no migrations with the prefix '%s'", $prefix));
     }
     return $migrations[0];
 }
Exemple #7
0
 public function prepareMultiInheritance($parentModelName)
 {
     if (!self::isModelBase($parentModelName) && !StringHelper::isEmpty($parentModelName)) {
         $name = ClassHelper::getNameFromNs($parentModelName, BaseOrm::getModelsNamespace());
         $attrName = lcfirst(str_replace(' ', '', ucwords(str_replace('\\', ' ', $name))));
         $attrName = sprintf('%sPtr', $attrName);
         if ($this->_fieldCache == null || !ArrayHelper::hasKey($this->_fieldCache, $attrName)) {
             $field = OneToOneField::createObject(['to' => ClassHelper::getNameFromNs($parentModelName, BaseOrm::getModelsNamespace()), 'onDelete' => Delete::CASCADE, 'name' => $attrName, 'autoCreated' => true, 'parentLink' => true]);
             $this->addToClass($attrName, $field);
             $this->meta->parents[$name] = $field;
         }
     }
 }
Exemple #8
0
 /**
  * Add the current object to the passed in object.
  *
  * @param string $propertyName the name map the current object to, in the class object passed in
  * @param Model  $classObject  the object to attach the current object to
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function contributeToClass($propertyName, $classObject)
 {
     $classObject->{$propertyName} = $this;
     $this->modelName = $this->getName($classObject->getFullClassName());
     $this->scopeModel = $classObject;
     // override with the configs now.
     foreach (static::$DEFAULT_NAMES as $defaultName) {
         if (ArrayHelper::hasKey($this->overrides, $defaultName)) {
             $this->{$defaultName} = $this->overrides[$defaultName];
         }
     }
     if ($this->dbTable == null) {
         $this->dbTable = $this->_getTableName();
     }
     $vName = $this->verboseName;
     $this->verboseName = empty($vName) ? ucwords(StringHelper::camelToSpace($this->modelName)) : $vName;
 }
Exemple #9
0
 /**
  * provides the m2m table name for this relation.
  *
  * @param Meta $meta
  *
  * @return string
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function _getM2MDbTable($meta)
 {
     if ($this->relation->through !== null) {
         return $this->relation->through->meta->dbTable;
     } elseif ($this->dbTable) {
         return $this->dbTable;
     } else {
         // oracle allows identifier of 30 chars max
         return StringHelper::truncate(sprintf('%s_%s', $meta->dbTable, $this->name), 30);
     }
 }
Exemple #10
0
 /**
  * @dataProvider providerEmptyStrings
  *
  * @param $original
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function testStringIsEmpty($original)
 {
     $this->assertTrue(StringHelper::isEmpty($original));
 }