/**
  * @return string path
  */
 public function getImagePath()
 {
     $file = preg_replace_callback('~[A-Z]~', function ($m) {
         return '.' . strToLower($m[0]);
     }, lcFirst($this->badge->key));
     return "/images/badges/{$file}.svg";
 }
Beispiel #2
0
 /**
  * Invokes _getProperty() for getProperty().
  * Caches return values.
  * @param string $method
  * @param array $args
  * @return mixed
  */
 public function __call($method, array $args)
 {
     if (isset($this->cache[$method])) {
         return $this->cache[$method];
     }
     if (strpos($method, 'get') === 0) {
         $property = lcFirst(substr($method, strlen('get')));
         $getter = "_{$method}";
         if (method_exists($this, $getter)) {
             $query = [];
             if ($this instanceof Entity) {
                 $query['entityId'] = $this->id;
                 $html = $this->request($this->getUrl($this->getUrlKey($property), $query))->getContent();
                 array_unshift($args, $html);
             }
             $res = call_user_func_array([$this, $getter], $args);
             $this->cache[$method] = $res;
             return $res;
         } else {
             if (method_exists($this, '_get')) {
                 array_unshift($args, $property);
                 $res = call_user_func_array([$this, '_get'], $args);
                 $this->cache[$method] = $res;
                 return $res;
             }
         }
     }
     $class = get_class($this);
     throw new InternalException("Call to undefined method {$class}::{$method}.");
 }
Beispiel #3
0
 protected function renderTest(array $data)
 {
     $conf = $data['config']['namespace'];
     $mode = 'default';
     if (array_key_exists('mode', $conf)) {
         $mode = $conf['mode'];
     }
     $array = explode('\\', $data['class']);
     $data['classShortName'] = end($array);
     $data['objectShortName'] = lcFirst($data['classShortName']);
     array_pop($array);
     $data['classNamespace'] = implode('\\', $array);
     $data['mainNamespace'] = $array[1];
     $array[0] = $array[0] . '\\Tests';
     if ('bundle' === $mode) {
         array_shift($array);
     } else {
         array_shift($array);
         array_shift($array);
     }
     $data['testDirectory'] = 'tests/' . implode('/', $array);
     $data['testNamespace'] = implode('\\', $array);
     $data['asserts'] = $this->renderAsserts($data);
     $data['expected'] = $this->renderExpected($data);
     $data['filename'] = $data['testDirectory'] . '/' . $data['classShortName'] . 'Test.php~';
     $data['content'] = $this->render($data, 'testCase');
     if (array_key_exists('testcase', $conf)) {
         $tc = $conf['testcase'];
     }
     $data['testcase'] = empty($tc) ? '\\PHPUnit_Framework_TestCase' : $tc;
     if (false === strpos($data['testcase'], 'TestCaseAbstract')) {
         $data['testcase'] .= ' as TestCaseAbstract';
     }
     return $data;
 }
 public function getFromArray(ClassEntity $entity)
 {
     $an = preg_match("/^[AEIOU]/i", $entity->getName()) ? 'an' : 'a';
     $code = array();
     $code[] = sprintf('%s/**', $this->getIndentation());
     $code[] = sprintf('%s * Create %s %s from an array', $this->getIndentation(), $an, $entity->getName());
     $code[] = sprintf('%s *', $this->getIndentation(), $entity->getName());
     $code[] = sprintf('%s * @param array $data The array to convert to an %s', $this->getIndentation(), $entity->getName());
     $code[] = sprintf('%s * @return %s The %s', $this->getIndentation(), $entity->getName(), $entity->getName());
     $code[] = sprintf('%s */', $this->getIndentation());
     $code[] = sprintf('%sfunction fromArray(array $data)', $this->getIndentation());
     $code[] = sprintf('%s{', $this->getIndentation());
     // body
     $code[] = sprintf('%s$%s = new %s();', $this->getIndentation(2), lcfirst($entity->getName()), $entity->getName());
     $code[] = '';
     $members = $entity->getMembers();
     foreach ($members as $member) {
         if ($member->getType() != 'property') {
             continue;
         }
         $code[] = sprintf("%sif (isset(\$data['%s'])) {", $this->getIndentation(2), $member->getName());
         $code[] = sprintf("%s\$%s->set%s(\$data['%s']);", $this->getIndentation(3), lcfirst($entity->getName()), ucfirst($member->getName()), $member->getName());
         $code[] = sprintf("%s}", $this->getIndentation(2));
         $code[] = '';
     }
     $code[] = sprintf('%sreturn $%s;', $this->getIndentation(2), lcFirst($entity->getName()));
     // end body
     $code[] = sprintf('%s}', $this->getIndentation());
     return $code;
 }
 protected function getTemplateFile($view = NULL)
 {
     $name = lcFirst($this->getReflection()->getShortName());
     if ($view && $view !== 'default') {
         $name .= ".{$view}";
     }
     return __DIR__ . "/../templates/controls/{$name}.latte";
 }
 /**
  * Ns\FooBar => foo-bar
  * @return string
  */
 public function getShortEntityName()
 {
     $fqn = $this->repository->getEntityClassName();
     $class = substr($fqn, strrpos($fqn, '\\') + 1);
     return preg_replace_callback('~[A-Z]~', function ($c) {
         return '-' . strToLower($c);
     }, lcFirst($class));
 }
