AppserverIo\Appserver\Core\Api\ContainerService::prepareFileSystem PHP Method

prepareFileSystem() public method

Prepares filesystem to be sure that everything is on place as expected
public prepareFileSystem ( ) : void
return void
    public function prepareFileSystem()
    {
        // load the directories
        $directories = $this->getDirectories();
        // load user and group from system configuration
        $user = $this->getSystemConfiguration()->getUser();
        $group = $this->getSystemConfiguration()->getGroup();
        // check if the necessary directories already exists, if not, create them
        foreach (DirectoryKeys::getServerDirectoryKeysToBeCreated() as $directoryKey) {
            // prepare the path to the directory to be created
            $toBeCreated = $this->realpath($directories[$directoryKey]);
            // prepare the directory name and check if the directory already exists
            if (is_dir($toBeCreated) === false) {
                // if not, try to create it
                if (mkdir($toBeCreated, 0755, true) === false) {
                    throw new \Exception(sprintf('Can\'t create necessary directory %s while starting application server', $toBeCreated));
                }
                // set user/group specified from the system configuration
                if ($this->setUserRights(new \SplFileInfo($toBeCreated), $user, $group) === false) {
                    throw new \Exception(sprintf('Can\'t switch user/group to %s/% for directory %s while starting application server', $user, $group, $toBeCreated));
                }
            }
        }
        // create the container specific directories
        /** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
        foreach ($this->getSystemConfiguration()->getContainers() as $containerNode) {
            // iterate over the container host's directories
            foreach ($containerNode->getHost()->getDirectories() as $directory) {
                // prepare the path to the directory to be created
                $toBeCreated = $this->realpath($directory);
                // prepare the directory name and check if the directory already exists
                if (is_dir($toBeCreated) === false) {
                    // if not, try to create it
                    if (mkdir($toBeCreated, 0755, true) === false) {
                        throw new \Exception(sprintf('Can\'t create necessary directory %s while starting application server', $toBeCreated));
                    }
                    // set user/group specified from the system configuration
                    if ($this->setUserRights(new \SplFileInfo($toBeCreated), $user, $group) === false) {
                        throw new \Exception(sprintf('Can\'t switch user/group to %s/% for directory %s while starting application server', $user, $group, $toBeCreated));
                    }
                }
            }
        }
        // check if specific directories has to be cleaned up on startup
        foreach (DirectoryKeys::getServerDirectoryKeysToBeCleanedUp() as $directoryKey) {
            // prepare the path to the directory to be cleaned up
            $toBeCleanedUp = $this->realpath($directories[$directoryKey]);
            // if the directory exists, clean it up
            if (is_dir($toBeCleanedUp)) {
                $this->cleanUpDir(new \SplFileInfo($toBeCleanedUp));
            }
        }
        // check if needed files do exist and have the correct user rights
        $files = $this->getFiles();
        foreach (FileKeys::getServerFileKeysToBeCreated() as $fileKeys) {
            // prepare the path to the file to be created
            $toBeCreated = $this->realpath($files[$fileKeys]);
            // touch the file (will lead to its creation if it does not exist by now)
            if (touch($toBeCreated) === false) {
                throw new \Exception(sprintf('Can\'t create necessary file %s while starting application server', $toBeCreated));
            } else {
                chmod($toBeCreated, 0755);
            }
            // set user/group specified from the system configuration
            if ($this->setUserRight(new \SplFileInfo($toBeCreated), $user, $group) === false) {
                throw new \Exception(sprintf('Can\'t switch user/group to %s/% for file %s while starting application server', $user, $group, $toBeCreated));
            }
        }
    }