Prepares the filesystem for smooth access (directory exists and writable check, trailing slash ...).

@author Benjamin Carl <opensource@clickalicious.de>

@return $this Instance for chaining

@throws Doozr_Cache_Service_Exception
765998
protected function prepareFilesystemAccess()
    {
        $directory = $this->getDirectory();

        // Convert relative paths to absolute. Cause it looks like the __destruct has problems with relative paths
        if ($this->unix && DIRECTORY_SEPARATOR !== $directory{0}) {
            $directory = realpath(getcwd().DIRECTORY_SEPARATOR.$directory).DIRECTORY_SEPARATOR;
        }

        // Check if a trailing slash is in directory -> we require
        if ($directory{strlen($directory) - 1} != DIRECTORY_SEPARATOR) {
            $directory .= DIRECTORY_SEPARATOR;
        }

        if (!file_exists($directory) && !is_dir($directory)) {
            if (mkdir($directory, 0755)) {
                throw new Doozr_Cache_Service_Exception(
                    sprintf('Directory "%s" for caching could not be created!', $directory)
                );
            }
        }

        $this->setDirectory($directory);

        return $this;
    }
872453
public function getCacheDirectory()
    {
        $dir = $this->cacheBaseDir.'/RatingServicesCache/';
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
            if (!file_exists($dir)) {
                $dir = null;
            }
        }
        if (!is_dir($dir) || !is_writable($dir)) {
            $dir = null;
        }

        return $dir;
    }
386823
public function setCacheDir($cacheDir)
    {
        if ($cacheDir !== null) {
            if (!is_dir($cacheDir)) {
                throw new Exception\InvalidArgumentException(
                    "Cache directory '{$cacheDir}' not found or not a directory"
                );
            } elseif (!is_writable($cacheDir)) {
                throw new Exception\InvalidArgumentException(
                    "Cache directory '{$cacheDir}' not writable"
                );
            } elseif (!is_readable($cacheDir)) {
                throw new Exception\InvalidArgumentException(
                    "Cache directory '{$cacheDir}' not readable"
                );
            }

            $cacheDir = rtrim(realpath($cacheDir), DIRECTORY_SEPARATOR);
        } else {
            $cacheDir = sys_get_temp_dir();
        }

        $this->cacheDir = $cacheDir;

        return $this;
    }
1136396
protected function checkCache() {
		if( isset(static::$instances[static::getClass()][$this->id()]) ) {
			return static::$instances[static::getClass()][$this->id()];
		}
		static::$instances[static::getClass()][$this->id()] = $this;
		return $this;
	}
1260600
protected function makeCacheableDir()
    {
        if ($this->configure['cache']) {
            $this->makeDirectories(strval($this->configure['cache_dir']), 0775);
            $this->cacheable = true;
        }
    }
827224
protected function createDirectory()
        {
            $dir = $this->getDirectory();

            if (!$this->files->isDirectory($dir)) {
                $this->files->makeDirectory($dir, 0755, true);
            }

            return $this;
        }
946443
public function warmUp($cacheDir)
    {
        $filesystem = new Filesystem();
        $templates = [];

        foreach ($this->finder->findAllTemplates() as $template) {
            $templates[$template->getLogicalName()] = rtrim($filesystem->makePathRelative($this->locator->locate($template), $cacheDir), '/');
        }

        $templates = str_replace("' => '", "' => __DIR__.'/", var_export($templates, true));

        $this->writeCacheFile($cacheDir.'/templates.php', sprintf("<?php return %s;\n", $templates));
    }
103173
protected function cachedFile()
    {
        if (null === $this->cachedFile) {
            $this->cachedFile = rtrim($this->dir, '/') . '/' . ltrim($this->filename, '/');
        }

        return $this->cachedFile;
    }
372871
public function getCache()
    {
        if (!isset($this->config['cache'])) {
            throw new InvalidArgumentException('A "cache" file system must be set.');
        }

        if (is_string($this->config['cache'])) {
            return new Filesystem(
                new Local($this->config['cache'])
            );
        }

        return $this->config['cache'];
    }