Beispiel #7
0
 public function writeArticleTextSmall(StreamInterface $stream, Article $article, $show_metainfo, $show_edit_delete_links)
 {
     $text = $this->text;
     $stream->write("\n\n" . '<article class="article_teaser ');
     if (!empty($article->featuredImage)) {
         $stream->write("with_featured_image");
     }
     $stream->write('">');
     // Title
     $titleHtml = $text->e($article->getTitle());
     $stream->write("<header>");
     $stream->write("<h3>" . $this->encloseInArticleLink($article, $titleHtml) . "</h3>");
     if ($show_metainfo) {
         $stream->write('<p class="meta">');
         // Created and last edited
         $stream->write($text->t('articles.created') . " " . $text->formatDateTime($article->getDateCreated()) . ' - ');
         if ($article->getDateLastEdited()) {
             $stream->write(lcFirst($text->t('articles.last_edited')) . " " . $text->formatDateTime($article->getDateLastEdited()) . '<br />');
         }
         // Category
         $stream->write($text->t('main.category') . ": ");
         $stream->write('<a href="' . $text->e($text->getUrlPage("category", $article->categoryId)) . '">');
         $stream->write($text->e($article->category) . '</a>');
         // Author
         $stream->write(" - " . $text->t('articles.author') . ": ");
         $stream->write('<a href="' . $text->e($text->getUrlPage("account", $article->authorId)) . '">');
         $stream->write($text->e($article->author) . "</a>");
         // Pinned
         if ($article->pinned) {
             $stream->write(" - " . $text->t('articles.pinned'));
         }
         // Hidden
         if ($article->isHidden()) {
             $stream->write(" - " . $text->t('articles.hidden'));
         }
         $stream->write('</p>');
     }
     $stream->write("</header>");
     // Featured image
     if (!empty($article->featuredImage)) {
         $imageHtml = '<img src="' . htmlSpecialChars($article->featuredImage) . '" alt="' . htmlSpecialChars($article->getTitle()) . '" />';
         $stream->write($this->encloseInArticleLink($article, $imageHtml));
     }
     // Intro
     $stream->write('<p class="article_teaser_text">');
     $stream->write($this->encloseInArticleLink($article, htmlSpecialChars($article->getIntro())));
     $stream->write('</p>');
     $stream->write('<footer class="article_teaser_links"><p>');
     // Edit and delete links
     $stream->write('<a class="arrow" href="' . $text->e($text->getUrlPage("article", $article->getId())) . '">' . $text->t('main.read') . '</a>');
     if ($show_edit_delete_links) {
         $stream->write('<a class="arrow" href="' . $text->e($text->getUrlPage("edit_article", $article->getId())) . '">' . $text->t('main.edit') . '</a>' . '<a class="arrow" href="' . $text->e($text->getUrlPage("delete_article", $article->getId())) . '">' . $text->t('main.delete') . '</a>');
     }
     $stream->write("</p></footer>");
     $stream->write('<p style="clear:both"></p>');
     $stream->write("</article>");
 }
 public function generateArrayCreation(ClassEntity $entity)
 {
     $code = array();
     $code[] = sprintf('$%s = array(', lcFirst($entity->getName()));
     $members = $entity->getMembers();
     foreach ($members as $member) {
         if ($member->getType() != 'property') {
             continue;
         }
         $code[] = sprintf("%s'%s' => %s,", $this->getIndentation(), $member->getName(), preg_match('|' . preg_quote('[]') . '|', $member->getMemberType()) ? 'array()' : "''");
     }
     $code[] = ');';
     return implode("\n", $code);
 }
