Skip to content

Instantly share code, notes, and snippets.

@tractorcow
Last active May 17, 2017 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tractorcow/c0656a334c13eac8ae252610d226a1c6 to your computer and use it in GitHub Desktop.
Save tractorcow/c0656a334c13eac8ae252610d226a1c6 to your computer and use it in GitHub Desktop.
app-rfc-v2
<?php
namespace SilverStripe\Core;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Config\Collections\ConfigCollectionInterface;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\ConfigLoader;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Injector\InjectorLoader;
use SilverStripe\Core\Manifest\ClassLoader;
use SilverStripe\Core\Manifest\ClassManifest;
use SilverStripe\Core\Manifest\Module;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Core\Manifest\ModuleManifest;
use SilverStripe\ORM\Connect\Database;
use SilverStripe\ORM\Connect\DBSchemaManager;
use SilverStripe\View\ThemeResourceLoader;
/**
* Core app object
*/
class App
{
/**
* @var App[] Stack of nested app instances
*/
protected static $instances = [];
/**
* Note: loader contains nested injectors for the current app
* Assists with nesting of injector instances
*
* @var InjectorLoader
*/
protected $injectorLoader = null;
/** @var Configloader */
protected $configLoader = null;
/** @var Database[] List of database connections */
protected $databaseConnections = [];
/** @var string */
protected $enviroment = null;
/**
* Project name
*
* @var string
*/
protected $project = 'mysite';
/** @var Session */
protected $session = null;
/** @var HTTPRequest */
protected $request = null;
/** @var ThemeResourceLoader */
protected $themeResourceLoader = null;
/** @var ClassLoader */
protected $classLoader = null;
/**
* Set to true once init() is called
*
* @var bool
*/
protected $started = false;
/**
* App constructor (Intentionally empty)
*/
public function __construct() { }
// ----------------- Core app accessors --------------------
/**
* Get app instance
*
* @return static
*/
static function inst() {}
/**
* Starts this app, copied from Core.php
*
* Performs:
* - Load config
* - Include all _config.php
* - Start error handling
*
* If app is started this will silently return
*
* @return $this
*/
public function start() {}
/**
* Nest app state; Allows a new app instance to be specified.
* By default the current app will be duplicated with a nested Injector / Config
*
* Note that nested applications reset the nesting stack for ALL nested sub-components.
* I.e. A newly nested app has a zero-depth nesting stack for Injector / Config.
*
* Injector and Config internal references can be nested several times for each app nesting stage.
*
* @param App $inst
* @return static
*/
public static function nest(App $inst = null) {}
/**
* Pops the top level app from the stack
*
* @return mixed
*/
public static function unnnest() {}
// ----------------- Class manifest accessors --------------------
/**
* @return ClassLoader
*/
public function getClassLoader() {}
/**
* @param ClassLoader $loader
* @return $this
*/
public function setClassLoader(ClassLoader $loader) {}
/**
* Shortcut for getClassLoader()->getManifest()
* @return ClassManifest
*/
public function getClassManifest() {}
// ----------------- Module accessors --------------------
/**
* Shortcut for getModuleLoader()->getManifest()
* @return ModuleManifest
*/
public function getModuleManifest() {}
/**
* @return ModuleLoader
*/
public function getModuleLoader() {}
/**
* Shortcut for getModuleManifest()->getModule($name)
*
* @param string $name
* @return Module
*/
public function getModule($name) {}
// ----------------- Config accessors --------------------
/**
* Get current config collection.
* Shortcut for getConfigLoader()->getManifest()
*
* @return ConfigCollectionInterface
*/
public function getConfig() {}
/**
* Used by Config::nest() and Config::modify() to push nested config to app.
* Relies on Config::nest() to provide logic necessary to generate new config
*
* @return ConfigLoader
*/
public function getConfigLoader() {}
// ----------------- Injector accessors --------------------
/**
* Get injector instance
* Shortcut for getInjectorLoader()->getInjector()
*
* @return Injector
*/
public function getInjector() {}
/**
* Used by Injector::nest() to push nested injector to app
*
* @return InjectorLoader
*/
public function getInjectorLoader() {}
/**
* @param InjectorLoader $loader
* @return null
*/
public function setInjectorLoader(InjectorLoader $loader) {}
/**
* Shortcut for getInjector()->get()
* App::inst()->getService() should replace all singelton() global methods.
*
* @param string $name
* @param bool $asSingleton
* @param array $constructorArgs
* @return mixed
*/
public function getService($name, $asSingleton = true, $constructorArgs = null) {}
/**
* Shortcut for:
* App::inst()
* ->getService(CacheFactory::class)
* ->create(CacheInterface::class, [
* 'namespace' => $namespace,
* ]);
*
* @param string $namespace
* @return CacheInterface
*/
public function getCache($namespace = null) {}
// ----------------- DB accessors --------------------
/**
* Get database connection
*
* @param string $namespace
* @return Database
*/
public function getDatabase($namespace = 'default') {}
/**
* Set database connection
*
* @param Database $connection
* @param string $name
*/
public static function setDatabase(Database $connection, $name = 'default') {}
/**
* Get schema for current database
*
* @param string $namespace
* @return DBSchemaManager
*/
public function getDatabaseSchema($namespace = 'default') {}
// ----------------- Session accessors --------------------
/**
* @return Session
*/
public function getSession() {}
/**
* @param Session $session
* @return $this
*/
public function setSession(Session $session) {}
// ----------------- Request accessors --------------------
/**
* Note: there is intentionally no getResponse() as this is the output of
* a request, not an input state.
*
* @return HTTPRequest
*/
public function getRequest() {}
/**
* @param HttpRequest $request
* @return $this
*/
public function setRequest(HTTPRequest $request) {}
// ----------------- Environment accessors --------------------
/**
* Replaces Director::get_environment_type()
*
* @return string
*/
public function getEnvironment() {}
/**
* Replaces Director::set_environment_type()
*
* @param string $env
* @return mixed
*/
public function setEnvironment($env) {}
/**
* Replaces `global $project`
*
* @return string
*/
public function getProject() {}
/**
* @param string $project
* @return $this
*/
public function setProject($project) {}
// ----------------- Resource loader accessors --------------------
/**
* @return ThemeResourceLoader
*/
public function getThemeResourceLoader() {}
/**
* @param ThemeResourceLoader $loader
* @return $this
*/
public function setThemeResourceLoader(ThemeResourceLoader $loader) {}
}
<?php
if (!App::inst()) {
App::nest(DefaultAppFactory::create());
}
App::inst()->start();
<?php
/**
* Initialises the root app
*/
class DefaultAppFactory {
/**
* Bootstraps root app.
* Copies logic from Core.php
*
* Performs:
* - Creates root inst()
*
* If app already exists, but isn't started ($started = false) it'll start it
* If app already exists and is started this will silently return
*
* @return App Returns root app instance
*/
static function create() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment