/** * @param string $path * @param boolean $absolute * @return string */ public static function getPath($path, $absolute = false) { $project = ProjectManager::getActiveProject(); $resPath = ''; if ($project) { $resPath .= $project->getResourcePath(); } else { $resPath .= ProjectManager::getDefaultProject()->getResourcePath(); } $resPath .= $path; if (!$absolute) { $resPath = str_replace(PathHelper::getBasePath(), '', $resPath); } return PathHelper::normalizePath($resPath); }
/** * Creates a new project by registering it * in the config file provided. It if doesn't exist * it will be created * * @param string $id * @param string $name * @param string $description * @param string $namespace * @param boolean $active * @param OutputInterface &$output */ public static function createProject($id, $name, $description, $namespace, $active, OutputInterface &$output = null) { /** * Create xml config entry */ if (!is_dir(PROJECT_ROOT_PATH)) { mkdir(PROJECT_ROOT_PATH); } if (!file_exists(PROJECT_CONFIG_FILE)) { fclose(fopen(PROJECT_CONFIG_FILE, "w")); if ($output) { $output->writeln(sprintf('<comment>Project config file not found. Created %s</comment>', PROJECT_CONFIG_FILE)); } } $xml = new Xml(PROJECT_CONFIG_FILE); if ($xml->isValid() && $xml->getTreeElement()->getChildByName($id, true)) { throw new \Exception("A project with the id " . $id . " already exists!"); } // Deactivate all if ($xml->isValid() && $xml->hasRoot() && $active) { if ($output) { $output->writeln("<comment>Deactivating other projects...</comment>"); } foreach ($xml->getTreeElement()->getChildren() as $child) { $child->addAttribute('active', 'false'); } } $projectElement = new XmlElement($id, array('active' => $active ? 'true' : 'false'), '', array(new XmlElement('name', array(), $name), new XmlElement('description', array(), $description), new XmlElement('namespace', array(), $namespace))); if ($xml->hasRoot()) { $xml->addChild($projectElement); } else { $root = new XmlElement('projects', array(), '', array($projectElement)); $xml->addChild($root); } $xml->save(); if ($output) { $output->writeln("<info>Registered project in the config file</info>"); } /** * Create base folder structure */ $folders = array(PROJECT_CLASS_PATH, PROJECT_RESOURCE_PATH, PROJECT_TEMPLATE_PATH, PROJECT_TRANSLATION_PATH, PROJECT_CONFIG_PATH); $projectRoot = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id)); if (!is_dir($projectRoot)) { mkdir($projectRoot); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $projectRoot)); } } foreach ($folders as $folder) { $projectComponentFolder = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id, $folder)); if (!is_dir($projectComponentFolder)) { mkdir($projectComponentFolder); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $projectComponentFolder)); } } } $layoutPath = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id, PROJECT_TEMPLATE_PATH, 'layouts')); $viewPath = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id, PROJECT_TEMPLATE_PATH, 'views')); if (!is_dir($layoutPath)) { mkdir($layoutPath); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $layoutPath)); } } if (!is_dir($viewPath)) { mkdir($viewPath); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $viewPath)); } } /** * Create base config files: * * - routes.xml * - environment.xml */ // routes.xml $routesXmlPath = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id, PROJECT_CONFIG_PATH)) . 'routes.xml'; if (!file_exists($routesXmlPath)) { fclose(fopen($routesXmlPath, "w")); $routesXml = new Xml($routesXmlPath); $routesXml->addChild(new XmlElement('routes', array(), "\n")); $routesXml->save(); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $routesXmlPath)); } } // environment.xml $environemntXmlPath = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id, PROJECT_CONFIG_PATH)) . 'environment.xml'; if (!file_exists($environemntXmlPath)) { fclose(fopen($environemntXmlPath, "w")); $routesXml = new Xml($environemntXmlPath); $routesXml->addChild(new XmlElement('environment', array(), '', array(new XmlElement('type', array('name' => 'production', 'flags' => 'live')), new XmlElement('type', array('name' => 'staging', 'flags' => 'testing')), new XmlElement('type', array('name' => 'development', 'flags' => 'local'), '', array(new XmlElement('condition', array('type' => 'url', 'value' => '/\\.local/'))))))); $routesXml->save(); if ($output) { $output->writeln(sprintf('<comment>Created %s with default options</comment>', $environemntXmlPath)); } } /** * Create default layout */ $defaultLayoutFile = $layoutPath . DEFAULT_LAYOUT . '.' . LAYOUT_FILETYPE; if (!file_exists($defaultLayoutFile)) { $handle = fopen($defaultLayoutFile, 'w'); fwrite($handle, "Your project \"" . $id . "\" is now available! \r\n<hr />\r\n" . '<?= $this->renderView() ?>'); fclose($handle); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $defaultLayoutFile)); } } /** * Create index view */ $defaultViewPath = PathHelper::createPath(array($viewPath, DEFAULT_LAYOUT)); $indexViewPath = PathHelper::createPath(array($viewPath, DEFAULT_LAYOUT, 'index')); if (!is_dir($defaultViewPath)) { mkdir($defaultViewPath); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $defaultViewPath)); } } if (!is_dir($indexViewPath)) { mkdir($indexViewPath); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $indexViewPath)); } } $indexViewFile = $indexViewPath . 'index.' . VIEW_FILETYPE; if (!file_exists($indexViewFile)) { $handle = fopen($indexViewFile, 'w'); fwrite($handle, 'Index view'); fclose($handle); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $indexViewFile)); } } /** * Create index controller */ $controllerPath = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id, PROJECT_CLASS_PATH, 'Controller')); $controllerViewPath = PathHelper::createPath(array(PROJECT_ROOT_PATH, $id, PROJECT_CLASS_PATH, 'Controller', 'View')); if (!is_dir($controllerPath)) { mkdir($controllerPath); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $controllerPath)); } } if (!is_dir($controllerViewPath)) { mkdir($controllerViewPath); if ($output) { $output->writeln(sprintf('<comment>Created %s</comment>', $controllerViewPath)); } } $controllerViewFile = $controllerViewPath . 'Index.php'; if (!file_exists($controllerViewFile)) { $handle = fopen($controllerViewFile, 'w'); fwrite($handle, trim(sprintf('<?php namespace %s\\Controller\\View; class Index extends \\Fewlines\\Core\\Controller\\View { public function indexAction() { // The default index action } }', $namespace))); fclose($handle); if ($output) { $output->writeln(sprintf('<comment>Created %s with default class</comment>', $controllerViewFile)); } } }
/** * @param string $id * @param string $name * @param string $description * @param string $nsName * @param string $root */ public function __construct($id, $name, $description, $nsName, $root = '') { $this->id = $id; $this->name = $name; $this->description = $description; $this->nsName = $nsName; // Set paths $this->root = empty($root) ? PathHelper::createPath(array(PROJECT_ROOT_PATH, $id)) : $root; $this->nsPath = PathHelper::createPath(array($this->root, PROJECT_CLASS_PATH)); $this->configPath = PathHelper::createPath(array($this->root, PROJECT_CONFIG_PATH)); $this->viewPath = PathHelper::createPath(array($this->root, PROJECT_VIEW_PATH)); $this->layoutPath = PathHelper::createPath(array($this->root, PROJECT_LAYOUT_PATH)); $this->translationPath = PathHelper::createPath(array($this->root, PROJECT_TRANSLATION_PATH)); $this->resourcePath = PathHelper::createPath(array($this->root, PROJECT_RESOURCE_PATH)); $this->templatePath = PathHelper::createPath(array($this->root, PROJECT_TEMPLATE_PATH)); }
/** * Adds the config files to * the local config files var * * @param array $configs */ public function addConfigFiles($configs) { $files = array(); for ($i = 0, $len = count($configs); $i < $len; $i++) { $dir = PathHelper::normalizePath($configs[$i]['dir']); if (!is_dir($dir)) { continue; } $files[$dir] = DirHelper::getFilesByType($dir, $configs[$i]['type'], true); } $files = DirHelper::flattenTree($files); for ($i = 0, $len = count($files); $i < $len; $i++) { $this->configFiles[] = $files[$i]; } // Reload config file list $this->updateFiles(); }
/** * Returns the content of * a rendered element * * @param string $viewPath * @param array $config * @param string $wrapper * @return string */ public function render($viewPath, $config = array(), $wrapper = '') { $view = PathHelper::getRealViewPath(ltrim($viewPath, DR_SP), '', Template::getInstance()->getLayout()->getName()); $path = $view; // Handle relative path if (!PathHelper::isAbsolute($viewPath)) { $backtrace = debug_backtrace(); $viewIndex = PathHelper::getFirstViewIndexFromDebugBacktrace($backtrace); if (false === $viewIndex) { throw new Exception\NoViewOriginFoundException('The view "' . $view . '" could not be include. You can render relative path\'s only from an other view.'); } $file = PathHelper::normalizePath($backtrace[$viewIndex]['file']); $dir = pathinfo($file, PATHINFO_DIRNAME); $path = PathHelper::getRealViewFile(PathHelper::normalizePath($dir . DR_SP . $viewPath)); $view = PathHelper::normalizePath(realpath($path)); if ($view == $file) { throw new Exception\RenderRecursionException('The view "' . $view . '" is including itself (Recursion).'); exit; } } /** * Default check & render */ if (!$view || !file_exists($view)) { if (!$view) { $view = $path; } throw new Exception\ViewIncludeNotFoundException('The view "' . $view . '" was not found and could not be included'); } $content = $this->getRenderedHtml($view, $config); if (!empty($wrapper)) { $content = sprintf($wrapper, $content); } return $content; }
/** * Sets the view path * * @param string $view */ public function setPath($view) { $layout = Template::getInstance()->getLayout()->getName(); $viewFile = PathHelper::getRealViewPath($view, $this->getAction(), $layout); $this->pathView = $view; $this->path = $viewFile; }
/** * Get a translation from a file by a path * * @param string|arary $path * @return string */ public static function get($path) { $project = ProjectManager::getActiveProject(); $project = $project ? $project : ProjectManager::getDefaultProject(); $pathParts = ArrayHelper::clean(explode(self::SUBPATH_SEPERATOR, $path)); $localeDir = PathHelper::createPath(array($project->getTranslationPath(), Locale::getKey())); $entryPoint = ''; $entryPointIndex = 0; // Check if path is empty if (empty($pathParts)) { return ''; } // Get entry point file for ($i = 0, $len = count($pathParts); $i < $len; $i++) { $isFile = false; $localeDir = PathHelper::getRealPath($localeDir); for ($x = 0, $lenX = count(self::$translationTypes); $x < $lenX; $x++) { $fileExt = self::$translationTypes[$x]; $pathPart = $pathParts[$i]; $isFile = is_file($localeDir . $pathPart . '.' . $fileExt); // Escape loop if file was found if (true == $isFile) { break; } } // Attach current "dir" to the localdir (next level) $localeDir .= $pathPart; // Excape loop if entry point was found if (true == $isFile) { $entryPointIndex = $i; $entryPoint = $localeDir . '.' . $fileExt; break; } } /** * If there is no entry point take * all supported translation files * from the given directory */ if (empty($entryPoint) && is_dir($localeDir)) { $files = DirHelper::scanDir($localeDir); $names = array(); foreach ($files as $file) { $extension = pathinfo($file['path'], PATHINFO_EXTENSION); $filename = pathinfo($file['path'], PATHINFO_FILENAME); if (array_search($extension, self::$translationTypes) !== false) { $names[] = $filename; } } return $names; } // if (true == empty($entryPoint)) { // throw new Translator\Exception\EntryPointNotFoundException("No entry point (file) found for: " . (string) $path); // } $pathParts = array_slice($pathParts, $entryPointIndex + 1); $pathParts = ArrayHelper::clean($pathParts); // The default translation value $value = $path; /** * Operate with the key functions for different file types. * At this point we are handling valid values */ switch ($fileExt) { case self::$translationTypes[0]: $value = self::getValueByKeyPHP($entryPoint, $pathParts); break; case self::$translationTypes[1]: $value = self::getValueByKeyCSV($entryPoint, $pathParts); break; } /** * If the value is a array and empty it doesn't * need to be returned. Return a empty string * instead */ if (empty($value)) { return ''; } else { return $value; } }
/** * @param string $name * @param string $path * @param array $routeUrlParts */ public function __construct($name, $path) { $this->setName($name); $this->path = PathHelper::normalizePath($path); }