resolvePath() public method

('@JarvesBundle/test.png', 'Resources/public/') => /var/www/jarves/src/Jarves/Resources/public/test.png ('@JarvesBundle/test.png') => /var/www/jarves/src/Jarves/test.png ('images/test.png') => /var/www/jarves/images/webtest.png
public resolvePath ( string $path, string $suffix = '', boolean $relativePath = false ) : string
$path string
$suffix string
$relativePath boolean
return string without trailing slash when relative
Ejemplo n.º 1
0
 /**
  * @param string $view
  *
  * @return mixed
  *
  * @throws FileNotFoundException
  * @throws \Jarves\Exceptions\BundleNotFoundException
  */
 public function getViewMTime($view)
 {
     $view = $this->jarves->resolvePath($view, 'Resources/views/');
     if (!file_exists($view)) {
         throw new FileNotFoundException(sprintf('File `%s` not found.', $view));
     }
     return filemtime($view);
 }
Ejemplo n.º 2
0
 /**
  * 
  * Options:
  * 
  *  //whether the template cache is deactivated. Navigation object is still cached
  *  boolean noCache = false
  *
  *  //whether the pathInfo is used in the cacheKey instead of the currentUrl.
  *  //Useful when you have in your navigation controller calls that are based on pathInfo like
  *  //pageStack->getCurrentUrlAffix()
  *  boolean pathInfoCache = false
  * 
  * Example:
  *   getRendered(['noCache' => true]);
  * 
  * @param array $options
  * @param \Twig_Environment $twig
  * @return string
  * @throws \Exception
  */
 public function getRendered($options, \Twig_Environment $twig)
 {
     $options['noCache'] = isset($options['noCache']) ? (bool) $options['noCache'] : false;
     $options['pathInfoCache'] = isset($options['pathInfoCache']) ? (bool) $options['pathInfoCache'] : false;
     $view = $options['template'] ?: $options['view'];
     $cacheKey = 'core/navigation/' . $this->pageStack->getCurrentPage()->getDomainId() . '.' . $this->pageStack->getCurrentPage()->getId() . ($options['pathInfoCache'] ? '_' . md5($this->pageStack->getRequest()->getPathInfo()) : '') . '_' . md5(json_encode($options));
     $fromCache = false;
     $viewPath = $this->jarves->resolvePath($view, 'Resources/views/');
     if ('@' === $view[0]) {
         $view = substr($view, 1);
     }
     if (!file_exists($viewPath)) {
         throw new \Exception(sprintf('View `%s` not found.', $view));
     } else {
         $mtime = filemtime($viewPath);
     }
     if (!$options['noCache']) {
         $cache = $this->cacher->getDistributedCache($cacheKey);
         if ($cache && isset($cache['html']) && $cache['html'] !== null && $cache['mtime'] == $mtime) {
             return $cache['html'];
         }
     }
     $cache = $this->cacher->getDistributedCache($cacheKey);
     if ($cache && isset($cache['object']) && $cache['mtime'] == $mtime) {
         $navigation = unserialize($cache['object']);
         $fromCache = true;
     } else {
         $navigation = $this->get($options);
     }
     $data['navigation'] = $navigation ?: false;
     if ($navigation !== false) {
         $html = $twig->render($view, $data);
         if (!$options['noCache']) {
             $this->cacher->setDistributedCache($cacheKey, array('mtime' => $mtime, 'html' => $html));
         } elseif (!$fromCache) {
             $this->cacher->setDistributedCache($cacheKey, array('mtime' => $mtime, 'object' => serialize($navigation)));
         }
         return $html;
     }
     //no navigation found, probably the template just uses the breadcrumb
     return $twig->render($view, $data);
 }