335404
public function writable()
    {
        return file_exists($this->filename) ? is_writable($this->filename) && is_file($this->filename) : $this->writableDir(\dirname($this->filename));
    }
448022
protected function init() {		
	    if(Apc::isAvailable()) {
		    $driver = new Apc();
		} else {
			if (!is_dir(WT_DATA_DIR.DIRECTORY_SEPARATOR.'cache')) {
				// We may not have permission - especially during setup, before we instruct
				// the user to "chmod 777 /data"
				@mkdir(WT_DATA_DIR.DIRECTORY_SEPARATOR.'cache');
			}
			if (is_dir(WT_DATA_DIR.DIRECTORY_SEPARATOR.'cache')) {
			    $driver = new FileSystem(array('path' => WT_DATA_DIR.DIRECTORY_SEPARATOR.'cache'));
			} else {
				// No cache available, let's just use a basic one :-(
				$driver = new Ephemeral();
			}
		}		

		$this->cache = new \Stash\Pool($driver);		
		$this->is_init = true;
	}
1344487
protected function BeforeInit()
    {
        //todo: check access rights
        $cacheFile = PathUtil::ContentCacheFile($this);
        $this->fileCacher = new FileCacher($cacheFile, $this->content->GetCacheLifetime());
        if ($this->fileCacher->MustUseCache())
        {
            $this->output = $this->fileCacher->GetFromCache();
            return true;
        }
        return parent::BeforeInit();
    }
768895
public function init() {
        if (preg_match('/^[0-9]+;/', $this->sessiondir)) {
            throw new exception('sessionhandlerproblem', 'error', '', null, 'Multilevel session directories are not supported');
        }
        // Make sure session directory exists and is writable.
        make_writable_directory($this->sessiondir, false);
        if (!is_writable($this->sessiondir)) {
            throw new exception('sessionhandlerproblem', 'error', '', null, 'Session directory is not writable');
        }
        // Need to disable debugging since disk_free_space()
        // will fail on very large partitions (see MDL-19222).
        $freespace = @disk_free_space($this->sessiondir);
        // MDL-43039: disk_free_space() returns null if disabled.
        if (!($freespace > 2048) and ($freespace !== false) and ($freespace !== null)) {
            throw new exception('sessiondiskfull', 'error');
        }

        // NOTE: we cannot set any lock acquiring timeout here - bad luck.
        ini_set('session.save_handler', 'files');
        ini_set('session.save_path', $this->sessiondir);
    }
445835
public function clean()
    {
        try {
            $this->filesystem->deleteDir($this->localPath);
        } catch (\ErrorException $e) {
            // For reasons beyond our control, the actual owner of the directory
            // contents may not be the same as the current user. Therefore we need
            // to catch ErrorException and throw an expected exception with informing
            // the current user.
            throw new FileSystemException($e->getMessage(), $e->getCode(), $e);
        }
    }
942191
public function clean()
    {
        $directory = scandir($this->path);

        if (!empty($directory)) {
            foreach ($directory as $item) {
                if ($item !== '.' && $item !== '..') {
                    $this->remove($item);
                }
            }
        }

        return $this;
    }
742524
private function ensureStorage()
    {
        if (!isset($this->storage)) {
            $this->storage = $this->getCache()->get($this->cacheKey);

            if ($this->storage === false) {
                $this->storage = $this->getSettingsStorage()->getBounded($this->settingsStorageKey);
                $this->cache();
            }
        }

        return $this->storage;
    }
152133
public function prepare()
    {
        $id = $this->getAttribute('id');
        $menus = $this->menu['with'] ?? [];
        $parent = $this->getAttribute('position');
        $position = Str::startsWith($parent, '^:') ? $parent.'.' : '^:';

        foreach ((array) $menus as $class) {
            $menu = $this->container->make($class)
                            ->setAttribute('position', "{$position}{$id}")
                            ->prepare();

            if ($menu->passes()) {
                $this->childs[] = $menu;
            }
        }

        $this->name = $id;
        $this->showToUser = $this->passesAuthorization();

        return $this;
    }