/** * Makes the work of autoload registered classes * * @param string! $className * @return boolean */ public function autoload($className) { $eventsManager = $this->_eventsManager; if (is_object($eventsManager)) { $eventsManager->fire("loader:beforeCheckClass", $this, $className); } /** * First we check for static paths for classes */ $classes = $this->_classes; if (is_array($classes)) { if (isset($classes[$className])) { $filePath = $classes[$className]; if (is_object($eventsManager)) { $this->_foundPath = $filePath; $eventsManager->fire("loader:pathFound", $this, $filePath); } } require $filePath; return true; } $extensions = $this->_extensions; $ds = DIRECTORY_SEPARATOR; $namespaceSeparator = "\\"; /** * Checking in namespaces */ $namespaces = $this->_namespaces; if (is_array($namespaces)) { foreach ($namespaces as $nsPrefix => $directory) { /** * The class name must start with the current namespace */ if (Text::startsWith($className, $nsPrefix)) { /** * Append the namespace separator to the prefix */ $fileName = substr($className, strlen($nsPrefix . $namespaceSeparator)); $fileName = str_replace($namespaceSeparator, $ds, $fileName); if ($fileName) { /** * Add a trailing directory separator if the user forgot to do that */ $fixedDirectory = rtrim($directory, $ds) . $ds; foreach ($extensions as $extension) { $filePath = $fixedDirectory . $fileName . "." . $extension; /** * Check if a events manager is available */ if (is_object($eventsManager)) { $this->_checkedPath = $filePath; $eventsManager->fire("loader:beforeCheckPath", $this); } /** * This is probably a good path, let's check if the file exists */ if (is_file($filePath)) { if (is_object($eventsManager)) { $this->_foundPath = $filePath; $eventsManager->fire("loader:pathFound", $this, $filePath); } /** * Simulate a require */ require $filePath; /** * Return true mean success */ return true; } } } } } } /** * Checking in prefixes */ $prefixes = $this->_prefixes; if (is_array($prefixes)) { foreach ($prefixes as $prefix => $directory) { /** * The class name starts with the prefix? */ if (Text::startsWith($className, $prefix)) { /** * Get the possible file path */ $fileName = str_replace($prefix . $namespaceSeparator, "", $className); $fileName = str_replace($prefix . "_", "", $fileName); $fileName = str_replace("_", $ds, $fileName); //$this->_fileName = $fileName; if ($fileName) { /** * Add a trailing directory separator if the user forgot to do that */ $fixedDirectory = rtrim($directory, $ds) . $ds; foreach ($extensions as $extension) { $filePath = $fixedDirectory . $fileName . "." . $extension; if (is_object($eventsManager)) { $this->_checkedPath = $filePath; $eventsManager->fire("loader:beforeCheckPath", $this, $filePath); } if (is_file($filePath)) { /** * Call 'pathFound' event */ if (is_object($eventsManager)) { $this->_foundPath = $filePath; $eventsManager->fire("loader:pathFound", $this, $filePath); } /** * Simulate a require */ require $filePath; /** * Return true meaning success */ return true; } } } } } } /** * Change the pseudo-separator by the directory separator in the class name */ $dsClassName = str_replace("_", $ds, $className); /** * And change the namespace separator by directory separator too */ $nsClassName = str_replace("\\", $ds, $dsClassName); /** * Checking in directories */ $directories = $this->_directories; if (is_array($directories)) { foreach ($directories as $directory) { /** * Add a trailing directory separator if the user forgot to do that */ $fixedDirectory = rtrim($directory, $ds) . $ds; foreach ($extensions as $extension) { /** * Create a possible path for the file */ $filePath = $fixedDirectory . $nsClassName . "." . $extension; if (is_object($eventsManager)) { $this->_checkedPath = $filePath; $eventsManager->fire("loader:beforeCheckPath", $this, $filePath); } /** * Check in every directory if the class exists here */ if (is_file($filePath)) { /** * Call 'pathFound' event */ if (is_object($eventsManager)) { $this->_foundPath = $filePath; $eventsManager->fire("loader:pathFound", $this, $filePath); } /** * Simulate a require */ require $filePath; /** * Return true meaning success */ return true; } } } } /** * Call 'afterCheckClass' event */ if (is_object($eventsManager)) { $eventsManager->fire("loader:afterCheckClass", $this, $className); } /** * Cannot find the class, return false */ return false; }
/** * Destroys the active session * *<code> * var_dump($session->destroy()); * var_dump($session->destroy(true)); *</code> * * @param boolean $removeData * @return boolean */ public function destroy($removeData = false) { if ($removeData) { $uniqueId = $this->_uniqueId; if (!empty($uniqueId)) { foreach ($_SESSION as $key => $value) { if (Text::startsWith($key, $uniqueId . '#')) { unset($_SESSION[$key]); } } } else { $_SESSION = []; } } $this->_started = false; return session_destroy(); }
/** * Possible class name that will be located to dispatch the request * * @return string */ public function getHandlerClass() { $this->_resolveEmptyProperties(); $handlerSuffix = $this->_handlerSuffix; $handlerName = $this->_handlerName; $namespaceName = $this->_namespaceName; //We don't camelize the classes if they are in namespaces $p = strpos($this->_handlerName, '\\'); if ($p === false) { $camelizedClass = Text::camelize($handlerName); } elseif ($p === 0) { //@note this only handles one leading slash $camelizedClass = substr($handlerName, strlen($handlerName) + 1); } else { $camelizedClass = $this->_handlerName; } //Create the complete controller class name prepending the namespace if ($namespaceName) { if (Text::endsWith($namespaceName, '\\')) { return $namespaceName . $camelizedClass . $handlerSuffix; } else { return $namespaceName . '\\' . $camelizedClass . $handlerSuffix; } } else { return $camelizedClass . $handlerSuffix; } return $handlerClass; }
/** * Returns routePaths * * @param mixed $paths * @return array */ public static function getRoutePaths($paths = null) { if ($paths !== null) { if (is_string($paths)) { $moduleName = null; $controllerName = null; $actionName = null; // Explode the short paths using the :: separator $parts = explode('::', $paths); // Create the array paths dynamically switch (count($parts)) { case 3: $moduleName = $parts[0]; $controllerName = $parts[1]; $actionName = $parts[2]; break; case 2: $controllerName = $parts[0]; $actionName = $parts[1]; break; case 1: $controllerName = $parts[0]; break; } $routePaths = []; // Process module name if ($moduleName !== null) { $routePaths['module'] = $moduleName; } // Process controller name if ($controllerName !== null) { // Check if we need to obtain the namespace if (strpos($controllerName, '\\') !== false) { $classWithNamespace = get_class($controllerName); $pos = strrpos($classWithNamespace, '\\'); if ($pos !== false) { //Extract the namespace from the namespaced class $namespaceName = substr($classWithNamespace, 0, $pos); //Extract the real class name from the namespaced class $realClassName = substr($classWithNamespace, $pos); //Update the namespace if ($namespaceName) { $routePaths['namespace'] = $namespaceName; } } else { $realClassName = $classWithNamespace; } } else { $realClassName = $controllerName; } // Always pass the controller to lowercase $routePaths["controller"] = Text::uncamelize($realClassName); } // Process action name if ($actionName !== null) { $routePaths['action'] = $actionName; } } else { $routePaths = $paths; } } else { $routePaths = []; } if (!is_array($routePaths)) { throw new Exception("The route contains invalid paths"); } return $routePaths; }
/** * Checks if a password hash is a valid bcrypt's hash * * @param string $passwordHash * @return boolean * @throws Exception */ public function isLegacyHash($passwordHash) { if (!is_string($passwordHash)) { throw new Exception('Invalid parameter type.'); } return Text::startsWith($passwordHash, '$2a$'); }
/** * Immediately invalidates all existing items. * * @return boolean */ public function flush() { //$prefix = $this->_prefix; $prefix = 'my-cache'; if (!isset($this->_options['cacheDir'])) { throw new Exception('Unexpected inconsistency in options'); } else { $cacheDir = $this->_options['cacheDir']; } $iterator = new \DirectoryIterator($cacheDir); if ($prefix !== null) { //Prefix is set foreach ($iterator as $item) { if (!is_dir($item)) { $key = $item->getFileName(); $cacheFile = $item->getPathName(); if (Text::startsWith($key, $prefix)) { if (!unlink($cacheFile)) { return false; } } } } } else { //Without using a prefix foreach ($iterator as $item) { if (!is_dir($item)) { $cacheFile = $item->getPathName(); if (!unlink($cacheFile)) { return false; } } } } return true; }
/** * Returns the available headers in the request * * @return array */ public function getHeaders() { $headers = []; $contentHeaders = ['CONTENT_TYPE' => true, 'CONTENT_LENGTH' => true]; foreach ($_SERVER as $name => $value) { if (Text::startsWith($name, 'HTTP_')) { $name = ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))); $name = str_replace(' ', '-', $name); $headers[$name] = $value; } else { $name = ucwords(strtolower(str_replace('_', ' ', $name))); $name = str_replace(' ', '-', $name); $headers[$name] = $value; } } return $headers; }
/** * Returns collection name mapped in the model * * @return string */ public function getSource() { $source = $this->_source; if (!$source) { $collection = $this; $source = Text::uncamelize(basename(get_class($this))); $this->_source = $source; } return $source; }