/** * 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(); }
/** * Reconfigure the route adding a new pattern and a set of paths * * @param string $pattern * @param array|null|string $paths * @throws Exception */ public function reConfigure($pattern, $paths = null) { if (!is_string($pattern)) { throw new Exception('The pattern must be string'); } $routePaths = self::getRoutePaths($paths); /** * If the route starts with '#' we assume that it is a regular expression */ if (!Text::startsWith($pattern, '#')) { if (strpos($pattern, '{') !== false) { /** * The route has named parameters so we need to extract them */ $extracted = $this->extractNamedParams($pattern); $pcrePattern = $extracted[0]; $routePaths = array_merge($routePaths, $extracted[1]); } else { $pcrePattern = $pattern; } /** * Transform the route's pattern to a regular expression */ $compiledPattern = $this->compilePattern($pcrePattern); } else { $compiledPattern = $pattern; } /** * Update the original pattern */ $this->_pattern = $pattern; /** * Update the compiled pattern */ $this->_compiledPattern = $compiledPattern; /** * Update the route's paths */ $this->_paths = $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; }