izzum\statemachine\loader\YAML::createFromFile PHP Method

createFromFile() public static method

creates an instance of this class with the data loaded from a file.
public static createFromFile ( string $filename ) : YAML
$filename string eg: __DIR__ . '/../../configuration.yaml'
return YAML an instance of YAML with the data read from the file
    public static function createFromFile($filename)
    {
        if (!file_exists($filename)) {
            throw new Exception(sprintf('Failed to load yaml from file "%s". The file does not exist', $filename), Exception::BAD_LOADERDATA);
        }
        //suppres warning with @ operator. we are explicitely testing the return value.
        $yaml = @file_get_contents($filename);
        if (false === $yaml) {
            throw new Exception(sprintf('Failed to read yaml data from file "%s". Unknown error (permissions?)', $filename), Exception::BAD_LOADERDATA);
        }
        return new static($yaml);
    }

Usage Example

 /**
  * @test
  */
 public function shouldThrowExceptionForNoMachineData()
 {
     $machine = new StateMachine(new Context(new Identifier('yaml-test', 'yaml-machine')));
     $loader = YAML::createFromFile(__DIR__ . '/fixture-no-machines.yaml');
     try {
         $loader->load($machine);
         $this->fail('should not come here');
     } catch (Exception $e) {
         $this->assertEquals(Exception::BAD_LOADERDATA, $e->getCode());
         $this->assertContains('no machine data', $e->getMessage());
     }
 }