Ejemplo n.º 3
0
 /**
  * @param string $lang
  * @param bool $force
  * @return array|null|string
  *
  * @throws \Jarves\Exceptions\BundleNotFoundException
  */
 public function loadMessages($lang = 'en', $force = false)
 {
     if (!$this->isValidLanguage($lang)) {
         $lang = 'en';
     }
     if ($this->messages && isset($this->messages['__lang']) && $this->messages['__lang'] == $lang && $force == false) {
         return null;
     }
     if (!$lang) {
         return null;
     }
     $code = 'core/lang/' . $lang;
     $this->messages = $this->cacher->getFastCache($code);
     $md5 = '';
     $bundles = array();
     foreach ($this->jarves->getBundles() as $bundleName => $bundle) {
         $path = $bundle->getPath();
         if ($path) {
             $path .= "/Resources/translations/{$lang}.po";
             if (file_exists($path)) {
                 $md5 .= @filemtime($path);
                 $bundles[] = $bundleName;
             }
         }
     }
     $md5 = md5($md5);
     if (!$this->messages || count($this->messages) == 0 || !isset($this->messages['__md5']) || $this->messages['__md5'] != $md5) {
         $this->messages = array('__md5' => $md5, '__plural' => $this->getPluralForm($lang), '__lang' => $lang);
         foreach ($bundles as $key) {
             $file = $this->jarves->resolvePath("@{$key}/{$lang}.po", 'Resources/translations');
             $po = $this->getLanguage($file);
             $this->messages = array_merge($this->messages, $po['translations']);
         }
         $this->cacher->setFastCache($code, $this->messages);
     }
     include_once $this->getPluralPhpFunctionFile($lang);
     return $this->messages;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritDoc}
  */
 public function getBranch($pk = null, Condition $condition = null, $depth = 1, $scope = null, $options = null)
 {
     $result = null;
     $path = $pk['path'];
     if ($depth === null) {
         $depth = 1;
     }
     if ($path) {
         $path = '@' . trim($path, '/@');
         $path = str_replace(':', '/', $path);
     }
     $c = 0;
     $offset = $options['offset'];
     $limit = $options['limit'];
     $result = array();
     if (!$path) {
         $result = array();
         $bundles = array_keys($this->jarves->getBundles());
         foreach ($bundles as $bundleName) {
             $directory = $this->jarves->resolvePath('@' . $bundleName, 'Resources/views', true);
             if (!$this->localFilesystem->has($directory)) {
                 continue;
             }
             $file = $this->localFilesystem->getFile($directory);
             if (!$file) {
                 $result[] = $directory;
                 continue;
             }
             $file = $file->toArray();
             $file['name'] = $bundleName;
             $file['path'] = $bundleName;
             if ($offset && $offset > $c) {
                 continue;
             }
             if ($limit && $limit < $c) {
                 continue;
             }
             if ($condition && $condition->hasRules() && !$this->conditionOperator->satisfy($condition, $file)) {
                 $result[] = $directory;
                 continue;
             }
             $c++;
             if ($depth > 0) {
                 $children = self::getBranch(array('path' => $bundleName), $condition, $depth - 1);
                 $file['_childrenCount'] = count($children);
                 if ($depth > 1 && $file['type'] == 'dir') {
                     $file['_children'] = $children;
                 }
             }
         }
     } else {
         if (!($bundleName = $this->jarves->getBundleFromPath($path))) {
             return [];
         }
         $directory = $this->jarves->resolvePath($path, 'Resources/views', true) . '/';
         if (!$this->localFilesystem->has($directory)) {
             return [];
         }
         $files = $this->localFilesystem->getFiles($directory);
         foreach ($files as $file) {
             $item = $file->toArray();
             if ($condition && $condition->hasRules() && !$this->conditionOperator->satisfy($condition, $item, 'jarves/file')) {
                 continue;
             }
             $c++;
             if ($offset && $offset >= $c) {
                 continue;
             }
             if ($limit && $limit < $c) {
                 continue;
             }
             $item = array('name' => $this->buildPath($path . '/' . Tools::getRelativePath($item['path'], $directory)), 'path' => $this->buildPath($path . '/' . Tools::getRelativePath($item['path'], $directory)));
             if ($file->isDir()) {
                 $children = self::getBranch(array('path' => $item['path']), $condition, $depth - 1);
                 foreach ($children as $child) {
                     //                        $child['name'] = $item['name'] . '/' . $child['name'];
                     $result[] = $child;
                 }
             }
             if ($file->isFile()) {
                 $result[] = $item;
             }
         }
     }
     return $result;
 }
