/** * Boot service provider to initiate global container. */ public function register() { /** * Some spiral functions require global/static container, for example it provides you ability to * write code like: * new Post(); * * In other scenario you with either be required to use document function of ODM component or * provide odm instance into your document explicitly: * * new Post([], null, $odm); * * @var Container $container */ SharedContainer::initContainer($container = new Container()); //Container bindings $container->bind(FactoryInterface::class, $container); $container->bind(ResolverInterface::class, $container); $container->bind(ContainerInterface::class, $container); //Since laravel uses this method for bindings, we can use it too $container->bind(TokenizerInterface::class, Tokenizer::class); $container->bind(ClassLocatorInterface::class, ClassLocator::class); //Spiral has it's own validation mechanism which is represented by a simple interface //we can wrap laravel validation functionality and rules $container->bind(ValidatorInterface::class, LaravelValidator::class); //Required for tokenizer to read file $container->bind(FilesInterface::class, FileManager::class); //Laravel also uses it's own configuration source, let's define our wrapper in spiral //container, default settings will use folder "spiral" under config directory to prevent //collisions $container->bindSingleton(ConfiguratorInterface::class, LaravelConfigurator::class); //ODM and some other components also use so called application memory (see doc) to store //behaviour schemas, we can use simple wrapper $container->bindSingleton(HippocampusInterface::class, $container->make(Memory::class, ['directory' => storage_path('/')])); //Ok, now can define our ODM as singleton $this->app->singleton(ODM::class, function () use($container) { //Container will do the rest, since ODM stated as singleton we //will always get same instance return $container->get(ODM::class); }); }
/** * Core class will extend default spiral container and initiate set of directories. You must * provide application, libraries and root directories to constructor. * * @param array $directories Core directories list. */ public function __construct(array $directories) { //Container constructing parent::__construct(); $this->directories = $directories + ['public' => $directories['root'] . '/webroot', 'config' => $directories['application'] . '/config', 'runtime' => $directories['application'] . '/runtime', 'cache' => $directories['application'] . '/runtime/cache']; if (empty($this->environment)) { //This is spiral shortcut to set environment, can be redefined by custom application class. $filename = $this->directory('runtime') . '/environment.php'; $this->setEnvironment(file_exists($filename) ? require $filename : self::DEVELOPMENT); } date_default_timezone_set($this->timezone); }
/** * Return a URL safe version of a string. * * @param string $string * @param string $separator * @return string */ public static function urlSlug($string, $separator = '-') { return Container::container()->get(Slugify::class)->slugify($string, $separator); }