DataSift\Storyplayer\PlayerLib\Tale_Loader::loadTale PHP Method

loadTale() public static method

load a story, throwing exceptions if problems are detected
public static loadTale ( string $filename ) : Story
$filename string path to the PHP file containing the story
return Story the story object
    public static function loadTale($filename)
    {
        if (!file_exists($filename)) {
            throw new E5xx_InvalidStoryListFile("Cannot find file '{$filename}' to load");
        }
        // load the contents
        $contents = file_get_contents($filename);
        // does it decode?
        $tale = json_decode($contents);
        if (!$tale) {
            throw new E4xx_InvalidStoryListFile("Story list '{$filename}' does not contain valid JSON");
        }
        // does it have the elements we require?
        if (!isset($tale->stories)) {
            throw new E4xx_InvalidStoryListFile("Story list '{$filename}' does not contain a 'stories' element");
        }
        if (!is_array($tale->stories)) {
            throw new E4xx_InvalidStoryListFile("The 'stories' element in the story list '{$filename}' must be an array");
        }
        if (count($tale->stories) == 0) {
            throw new E4xx_InvalidStoryListFile("The 'stories' element in the story list '{$filename}' cannot be an empty array");
        }
        // our base directory to search from
        $storiesBasedir = dirname($filename);
        // do all of the stories in the list exist?
        foreach ($tale->stories as $index => $storyFile) {
            $realStoryFile = $storiesBasedir . DIRECTORY_SEPARATOR . $storyFile;
            if (file_exists($realStoryFile)) {
                throw new E4xx_InvalidStoryListFile("Cannot find the story file '{$realStoryFile}' on disk");
            }
            $tale->stories[$index] = $realStoryFile;
        }
        // inject defaults for optional fields
        if (!isset($tale->options)) {
            $tale->options = new stdClass();
        }
        if (!isset($tale->options->reuseTestEnvironment)) {
            $tale->options->reuseTestEnvironment = false;
        }
        // all done
        return $tale;
    }