/** * handle method * * @param \Codex\Contracts\Menus\Menu|\Codex\Menus\Menu $menu */ public function handle(Menu $menu, Project $project = null) { $menu->setAttribute('title', $project === null ? 'Pick...' : $project->getDisplayName()); $menu->setAttribute('subtitle', 'project'); foreach ($this->codex->projects->all() as $project) { # Add to menu $name = (string) $project->config('display_name'); $names = []; if (strpos($name, ' :: ') !== false) { $names = explode(' :: ', $name); $name = array_shift($names); } $href = $project->url(); $metas = compact('project'); $id = Str::slugify($name, '_'); if (!$menu->has($id)) { $node = $menu->add($id, $name, 'root', count($names) === 0 ? $metas : [], count($names) === 0 ? compact('href') : []); } $parentId = $id; while (count($names) > 0) { $name = array_shift($names); $id .= '::' . $name; $id = Str::slugify($id, '_'); if (!$menu->has($id)) { $node = $menu->add($id, $name, $parentId, $metas, count($names) === 0 ? compact('href') : []); } $parentId = $id; } } }
/** * recurse method * * @param array $items * @param string $parentId */ protected function recurse($items = [], $parentId = 'root') { foreach ($items as $item) { $link = '#'; $type = null; if (array_key_exists('document', $item)) { $type = 'document'; // remove .md extension if present $path = ends_with($item['document'], ['.md']) ? Str::remove($item['document'], '.md') : $item['document']; $link = $this->codex->url($this->ref->getProject(), $this->ref->getName(), $path); } elseif (array_key_exists('href', $item)) { $type = 'href'; $link = $item['href']; } $id = md5($item['name'] . $link); $node = $this->menu->add($id, $item['name'], $parentId); $node->setAttribute('href', $link); $node->setAttribute('id', $id); if (isset($path)) { $node->setMeta('path', $path); } if (isset($item['icon'])) { $node->setMeta('icon', $item['icon']); } if (isset($item['children'])) { $type = 'parent'; } $node->setMeta('type', $type); if (isset($item['children'])) { $this->recurse($item['children'], $id); } } }
/** * callExtension method * * @private * * @param $name * @param $parameters * * @return mixed */ protected function callExtension($name, $parameters) { $callback = static::$extensions[$name]; if ($callback instanceof Closure) { return call_user_func_array($callback->bindTo($this, get_class($this)), $parameters); } elseif (is_string($callback) && Str::contains($callback, '@')) { return $this->callClassBasedExtension($callback, $parameters); } }
/** * Calls a macro extension (method) with the given parameters * * @internal * * @param string $name The name of the macro * @param array $parameters The optional parameters * * @return mixed */ protected function callMacro($name, $parameters = []) { $callback = static::getExtenableProperty('macros')[$name]; if ($callback instanceof Closure) { return call_user_func_array($callback->bindTo($this, get_class($this)), $parameters); } elseif (is_string($callback) && Str::contains($callback, '@')) { return $this->callClassBasedMacro($callback, $parameters); } }
/** * Very simple 'template' parser. Replaces (for example) {name} with the value of $vars['name'] in strings * * @param $str * @param array $vars * @param string $openDelimiter * @param string $closeDelimiter * * @return string * * @example * <?php * $result = Util::template('This is the best template parser. Created by {developerName}', ['developerName' => 'Radic']); * echo $result; // This is the best template parser. Created by Radic */ public static function template($str, array $vars = [], $openDelimiter = '{', $closeDelimiter = '}') { foreach ($vars as $k => $var) { if (is_array($var)) { $str = static::template($str, $var); } elseif (is_string($var)) { $str = Str::replace($str, $openDelimiter . $k . $closeDelimiter, $var); } } return $str; }
public function isAction($url) { return Str::startsWith(Uri::createFromString($url)->getFragment(), $this->config['needle'] . ':', true); }
/** * Transforms a package name (eg: codex-project/github-filesystem) into a namespace (eg: CodexProject\GithubFilesystem) * * @param $packageName * @return string */ protected function getPackageNamespace($packageName) { return Str::namespacedStudly($packageName); }
/** * addStylesheet method * * @param $name * @param $src * @param array $depends * @param bool $external * * @return static */ public function addStylesheet($name, $src, array $depends = [], $external = false) { $src = Str::ensureRight($src, '.css'); $src = $external ? $src : asset($src); $this->stylesheets->put($name, compact('src', 'depends', 'external', 'name')); return $this; }
protected function addPlugin($name) { if ($this->isValidPlugin($name) && false === $this->theme->javascripts()->has($key = "prism-{$name}")) { $this->theme->addJavascript($key, $this->getPluginPath($name), ['prism']); if (file_exists(public_path($this->getPluginPath($name) . '.css'))) { $this->theme->addStylesheet($key, $this->getPluginPath($name), ['prism']); } $method = Str::camelize("add-{$name}-plugin"); if (static::hasMacro($method) || method_exists($this, $method)) { $this->{$method}($name); } } }
public function testCanGetBaseClass() { $this->assertEquals('Baz', Str::baseClass('Foo\\Bar\\Baz')); }