Beispiel #9
0
 public function invoke(Scaffolding $scaffolding)
 {
     $name = ucFirst($this->in->getArgument('entityName'));
     $file = $scaffolding->createRepository($name);
     $this->writeCreatedFilesHeader();
     $this->writeCreatedFile($file);
     $file = $scaffolding->createMapper($name);
     $this->writeCreatedFile($file);
     $params = [];
     foreach ($this->in->getVariadics() as $arg) {
         list($param, $type) = explode(':', $arg) + [NULL, 'mixed'];
         $params[$param] = $type;
     }
     $file = $scaffolding->createEntity($name, $params);
     $this->writeCreatedFile($file);
     $this->out->writeln("\n<comment>Don't forget to add repository to your Model class</comment>");
     $plural = Inflect::pluralize($name);
     $repoClass = 'Rme\\' . ucFirst($plural) . 'Repository';
     $param = lcFirst($plural);
     $this->out->writeln(" * @property-read {$repoClass} \${$param}");
 }
Beispiel #10
0
 private function eagerLoadRelations($relations, &$results)
 {
     if (sizeof($results) > 0) {
         foreach ($relations as $relation) {
             if (!array_key_exists($relation, $this->relations)) {
                 throw new Exception("Datamapper misconfiguration. Relation '" . $relation . "' not defined in datamapper.");
             }
             $mapper = $relation . "Mapper";
             $relationMapper = new $mapper($this->database);
             foreach ($this->relations as $relationKey => $relationValue) {
                 if ($relation === $relationKey) {
                     $relationType = isset($relationValue['type']) ? $relationValue['type'] : "";
                     if ($relationType === "single") {
                         $relationAlias = isset($relationValue['alias']) ? $relationValue['alias'] : lcFirst($relationKey);
                     } else {
                         if ($relationType === "multiple") {
                             $relationAlias = isset($relationValue['alias']) ? $relationValue['alias'] : lcFirst($relationKey) . "s";
                         }
                     }
                 }
             }
             if (empty($relationType)) {
                 throw new Exception("Datamapper error: relation type expected");
             }
             $pkCount = 0;
             foreach ($relationMapper->dbfields as $dbfield => $val) {
                 if (isset($val["primary"])) {
                     $pk = $dbfield;
                     $pkCount++;
                 }
             }
             if ($pkCount > 1 || !isset($pk)) {
                 $pk = "id";
             }
             $relationValues = array();
             //load single entity relations
             if ($relationType === "single") {
                 //build the search for related entries
                 $mapper = ucfirst(strtolower($relation)) . "Mapper";
                 $mapper = new $mapper($this->database);
                 $key = $mapper->datasource . "_" . $mapper->findPk();
                 $values = explode("_", $key);
                 $field = "";
                 foreach ($values as $value) {
                     $field .= ucfirst($value);
                 }
                 $field = lcfirst($field);
                 foreach ($results as $entry) {
                     if (!in_array($entry->{$field}, $relationValues)) {
                         $relationValues[] = $entry->{$field};
                     }
                 }
                 $firstEntry = $pk . " in (";
                 foreach ($relationValues as $t) {
                     $firstEntry .= "?,";
                 }
                 $firstEntry = rtrim($firstEntry, ",");
                 $firstEntry .= ")";
                 array_unshift($relationValues, $firstEntry);
                 $related = $this->database->findMany($relationMapper->datasource, array("conditions" => $relationValues));
                 if (empty($related)) {
                     throw new Exception("Datamapper error: key to single entity not found, check your datamapper configuration");
                 }
                 foreach ($results as $key => $value) {
                     foreach ($related as $struct) {
                         if ($value->{$field} == $struct->{$pk}) {
                             $value->{$relationAlias} = $relationMapper->toEntity($struct);
                             break;
                         }
                     }
                 }
             }
             //load possible multi entity relations
             if ($relationType === "multiple") {
                 $field = $this->parseFk($this);
                 $firstEntry = $field . " in (";
                 foreach ($results as $entry) {
                     if (!in_array($entry->{$pk}, $relationValues)) {
                         $firstEntry .= "?,";
                         $relationValues[] = $entry->{$pk};
                     }
                 }
                 $firstEntry = rtrim($firstEntry, ",");
                 $firstEntry .= ")";
                 array_unshift($relationValues, $firstEntry);
                 $related = $this->database->findMany($relationMapper->datasource, array("conditions" => $relationValues));
                 foreach ($results as $key => $value) {
                     $resultArray[$value->{$pk}] = array();
                     foreach ($related as $struct) {
                         if ($value->{$pk} == $struct->{$field}) {
                             $resultArray[$value->{$pk}][] = $relationMapper->toEntity($struct);
                         }
                     }
                     $value->{$relationAlias} = $resultArray[$value->{$pk}];
                 }
             }
         }
     }
 }
