public static function generate($modelClass, Extension $extension) { /** * @var $newController \MABI\ModelController */ $calledClass = get_called_class(); $newController = new $calledClass($extension); $newController->modelClass = $modelClass; $newController->base = Inflector::pluralize(strtolower(ReflectionHelper::stripClassName($modelClass))); $newController->model = call_user_func($newController->modelClass . '::init', $newController->getApp()); return $newController; }
public function __construct($extension) { parent::__construct($extension); $systemCache = $this->getApp()->getCacheRepository('system'); $cacheKey = get_called_class() . get_class() . '::__construct'; if ($systemCache != null && ($cache = $systemCache->get($cacheKey)) != null) { $this->documentationName = $cache; return; } // If this was an autogenerated RESTModelController instead of an overridden one, the name in the documentation // should be based on the Model class, unless it was changed on the overrride if (get_called_class() == 'MABI\\RESTModelController') { $this->documentationName = ucwords(ReflectionHelper::stripClassName($this->modelClass)); } if ($systemCache != null) { $systemCache->forever($cacheKey, $this->documentationName); } }
public static function addModel(\SimpleXMLElement &$iosModel, \MABI\Model $mabiModel) { $entity = $iosModel->addChild('entity'); $entity->addAttribute('name', ReflectionHelper::stripClassName(get_class($mabiModel))); $entity->addAttribute('syncable', 'YES'); $entity->addAttribute('representedClassName', ReflectionHelper::stripClassName(get_class($mabiModel))); $attribute = $entity->addChild('attribute'); $attribute->addAttribute('name', $mabiModel->getIdProperty()); $attribute->addAttribute('optional', 'YES'); $attribute->addAttribute('attributeType', 'String'); $attribute->addAttribute('syncable', 'YES'); $rClass = new \ReflectionClass($mabiModel); $rProperties = $rClass->getProperties(\ReflectionProperty::IS_PUBLIC); foreach ($rProperties as $rProperty) { /* * Ignores writing any model property with 'internal' or 'system' option */ if (in_array('internal', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field')) || in_array('system', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field'))) { continue; } // Pulls out the type following the pattern @var <TYPE> from the doc comments of the property $varDocs = ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'var'); if (empty($varDocs)) { self::addIOSAttribute($entity, $rProperty->getName(), 'string'); } else { $type = $varDocs[0]; $matches = array(); if (preg_match('/(.*)\\[\\]/', $type, $matches)) { // If the type follows the list of type pattern (<TYPE>[]), an array will be generated and filled // with that type $type = $matches[1]; self::addIOSAttribute($entity, $rProperty->getName(), $type); } else { self::addIOSAttribute($entity, $rProperty->getName(), $type); } } } }
public function __construct($extension) { $this->extension = $extension; $systemCache = $this->getApp()->getCacheRepository('system'); $cacheKey = get_called_class() . get_class() . '::__construct'; /** * @var $cache \MABI\CachedControllerConstructor */ if ($systemCache != null && is_object($cache = $systemCache->get($cacheKey))) { $this->base = $cache->base; foreach ($cache->middlewareFiles as $middlewareClass => $middlewareFile) { $this->addMiddlewareByClass($middlewareClass, $middlewareFile); } $this->documentationName = $cache->documentationName; return; } if (empty($this->base)) { $this->base = strtolower(ReflectionHelper::stripClassName(ReflectionHelper::getPrefixFromControllerClass(get_called_class()))); } $rClass = new \ReflectionClass(get_called_class()); // Load middlewares from @middleware directive $middlewareFiles = array(); $middlewares = ReflectionHelper::getDocDirective($rClass->getDocComment(), 'middleware'); foreach ($middlewares as $middlewareClass) { $middlewareFile = ReflectionHelper::stripClassName($middlewareClass) . '.php'; $this->addMiddlewareByClass($middlewareClass, $middlewareFile); $middlewareFiles[$middlewareClass] = $middlewareFile; } if (empty($this->documentationName)) { $this->documentationName = ucwords(ReflectionHelper::stripClassName(ReflectionHelper::getPrefixFromControllerClass(get_called_class()))); } if ($systemCache != null) { $systemCache->forever($cacheKey, new CachedControllerConstructor($this->base, $middlewareFiles, $this->documentationName)); } }
/** * todo: docs * * @param $app App * * @return Model */ public static function init($app) { $modelClass = get_called_class(); /** * @var $newModelObj Model */ $newModelObj = new $modelClass(); $newModelObj->modelClass = $modelClass; $newModelObj->app = $app; $systemCache = $app->getCacheRepository('system'); $cacheKey = get_called_class() . get_class() . '::init'; /** * @var $cache \MABI\CachedModelConstructor */ if ($systemCache != null && is_object($cache = $systemCache->get($cacheKey))) { $newModelObj->table = $cache->table; $newModelObj->idColumn = $cache->idColumn; $newModelObj->idProperty = $cache->idProperty; $newModelObj->modelFieldsInfo = $cache->modelFieldsInfo; $newModelObj->{$newModelObj->idProperty} = NULL; return $newModelObj; } if (empty($newModelObj->table)) { $newModelObj->table = strtolower(Inflector::pluralize(ReflectionHelper::stripClassName($modelClass))); } // Gets the default ID column on the DataConnection side if (empty($newModelObj->idColumn)) { $newModelObj->idColumn = $newModelObj->app->getDataConnection($newModelObj->connection)->getDefaultIdColumn(); } // Allows overrides of idProperty for the name of the ID on the MABI side if (empty($newModelObj->idProperty)) { // $rClass = new \ReflectionClass($newModelObj); $rProperties = $rClass->getProperties(\ReflectionProperty::IS_PUBLIC); foreach ($rProperties as $rProperty) { /* * Looks for the '@field id' directive to set as the id property */ if (in_array('id', ReflectionHelper::getDocDirective($rProperty->getDocComment(), 'field'))) { $newModelObj->idProperty = $rProperty->getName(); break; } } if (empty($newModelObj->idProperty)) { $newModelObj->idProperty = 'id'; } } $newModelObj->setupFieldInfo(); if ($systemCache != null) { $systemCache->forever($cacheKey, new CachedModelConstructor($newModelObj->table, $newModelObj->idColumn, $newModelObj->idProperty, $newModelObj->modelFieldsInfo)); } $newModelObj->{$newModelObj->idProperty} = NULL; return $newModelObj; }
public function getModelClasses() { $modelClasses = array(); foreach ($this->extensions as $extension) { $modelClasses = array_merge($modelClasses, $extension->getModelClasses()); } foreach ($this->modelLoaders as $modelLoader) { $loadModelClasses = $modelLoader->loadModels(); foreach ($loadModelClasses as $modelClass) { // allow model overrides foreach ($modelClasses as $k => $overriddenModel) { if (ReflectionHelper::stripClassName($modelClass) == ReflectionHelper::stripClassName($overriddenModel)) { unset($modelClasses[$k]); break; } } $modelClasses[] = $modelClass; } } return $modelClasses; }