2018-01-02 21:26:33 +01:00
< ? php
namespace Misuzu ;
2018-01-02 21:57:06 +01:00
use Aitemu\RouteCollection ;
2018-01-02 21:26:33 +01:00
use Misuzu\Config\ConfigManager ;
class Application
{
private static $instance = null ;
public static function getInstance () : Application
{
if ( is_null ( static :: $instance ) || ! ( static :: $instance instanceof Application )) {
throw new \Exception ( 'Invalid instance type.' );
}
return static :: $instance ;
}
public static function start () : Application
{
if ( ! is_null ( static :: $instance ) || static :: $instance instanceof Application ) {
throw new \Exception ( 'An Application has already been set up.' );
}
static :: $instance = new Application ;
return static :: getInstance ();
}
2018-01-03 02:12:28 +01:00
public static function gitCommitInfo ( string $format ) : string
2018-01-02 21:26:33 +01:00
{
2018-01-03 02:12:28 +01:00
return trim ( shell_exec ( sprintf ( 'git log --pretty="%s" -n1 HEAD"' , $format )));
}
2018-01-02 21:26:33 +01:00
2018-01-03 02:12:28 +01:00
public static function gitCommitHash ( bool $long = false ) : string
{
return self :: gitCommitInfo ( $long ? '%H' : '%h' );
2018-01-02 21:26:33 +01:00
}
2018-01-03 02:12:28 +01:00
public static function gitBranch () : string
2018-01-02 21:26:33 +01:00
{
2018-01-03 02:12:28 +01:00
return trim ( shell_exec ( 'git rev-parse --abbrev-ref HEAD' ));
2018-01-02 21:26:33 +01:00
}
2018-01-03 02:12:28 +01:00
private $modules = [];
public function __get ( $name )
2018-01-02 21:26:33 +01:00
{
2018-01-03 02:12:28 +01:00
if ( starts_with ( $name , 'has' ) && strlen ( $name ) > 3 && ctype_upper ( $name [ 3 ])) {
$name = lcfirst ( substr ( $name , 3 ));
return $this -> hasModule ( $name );
}
2018-01-02 21:26:33 +01:00
2018-01-03 02:12:28 +01:00
if ( $this -> hasModule ( $name )) {
return $this -> modules [ $name ];
2018-01-02 21:26:33 +01:00
}
2018-01-03 02:12:28 +01:00
throw new \Exception ( 'Invalid property.' );
2018-01-02 21:57:06 +01:00
}
2018-01-03 02:12:28 +01:00
protected function __construct ( $config = null )
2018-01-02 21:57:06 +01:00
{
2018-01-03 02:12:28 +01:00
ExceptionHandler :: register ();
2018-01-02 21:57:06 +01:00
2018-01-03 02:12:28 +01:00
$this -> addModule ( 'router' , new RouteCollection );
$this -> addModule ( 'templating' , new TemplateEngine );
$this -> addModule ( 'config' , new ConfigManager ( $config ));
$this -> templating -> addFilter ( 'json_decode' );
$this -> templating -> addFilter ( 'byte_symbol' );
$this -> templating -> addFunction ( 'byte_symbol' );
$this -> templating -> addFunction ( 'session_id' );
$this -> templating -> addFunction ( 'config' , [ $this -> config , 'get' ]);
$this -> templating -> addFunction ( 'route' , [ $this -> router , 'url' ]);
echo sprintf (
'Running on commit <a href="https://github.com/flashwave/misuzu/commit/%s">%s</a> on branch <a href="https://github.com/flashwave/misuzu/tree/%3$s">%3$s</a>!' ,
self :: gitCommitHash ( true ),
self :: gitCommitHash (),
self :: gitBranch ()
);
2018-01-02 21:57:06 +01:00
}
2018-01-03 02:12:28 +01:00
public function __destruct ()
2018-01-02 21:26:33 +01:00
{
2018-01-03 02:12:28 +01:00
ExceptionHandler :: unregister ();
2018-01-02 21:26:33 +01:00
}
2018-01-03 02:12:28 +01:00
public function debug ( bool $mode ) : void
2018-01-02 21:26:33 +01:00
{
2018-01-03 02:12:28 +01:00
ExceptionHandler :: debug ( $mode );
2018-01-02 21:26:33 +01:00
2018-01-03 02:12:28 +01:00
if ( $this -> hasTemplating ) {
$this -> templating -> debug ( $mode );
}
2018-01-02 21:26:33 +01:00
}
2018-01-03 02:12:28 +01:00
public function addModule ( string $name , $module ) : void
2018-01-02 21:26:33 +01:00
{
2018-01-03 02:12:28 +01:00
if ( $this -> hasModule ( $name )) {
throw new \Exception ( 'This module has already been registered.' );
}
$this -> modules [ $name ] = $module ;
2018-01-02 21:26:33 +01:00
}
2018-01-03 02:12:28 +01:00
public function hasModule ( string $name ) : bool
2018-01-02 21:26:33 +01:00
{
2018-01-03 02:12:28 +01:00
return array_key_exists ( $name , $this -> modules ) && ! is_null ( $this -> modules [ $name ]);
2018-01-02 21:26:33 +01:00
}
}