protected function onPrepare($argv = null) { $this->thisApp = App::getApp(); $root = $this->getNewAppDir(); if (is_dir($root)) { throw new \Exception("Directory {$root} is existing!"); } $kephpEntry = relative_path($root, $this->thisApp->kephp()); list($path, $phar) = split_phar($kephpEntry); if ($phar !== false) { $kephpEntry = "'phar://' . __DIR__ . '{$path}/{$phar}{$this->entryFile}'"; } else { $kephpEntry = "__DIR__ . '{$path}{$this->entryFile}'"; } $this->context['kephpLibEntry'] = $kephpEntry; if (empty($this->appNamespace)) { $this->appNamespace = path2class($this->name); } else { if (!preg_match('#^[a-z0-9_]+$#i', $this->appNamespace)) { throw new \Exception("App namespace only can use char in a-z0-9_."); } } $this->context['appNamespace'] = trim($this->appNamespace, KE_PATH_NOISE); }
function relative_path(string $target, string $source, string $plus = null, $slash = KE_DS_UNIX) { if ($slash !== KE_DS_UNIX && $slash !== KE_DS_WIN) { $slash = KE_DS_UNIX; } $source = convert_path_slash($source, $slash); $target = convert_path_slash($target, $slash); if (isset($plus)) { $plus = ltrim($plus, KE_PATH_NOISE); } list($source, $phar) = split_phar($source); $relative = $source; if (strlen($source) > 0) { $samePart = compare_path($source, $target, $slash); if (!empty($samePart)) { $samePart = preg_quote($samePart); $quoteSlash = preg_quote($slash); $regex = "#^({$quoteSlash}?{$samePart}{$quoteSlash}?)#i"; $sourceTail = preg_replace($regex, '', $source); $sourceTail = trim($sourceTail, KE_PATH_NOISE); $targetTail = preg_replace($regex, '', $target); $targetTail = trim($targetTail, KE_PATH_NOISE); $targetDepth = 0; if (strlen($targetTail) > 0) { $targetSplit = explode($slash, $targetTail); $targetDepth = count($targetSplit); } $relative = str_repeat($slash . '..', $targetDepth); if (strlen($sourceTail) > 0) { $relative .= $slash . $sourceTail; } } } if ($phar !== false) { $relative = 'phar://' . $relative; $relative .= $slash . $phar; } if (strlen($plus) > 0) { $relative .= $slash . convert_path_slash($plus); } return $relative; }