xf = \XoopsFile::getHandler(); } /** * @public static function getInstance * * @return Modulebuilder\Files\CreateFile */ public static function getInstance() { static $instance = false; if (!$instance) { $instance = new self(); } return $instance; } /** * @public function create * * @param $moduleDirname * @param $subdir * @param $fileName * @param string $content * @param $created * @param $notCreated * @param string $mode */ public function create($moduleDirname, $subdir = null, $fileName = null, string $content = '', $created = null, $notCreated = null, string $mode = 'w+'): void { $this->setFileName($fileName); $this->created = $created; $this->notCreated = $notCreated; $this->setMode($mode); $this->setRepositoryPath($moduleDirname); if (!empty($content) && \is_string($content)) { $this->setContent($content); } if (isset($subdir) && \is_string($subdir)) { $this->setSubDir($subdir); } } /** * @public function write * @param mixed $moduleDirname */ /*public function write($module, $fileName) { $this->setModule($module); $this->setFileName($fileName); }*/ /** * @private function setRepositoryPath * @param string $moduleDirname */ private function setRepositoryPath(string $moduleDirname): void { $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH . '/' . $moduleDirname; } /** * @private function getRepositoryPath * * @return string */ private function getRepositoryPath() { return $this->uploadPath; } /** * @private function setSubDir * @param $subdir */ private function setSubDir($subdir): void { $this->subdir = $subdir; } /** * @private function getSubDir * * @return string */ private function getSubDir() { return $this->subdir; } /** * public function setFileName. * * @param $fileName */ public function setFileName($fileName): void { $this->fileName = $fileName; } /** * @public function getFileName * * @return mixed */ public function getFileName() { return $this->fileName; } /** * @private function setContent * @param $content */ private function setContent($content): void { //replace tabs by 4 spaces $this->content = \preg_replace('/\t/', $this->tab, $content); } /** * @private function setContent * * @return string */ private function getContent() { return $this->content; } /** * @private function getFolderName * * @return string */ private function getFolderName() { $path = $this->getUploadPath(); if (\mb_strrpos($path, '\\')) { $str = \mb_strrpos($path, '\\'); if (false !== $str) { return \mb_substr($path, $str + 1, mb_strlen($path)); } return \mb_substr($path, $str, mb_strlen($path)); } //return 'root module'; return false; } /** * @public function getUploadPath * * @return string */ private function getUploadPath() { if (null != $this->getSubDir()) { $ret = $this->getRepositoryPath() . '/' . $this->getSubDir(); } else { $ret = $this->getRepositoryPath(); } return $ret; } /** * @private function getCreated * * @return bool */ private function getCreated() { return $this->created; } /** * @private function getNotCreated * * @return bool */ private function getNotCreated() { return $this->notCreated; } /** * @private function setMode * @param $mode */ private function setMode($mode): void { $this->mode = $mode; } /** * @private function getMode * * @return string */ private function getMode() { return $this->mode; } /** * @public function getLanguage * @param string $moduleDirname * @param string $prefix * @param string $suffix * @param bool $addFq //add function qualifier * @return string */ public function getLanguage(string $moduleDirname, string $prefix = '', string $suffix = '', bool $addFq = true) { $lang = ''; if ($addFq) { $lang = '\\'; } $lang .= '_' . $prefix . '_' . \mb_strtoupper($moduleDirname); $ret = $lang; if (!empty($suffix) || '_' !== $suffix) { $ret = $lang . '_' . $suffix; } elseif ('_' === $suffix) { $ret = $lang . '_'; } return $ret; } /** * @public function getLeftString * @param string $string * * @return string */ public function getLeftString(string $string) { $ret = ''; if ('' != $string) { $ret .= \mb_substr($string, 0, (int)\mb_strpos($string, '_')); } return $ret; } /** * @public function getRightString * @param $string * * @return string */ public function getRightString($string = null) { if ('' != $string) { if (\mb_strpos($string, '_')) { $str = \mb_strpos($string, '_'); if (false !== $str) { return \mb_substr($string, $str + 1, \mb_strlen($string)); } } } return $string; } /** * @public function getCamelCase * @param $string * @param bool $ucfirst * @param bool $lcfirst * * @return string */ public function getCamelCase($string, bool $ucfirst = false, bool $lcfirst = false) { $rightString = $this->getRightString($string); $leftString = $this->getLeftString($string); if ($ucfirst) { return $this->getUcfirst($leftString) . $this->getUcfirst($rightString); } if ($lcfirst) { return $this->getLcfirst($leftString) . $this->getUcfirst($rightString); } return $string; } /** * @public function getUcfirst * @param $string * * @return string */ public function getUcfirst($string) { $ret = ''; if ('' != $string) { $ret .= \ucfirst($string); } return $ret; } /** * @public function getLcfirst * * @param $string * @return string */ public function getLcfirst($string) { $ret = ''; if ('' != $string) { $ret .= \lcfirst($string); } return $ret; } /** * @public function getStrToUpper * * @param $string * @return string */ public function getStrToUpper($string) { $ret = ''; if ('' != $string) { $ret .= \mb_strtoupper($string); } return $ret; } /** * @public function getStrToLower * @param $string * * @return string */ public function getStrToLower($string) { $ret = ''; if ('' != $string) { $ret .= \mb_strtolower($string); } return $ret; } /** * @public function getRequire * @param string $filename * @return string */ public function getRequire(string $filename = 'header') { return "require __DIR__ . '/{$filename}.php';\n"; } /** * @public function getRequireOnce * @param string $filename * @return string */ public function getRequireOnce(string $filename = 'header') { return "require_once __DIR__ . '/{$filename}.php';\n"; } /** * @private function getDashComment * * @param $comment * * @return string */ public function getDashComment($comment) { return "// ------------------- {$comment} ------------------- //\n"; } /** * @public function getSimpleString * @param $string * * @param string $t * @return string */ public function getSimpleString($string, string $t = '') { return "{$t}{$string}\n"; } /** * @public function getHeaderFilesComments * @param $module * @param $noPhpFile * * @param string $namespace * @return string */ public function getHeaderFilesComments($module, $noPhpFile = null, string $namespace = '') { $pc = Modulebuilder\Files\CreatePhpCode::getInstance(); $name = $module->getVar('mod_name'); $dirname = $module->getVar('mod_dirname'); //$version = $module->getVar('mod_version'); //$since = $module->getVar('mod_since'); //$minXoops = $module->getVar('mod_min_xoops'); $author = $module->getVar('mod_author'); //$credits = $module->getVar('mod_credits'); $authorMail = $module->getVar('mod_author_mail'); $authorWebsiteUrl = $module->getVar('mod_author_website_url'); $license = $module->getVar('mod_license'); //$subversion = $module->getVar('mod_subversion'); //$date = date('D Y-m-d H:i:s'); if (null === $noPhpFile) { $ret = ' 'module for xoops', '' => '', '@copyright ' => date('Y') . ' XOOPS Project (https://xoops.org)', '@license ' => $license, '@package ' => $dirname, //'@since ' => $since, //'@min_xoops ' => $minXoops, ]; $authorLine = $author; if ('' !== $authorMail) { $authorLine .= ' - Email:' . $authorMail; } if ('' !== $authorWebsiteUrl) { $authorLine .= ' - Website:' . $authorWebsiteUrl; } $copyright['@author '] = $authorLine; $ret .= $pc->getPhpCodeCommentMultiLine($copyright); return $ret; } /** * @public function renderFile * * @return string */ public function renderFile() { $fileName = $this->getFileName(); $path = $this->getUploadPath() . '/' . $fileName; $created = $this->getCreated(); $notCreated = $this->getNotCreated(); $folderName = $this->getFolderName(); $mode = $this->getMode(); $ret = ''; if (!$this->xf->__construct($path, true)) { // Force to create file if not exist if ($this->xf->open($mode, true)) { if ($this->xf->writable()) { // Integration of the content in the file if (!$this->xf->write($this->getContent(), $mode, true)) { $ret .= \sprintf($notCreated, $fileName, $folderName); $GLOBALS['xoopsTpl']->assign('created', false); return $ret; } // Created $ret .= \sprintf($created, $fileName, $folderName); $GLOBALS['xoopsTpl']->assign('created', true); $this->xf->close(); } else { $ret .= \sprintf($notCreated, $fileName, $folderName); $GLOBALS['xoopsTpl']->assign('created', false); } } else { $ret .= \sprintf($notCreated, $fileName, $folderName); $GLOBALS['xoopsTpl']->assign('created', false); } } return $ret; } }