function init() { $this->name_many = String::pluralize($this->name); $this->class_name = String::convertToCamelCase($this->name); $this->class_name_many = String::pluralize($this->class_name); $this->repository_class = 'Storage\\Repository\\' . $this->class_name . 'Repository'; $this->model_class = 'Storage\\Model\\' . $this->class_name . 'Base'; $model_file = $this->application->getProject()->getPath('src php Model ' . $this->class_name . '.php'); if (file_exists($model_file)) { $this->model_class = 'Model\\' . $this->class_name; } $repository_file = $this->application->getProject()->getPath('src php Repository ' . $this->class_name . '.php'); if (file_exists($repository_file)) { $this->repository_class = 'Repository\\' . $this->class_name; } if (count($this->properties)) { $properties = $this->properties; $this->properties = array(); foreach ($properties as $key => $config) { if ($config instanceof Model) { $reference = $this->hasOne($config); if (is_string($key)) { $reference->usingAlias($key); } } else { $this->addProperty($key, $config); } } } if (!$this->getPk()) { $this->addBehaviour('id'); } }
public function testFormatBytes() { $this->assertSame(String::formatBytes(1024), '1k'); $this->assertSame(String::formatBytes(1024 * 1024 * 2), '2M'); $this->assertSame(String::formatBytes(1024 * 1024 * 1024 * 4), '4G'); $this->assertSame(String::formatBytes(1024 * 1024 * 1024 * 1024 * 5), '5T'); }
public function build($script) { $dependencies = array_merge($this->dependency->getList($this->project->getPath("resources coffee {$script}.coffee")), $this->dependency->getList($this->sencha->getPath('src coffee Cti.coffee'))); $fs = new Filesystem(); $result = ''; $sourceList = array(); $stopwatch = new Stopwatch(); foreach (array_reverse($dependencies) as $coffee) { $sourceList[] = $coffee; $local = $this->source->getLocalPath($coffee); $local_js = dirname($local) . DIRECTORY_SEPARATOR . basename($local, 'coffee') . 'js'; $javascript = $this->project->getPath(sprintf('build js %s', $local_js)); if (!file_exists($javascript) || filemtime($coffee) >= filemtime($javascript)) { if ($this->debug) { $stopwatch->start($local); echo '- compile ' . $local; } $code = \CoffeeScript\Compiler::compile(file_get_contents($coffee), array('filename' => $coffee, 'bare' => true, 'header' => false)); if ($this->debug) { $event = $stopwatch->stop($local); echo ' (' . String::formatMilliseconds($event->getDuration()) . ' using ' . String::formatBytes($event->getMemory()) . ')' . PHP_EOL; } $fs->dumpFile($javascript, $code); } else { $code = file_get_contents($javascript); } $result .= $code . PHP_EOL; } $this->hash[$script] = $sourceList; $this->cache->set(__CLASS__, $this->hash); $filename = $this->project->getPath("public js {$script}.js"); $fs->dumpFile($filename, $result); return $filename; }
/** * @param string $nick * @param array $config * @return Behaviour * @throws \Exception */ public static function create(Model $model, $nick, $config = array()) { $class_name = sprintf('Cti\\Storage\\Behaviour\\%s', String::convertToCamelCase($nick)); if (!Reflection::getReflectionClass($class_name)->isSubclassOf(__CLASS__)) { throw new Exception(sprintf('Behaviour %s not found!', $nick)); } return new $class_name($model, $config); }
protected function execute(InputInterface $input, OutputInterface $output) { $id = implode('_', $input->getArgument('name')); $class = String::convertToCamelCase($id); $timestamp = time(); $filename = $this->application->getProject()->getPath('resources php migrations ' . date('Ymd_His', $timestamp) . '_' . $id . '.php'); $migration = $this->application->getManager()->create('Cti\\Storage\\Generator\\Migration', array('class' => $class, 'timestamp' => $timestamp)); $fs = new Filesystem(); $fs->dumpFile($filename, $migration); if (!defined('TEST_ENVIRONMENT')) { echo 'Migration created. ' . $filename . PHP_EOL; } }
/** * rendering full application * @return string */ private function renderApplication() { $contents = $this->renderHeader(); $bootstrap = $warm = array('Cti\\Core\\Module\\Manager' => 'Manager'); $methods = array('Manager' => $this->renderManager()); $core = $this->core; if (!count($core)) { $modules = scandir(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Module'); foreach ($modules as $line) { if (strpos($line, '.php')) { $core[] = 'Cti\\Core\\Module\\' . basename($line, '.php'); } } } foreach (array($core, $this->modules) as $source) { foreach ($source as $alias => $class) { if (is_numeric($alias)) { $alias = Reflection::getReflectionClass($class)->getShortName(); } else { $alias = String::convertToCamelCase($alias); } if (!isset($methods[$alias])) { if (Reflection::getReflectionClass($class)->implementsInterface('Cti\\Core\\Application\\Bootloader')) { $bootstrap[] = $alias; } if (Reflection::getReflectionClass($class)->implementsInterface('Cti\\Core\\Application\\Warmer')) { $warm[$class] = $alias; } $methods[$alias] = $this->renderGetter($alias, $class); } } } $methods['-2'] = $this->renderBootstrap($bootstrap); $warm = $this->processDependencies($warm); $methods['-1'] = $this->renderWarm($warm); ksort($methods); $contents .= implode(PHP_EOL . PHP_EOL, $methods); $contents .= PHP_EOL . '}'; return $contents; }
public function renderIndex($index) { $fields = $index->getFields(); sort($fields); $name = array(); $params = array(); $where = array(); foreach ($fields as $field) { $name[] = String::convertToCamelCase($field); $params[] = $field; $where[] = "->where('{$field} = :{$field}', array(\r\n '{$field}' => \${$field}\r\n ))"; } $name = 'by' . implode('And', $name); $params = '$' . implode(', $', $params); $where = implode(PHP_EOL . ' ', $where); return <<<STR public function {$name}({$params}) { return \$this {$where}; } STR; }
/** * Init Fenom engine with modifiers and functions * @param $fenom */ private function initEngine($fenom) { $fenom->addModifier('pluralize', function ($string) { return String::pluralize($string); }); $fenom->addModifier('camelcase', function ($string) { return String::convertToCamelCase($string); }); }
public function renderGetterAndSetter() { $reference = $this->reference; $property_name = String::pluralize($reference->getReferencedBy()); $getter = 'get' . String::convertToCamelCase($property_name); $source = $this->schema->getModel($reference->getSource()); $destination = $this->schema->getModel($reference->getDestination()); $finder = array(); foreach ($reference->getProperties() as $property) { $finder[] = "'" . $property->getName() . "' => \$this->" . $destination->getProperty($property->getForeignName())->getGetter() . "(),"; } $finder = implode(PHP_EOL . ' ', $finder); $source_model_class = $source->getModelClass(); $source_name = $reference->getSource(); $repository_getter = 'get' . String::pluralize(String::convertToCamelCase($source->getName())); $result = <<<PROPERTY /** * get {$property_name} * @return {$source_model_class} */ public function {$getter}() { if(is_null(\$this->{$property_name})) { \$this->{$property_name} = \$this->getRepository()->getMaster()->{$repository_getter}()->findAll(array( {$finder} )); } return \$this->{$property_name}; } PROPERTY; if ($this->schema->getModel($source_name)->hasBehaviour('link')) { $link = $this->schema->getModel($source_name); $foreign = $link->getBehaviour('link')->getForeignModel($destination); foreach ($link->getOutReferences() as $relation) { if ($relation->getDestination() == $foreign->getName()) { break; } } $relation_property_name = String::pluralize($relation->getDestinationAlias()); $relation_getter = 'get' . String::convertToCamelCase($relation_property_name); $link_getter = 'get' . String::convertToCamelCase($relation->getDestinationAlias()); $foreign_model_class = $foreign->getModelClass(); $result .= <<<PROPERTY /** * get {$relation_property_name} * @return {$foreign_model_class}[] */ public function {$relation_getter}() { if(is_null(\$this->{$relation_property_name})) { \$this->{$relation_property_name} = array(); foreach(\$this->{$getter}() as \$link) { \$this->{$relation_property_name}[] = \$link->{$link_getter}(); } } return \$this->{$relation_property_name}; } PROPERTY; } return $result; }
/** * run web processor * @throws \Cti\Core\Exception * @throws \Exception */ public function run() { $mount = array(); foreach ($this->controllers as $path => $controllerName) { $mount[$path] = strlen($path); } arsort($mount); $controller = null; foreach (array_keys($mount) as $path) { if (strpos($this->location, $path) === 0) { $controller = $this->controllers[$path]; $chain = explode('/', substr($this->location, strlen($path))); break; } } if (!$controller) { throw new Exception(sprintf("No controller can process url: %s", $this->location)); } $chain = array_values(array_filter($chain, 'strlen')); try { $slug = count($chain) ? array_shift($chain) : ''; $method = $this->method . String::convertToCamelCase($slug); if (method_exists($controller, $method)) { $result = $this->manager->call($controller, $method, array_merge($chain, array('chain' => $chain))); } elseif (method_exists($controller, 'processChain')) { if ($slug != 'index') { array_unshift($chain, $slug); } $result = $this->manager->call($controller, 'processChain', array('chain' => $chain)); } else { throw new Exception("Not found", 404); } } catch (\Exception $e) { if (!method_exists($controller, 'processException')) { throw $e; } $result = $this->manager->call($controller, 'processException', array($e, 'exception' => $e)); } echo $result; }
/** * @param $params */ public function __construct($params) { if (isset($params[0])) { // numeric keys switch (count($params)) { case 4: case 3: $params = array('name' => $params[0], 'type' => $params[1], 'comment' => $params[2]); break; default: throw new Exception("Error Processing property" . PHP_EOL . var_export($params, true)); } } $this->name = $params['name']; $this->comment = isset($params['comment']) ? $params['comment'] : null; $this->foreignName = isset($params['foreignName']) ? $params['foreignName'] : null; $this->required = isset($params['required']) ? $params['required'] : false; $this->primary = isset($params['primary']) ? $params['primary'] : false; $this->behaviour = isset($params['behaviour']) ? $params['behaviour'] : false; $this->relation = isset($params['relation']) ? $params['relation'] : null; $this->readonly = isset($params['readonly']) ? $params['readonly'] : $this->primary; if (isset($params['model'])) { $this->model = $params['model']; } if (isset($params['type'])) { $this->type = $params['type']; } else { if (substr($this->name, 0, 3) == 'dt_') { $this->type = 'datetime'; } elseif (substr($this->name, 0, 3) == 'id_') { $this->type = 'integer'; } elseif (substr($this->name, 0, 3) == 'is_') { $this->type = 'boolean'; } else { $this->type = 'string'; } } if (isset($params['setter'])) { $this->setter = $params['setter']; } else { $this->setter = 'set' . String::convertToCamelCase($this->name); } if (isset($params['getter'])) { $this->getter = $params['getter']; } else { $this->getter = 'get' . String::convertToCamelCase($this->name); } if (isset($params['min'])) { $this->min = $params['min']; } if (isset($params['max'])) { $this->max = $params['max']; } }
/** * process migrations from filesystem */ function processMigrations() { $filesystem = new Filesystem(); $project = $this->application->getProject(); $migrations = $project->getPath('build php Storage Migration'); if ($filesystem->exists($migrations)) { $filesystem->remove($migrations); } $filesystem->mkdir($migrations); $finder = new Finder(); if (!is_dir($project->getPath('resources php migrations'))) { return true; } $finder->files()->name("*.php")->in($project->getPath('resources php migrations')); foreach ($finder as $file) { $date = substr($file->getFileName(), 0, 8); $time = substr($file->getFileName(), 9, 6); $index = substr($file->getBasename('.php'), 16); $name = String::convertToCamelCase($index); $class_name = $name . '_' . $date . '_' . $time; $class = 'Storage\\Migration\\' . $class_name; $filesystem->copy($file->getRealPath(), $migrations . DIRECTORY_SEPARATOR . $class_name . '.php'); if (!class_exists($class)) { include $file->getRealPath(); } $this->application->getManager()->get($class)->process($this); $this->setNamespace(null); } }
public function renderRelation() { $model = $this->model; $property = $this->property; $relation = $property->getRelation(); $foreignModel = $this->schema->getModel($relation->getDestination()); $name = $relation->getDestinationAlias() ? $relation->getDestinationAlias() : $foreignModel->getName(); $getter = 'get' . String::convertToCamelCase($name); $setter = 'set' . String::convertToCamelCase($name); $clear = array(); $finder = array(); $reader = array(); foreach ($relation->getProperties() as $property) { $clear[] = "\$this->" . $property->getSetter() . '(null);'; $reader[] = sprintf("\$this->%s(\$%s->%s());", $property->getSetter(), $name, $foreignModel->getProperty($property->getForeignName())->getGetter()); $finder[] = "'" . $property->getForeignName() . "' => \$this->" . $property->getGetter() . '()'; } $finder = implode(',' . PHP_EOL . ' ', $finder); $reader = implode(PHP_EOL . ' ', $reader); $clear = implode(PHP_EOL . ' ', $clear); $foreign_model_class = $foreignModel->getModelClass(); $destination = $relation->getDestination(); $model_class = $model->getModelClass(); $foreign_class_name = $foreignModel->getClassName(); return <<<RELATION /** * Get {$name} * @return {$foreign_model_class} */ public function {$getter}() { if(!\$this->{$name}) { \$master = \$this->getRepository()->getMaster(); \$this->{$name} = \$master->findByPk('{$destination}', array( {$finder} )); } return \$this->{$name}; } /** * Set {$name} * @param {$foreign_model_class} \${$name} * @return {$model_class} */ public function {$setter}({$foreign_class_name} \${$name} = null) { {$clear} if(\${$name}) { {$reader} } return \$this; } RELATION; }