protected function getContentType($filePath) { $extension = \UString::substrAfterLast($filePath, '.'); if (in_array($extension, ['html', 'css'])) { $contentType = 'text/' . $extension; } else { if ($extension === 'js') { $contentType = 'text/javascript'; } else { if ($extension === 'ico') { $contentType = 'image/png'; } else { try { $finfo = finfo_open(FILEINFO_MIME_TYPE); $contentType = finfo_file($finfo, $filePath); finfo_close($finfo); } catch (\Exception $e) { $contentType = 'application/octet-stream'; } } } } return $contentType; }
protected function fetchFileData($id, $filePath) { $data = []; $data[$this->idField] = $id; $extension = \UString::substrAfterLast(basename($filePath), '.'); if ($extension == 'json') { $json = json_decode(file_get_contents($filePath), TRUE); if (is_array($json)) { $data = array_merge($data, $json); } } else { if ($extension == 'md') { $data['content'] = MarkdownExtra::defaultTransform(file_get_contents($filePath)); } else { if ($extension == 'html') { $data['content'] = file_get_contents($filePath); } else { if ($extension == 'php') { ob_start(); include $filePath; $content = ob_get_contents(); ob_end_clean(); $data['content'] = $content; } else { if ($extension == 'txt') { $data['content'] = str_replace(PHP_EOL, HTML_EOL, htmlentities(file_get_contents($filePath))); } } } } } return $data; }
public static function getClassName($class) { \UObject::doConvertToClass($class); return \UString::substrAfterLast($class, '\\'); }
public function test_substr_after_last__no_match() { $substr = \UString::substrAfterLast('example.com', '/'); $this->assertEquals('example.com', $substr); }
public static function doSubstrAfterLast(&$haystack, $needles) { $substr = \UString::substrAfterLast($haystack, $needles); $pop = substr($haystack, 0, strlen($haystack) - strlen($substr)); $haystack = $substr; return $pop; }
protected function initialize() { /* Id */ if (substr_count($this->id, '/') == 2) { $this->section = \UString::substrBefore($this->id, '/'); $this->subsection = \UString::substrBeforeLast(\UString::substrAfter($this->id, '/'), '/'); $this->shortTitle = \UString::substrAfterLast($this->id, '/'); } /* Retreat content */ if (trim($this->content)) { $doc = new \DOMDocument(); @$doc->loadHTML('<?xml encoding="UTF-8">' . $this->content); /* H1 */ $h1List = $doc->getElementsByTagName('h1'); if ($h1List->length) { $this->title = $h1List->item(0)->textContent; } foreach ($h1List as $h1) { $h1->parentNode->removeChild($h1); } /* H2 */ $h2List = $doc->getElementsByTagName('h2'); foreach ($h2List as $h2) { $title = $h2->textContent; if ($h2->hasAttribute('id')) { $id = $h2->getAttribute('id'); } else { $id = \UString::stripSpecialChar($title); $h2->setAttribute('id', $id); } $this->summary[$id] = $title; $h2->nodeValue = ''; $link = $doc->createElement('a'); $link->nodeValue = htmlentities($title); $link->setAttribute('href', '#' . $id); $h2->appendChild($link); } /* Image */ $imageList = $doc->getElementsByTagName('img'); if ($imageList->length) { $image = $imageList->item(0); if ($image->hasAttribute('src')) { $this->image = $image->getAttribute('src'); } } /* Intro */ $ps = $doc->getElementsByTagName('p'); if ($ps->length) { $firstP = $ps->item(0); $this->intro = $firstP->textContent; $this->introHTML = $doc->saveHTML($firstP); $firstP->parentNode->removeChild($firstP); } /* Content */ $stripHTML = ['/^\\<\\!DOCTYPE.*?<html><body>/si', '!</body></html>$!si']; $this->content = preg_replace($stripHTML, '', $doc->saveHTML()); } if (!$this->label) { $this->label = $this->title; } }
public static function getFileExtension($path) { return \UString::substrAfterLast($path, '.'); }