src/GameOfLife/Inputs/TemplateHandler/TemplateLoader.php
/**
     * Loads a template from a file.
     *
     * @param String $_templateName The name of the template
     *
     * @return array The loaded template
     *
     * @throws \Exception The exception when the template file was not found or the file could not be read
     */
Loads a template from a file.
@param String $_templateName The name of the template

@return array The loaded template

@throws \Exception The exception when the template file was not found or the file could not be read
Loads a template from a file . @param String $_templateName The name of the template @return array The loaded template @throws \ Exception The exception when the template file was not found or the file could not be read
public function loadTemplate(String $_templateName)
    {
        $fileName = $_templateName . ".txt";
        $defaultTemplatePath = $this->defaultTemplatesDirectory . "/" . $fileName;
        $customTemplatePath = $this->customTemplatesDirectory . "/" . $fileName;
        if (file_exists($defaultTemplatePath)) $fileName = $defaultTemplatePath;
        elseif (file_exists($customTemplatePath)) $fileName = $customTemplatePath;
        else throw new \Exception("The template file could not be found.");
        $lines = $this->fileSystemReader->readFile($fileName);
        $fields = array();
        for ($y = 0; $y < count($lines); $y++)
        {
            $fields[] = array();
            $row = str_split($lines[$y]);
            for ($x = 0; $x < count($row); $x++)
            {
                if ($row[$x] == "X") $cellState = true;
                else $cellState = false;
                $fields[$y][] = new Field($x, $y, $cellState);
            }
        }
        return $fields;
    }
public function loadTemplate ( String $ _templateName ) { $ fileName = $ _templateName . " .txt " ; $ defaultTemplatePath = $ this -> defaultTemplatesDirectory . " / " . $ fileName ; $ customTemplatePath = $ this -> customTemplatesDirectory . " / " . $ fileName ; if ( file_exists ( $ defaultTemplatePath ) ) $ fileName = $ defaultTemplatePath ; elseif ( file_exists ( $ customTemplatePath ) ) $ fileName = $ customTemplatePath ; else throw new \ Exception ( " The template file could not be found. " ) ; $ lines = $ this -> fileSystemReader -> readFile ( $ fileName ) ; $ fields = array ( ) ; for ( $ y = 0 ; $ y < count ( $ lines ) ; $ y ++ ) { $ fields [ ] = array ( ) ; $ row = str_split ( $ lines [ $ y ] ) ; for ( $ x = 0 ; $ x < count ( $ row ) ; $ x ++ ) { if ( $ row [ $ x ] == " X " ) $ cellState = true ; else $ cellState = false ; $ fields [ $ y ] [ ] = new Field ( $ x , $ y , $ cellState ) ; } } return $ fields ; }
Loads a template from a file.
Loads a template from a file .
// Check whether the specified template exists // Read the template