Ejemplo n.º 5
0
 /**
  * @param bool $withoutObjectCheck
  *
  * @throws ObjectNotFoundException
  */
 public function initialize($withoutObjectCheck = false)
 {
     if ($this->objectDefinition) {
         return;
     }
     if (!$this->getObject()) {
         return;
     }
     $this->objectDefinition = $this->objects->getDefinition($this->getObject());
     if (!$this->objectDefinition && $this->getObject() && !$withoutObjectCheck) {
         throw new ObjectNotFoundException("Can not find object '" . $this->getObject() . "'");
     }
     if ($this->objectDefinition) {
         if ($apiControllerDefinition = $this->objectDefinition->getApiControllerDefinition()) {
             $path = $this->jarves->resolvePath($apiControllerDefinition);
             $definitionContent = file_get_contents($path);
             $yaml = new Parser();
             $parsedDefinition = $yaml->parse($definitionContent);
             if ($parsedDefinition) {
                 foreach ($parsedDefinition as $key => $val) {
                     $setter = 'set' . ucfirst($key);
                     if (method_exists($this, $setter)) {
                         $this->{$setter}($val);
                     }
                 }
             }
         }
         $this->asNested = $this->objectDefinition->getNested();
         if (!$this->table) {
             $this->table = $this->objectDefinition->getTable();
         }
         if (!$this->fields) {
             $this->fields = [];
             foreach ($this->objectDefinition->getFields() as $field) {
                 if (!$field->isAutoIncrement()) {
                     $this->fields[] = $field;
                 }
             }
         }
         if (!$this->columns) {
             foreach ($this->fields as $field) {
                 if ($field->isPrimaryKey() || $field->isAutoIncrement()) {
                     continue;
                 }
                 if ('object' !== $field->getType()) {
                     $this->columns[$field->getId()] = $field;
                 }
             }
             if ($labelField = $this->objectDefinition->getLabelField()) {
                 $field = $this->objectDefinition->getField($labelField);
                 $this->columns[$field->getId()] = $field;
             }
         }
         //we need to call it, no matter if it's already defined, because of multiLanguage field.
         $this->prepareFieldItem($this->fields);
         $this->translateFields($this->fields);
         $this->translateFields($this->columns);
         if (!isset($this->titleField)) {
             $this->titleField = $this->objectDefinition->getLabel();
         }
     } else {
         //resolve shortcuts
         if ($this->fields) {
             $this->prepareFieldDefinition($this->fields);
             $this->convertToFieldObjects($this->fields);
             $this->prepareFieldItem($this->fields);
             $this->translateFields($this->fields);
         }
         if ($this->columns) {
             $this->prepareFieldDefinition($this->columns);
             $this->convertToFieldObjects($this->columns);
             $this->translateFields($this->columns);
         }
     }
     $this->ensureLanguageField();
     $this->fields = $this->toIdIndex($this->fields);
     $this->columns = $this->toIdIndex($this->columns);
     if ($this->addMultipleFields) {
         $this->prepareFieldDefinition($this->addMultipleFields);
         $this->convertToFieldObjects($this->addMultipleFields);
         $this->translateFields($this->addMultipleFields);
     }
     if ($this->addMultipleFixedFields) {
         $this->prepareFieldDefinition($this->addMultipleFixedFields);
         $this->convertToFieldObjects($this->addMultipleFixedFields);
         $this->translateFields($this->addMultipleFixedFields);
     }
     if (is_string($this->primary)) {
         $this->primary = explode(',', str_replace(' ', '', $this->primary));
     }
     if (!$this->order || count($this->order) == 0) {
         /* compatibility */
         $this->orderByDirection = strtolower($this->orderByDirection) == 'asc' ? 'asc' : 'desc';
         if ($this->orderBy) {
             $this->order = array($this->orderBy => $this->orderByDirection);
         }
     }
     if ((!$this->order || count($this->order) == 0) && $this->columns) {
         reset($this->columns);
         $field = current($this->columns);
         if ($field instanceof Field) {
             $this->order[$field->getId()] = 'asc';
         }
     }
     //normalize order array
     if (count($this->order) > 0 && is_numeric(key($this->order))) {
         $newOrder = array();
         foreach ($this->order as $order) {
             $newOrder[$order['field']] = $order['direction'];
         }
         $this->order = $newOrder;
     }
     $this->filterFields = array();
     if ($this->filter) {
         foreach ($this->filter as $key => $val) {
             if (is_numeric($key)) {
                 //no special definition
                 $fieldKey = $val;
                 $field = $this->fields[$val];
             } else {
                 $field = $val;
                 $fieldKey = $key;
             }
             $this->prepareFieldItem($field);
             $this->filterFields[$fieldKey] = $field;
         }
     }
     if (!$this->primary) {
         $this->primary = array();
         if ($this->objectDefinition) {
             foreach ($this->objectDefinition->getPrimaryKeys() as $sfield) {
                 $this->primary[] = $sfield->getId();
             }
         }
     }
     $this->translate($this->nestedRootAddLabel);
     $this->translate($this->newLabel);
 }