Beispiel #11
0
 /**
  * Looks at $this->route to load the appropriate controller
  * Works its way through URI nodes until it finds a controller with the name of that node.
  * If a file exists in the controllers directory by that name, includes the file and returns 
  * Otherwise, if a directory exists inside the controllers directory by this name, It looks in there.
  * If, inside the directory by this node's name, there is an Index.php, it loads that.
  * Otherwise, it tries to load a file by the name of the next uri node's name.
  * If there is not an index file, or a file by the node's name, it looks into a folder by that node's name and proceeds.
  * Note that folders must be named in lowerCamelCase, and files must be named in UpperCamelCase -- otherwise your site will die on linux servers
  */
 public function loadController()
 {
     // The first URI node
     $controller = $this->route->getControl();
     // The Directory with controllers in it
     $dir = APP_HOME . $this->config->dir->controllers;
     // Maybe we're at website.com/hello/. Check the directory for a Hello.php
     $file = $dir . ucFirst($controller) . '.php';
     $file = str_replace('-', '_', $file);
     $triedFiles = array();
     // This loop will be broken out from once we find the file. Otherwise it will throw an error.
     while (true) {
         $triedFiles[] = $file;
         if (is_file($file)) {
             break;
         } else {
             if (is_dir($dir)) {
                 // Now check for a directory by this node's name. Directories are all lowerCamelCase
                 $dir .= lcFirst($controller) . _DS_;
                 // Update the controller to point to the next URI node
                 $this->router->advancePointer();
                 $controller = $this->route->getControl();
                 // If we are on the final node (controller is null) then Check this directory for an Index.php
                 $indexFile = $dir . lcFirst($controller) . _DS_ . 'Index.php';
                 if (!$controller && is_file($indexFile)) {
                     $this->router->advancePointer();
                     $this->route->setControl('Index');
                     $dir .= lcFirst($controller) . _DS_;
                     $file = $dir . 'Index.php';
                     break;
                 }
                 // Update the file to look for a file the new node's name in the directory
                 $file = $dir . ucFirst($controller) . '.php';
             } else {
                 $files = implode("<br>", $triedFiles);
                 throw new MvcException(MvcException::INVALID_FILE, "Unable to load controller. <br>None of the following files exist:<br>{$files}");
             }
         }
     }
     include $file;
 }
 public function lcFirst($text)
 {
     return lcFirst($text);
 }
 /**
  * @return string
  * Foo\SelectExercise => select
  */
 public function getTemplateName()
 {
     $parts = explode('\\', static::class);
     $last = $parts[count($parts) - 1];
     return lcFirst(substr($last, 0, -strlen('Exercise')));
 }
 /**
  * Create new model
  * @return \Sebk\SmallOrmBundle\Dao\modelClass
  */
 public function newModel()
 {
     $modelClass = $this->modelNamespace . "\\" . $this->modelName;
     $primaryKeys = array();
     foreach ($this->primaryKeys as $primaryKey) {
         $primaryKeys[] = lcfirst($primaryKey->getModelName());
     }
     $fields = array();
     foreach ($this->fields as $field) {
         $fields[] = lcfirst($field->getModelName());
     }
     $toOnes = array();
     foreach ($this->toOne as $toOneAlias => $toOne) {
         $toOnes[] = lcFirst($toOneAlias);
     }
     $toManys = array();
     foreach ($this->toMany as $toManyAlias => $toMany) {
         $toManys[] = lcFirst($toManyAlias);
     }
     $model = new $modelClass($this->modelName, $this->modelBundle, $primaryKeys, $fields, $toOnes, $toManys, $this->container);
     foreach ($this->defaultValues as $property => $defaultValue) {
         $method = "set" . $property;
         $model->{$method}($defaultValue);
     }
     return $model;
 }
 protected function defaultControlName(Controls\BaseControl $control, ClassMetadata $class, array $mapping)
 {
     return 'entity.' . lcFirst($class->getReflectionClass()->getShortName()) . '.' . $mapping['fieldName'];
 }
 /**
  * Arbitrary identifier such as mysql, postgres, elastic, neo4j
  *
  * @return string
  */
 public function getStorageType()
 {
     $class = get_class($this->event->connection->driver);
     $name = preg_replace('~^Dibi|Driver$~', '', $class);
     return lcFirst($name);
 }
Beispiel #17
0
 /**
  * Getter
  *
  * @param $name
  *
  * @return mixed
  */
 public function __get($name)
 {
     $name = '_' . lcFirst($name);
     return $this->{$name};
 }
 protected function getTemplateFile($view = NULL)
 {
     $name = lcFirst(ClassType::from($this->formClass)->getShortName());
     return __DIR__ . "/../templates/forms/{$name}.latte";
 }