diff --git a/framework/core/composer.json b/framework/core/composer.json index 25f33b8f3..7440410d5 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -9,7 +9,19 @@ ], "require": { "php": ">=5.4.0", + "illuminate/bus": "5.1.*", + "illuminate/cache": "5.1.*", + "illuminate/config": "5.1.*", + "illuminate/container": "5.1.*", + "illuminate/contracts": "5.1.*", + "illuminate/database": "5.1.*", + "illuminate/events": "5.1.*", + "illuminate/filesystem": "5.1.*", + "illuminate/hashing": "5.1.*", + "illuminate/mail": "5.1.*", "illuminate/support": "5.1.*", + "illuminate/validation": "5.1.*", + "illuminate/view": "5.1.*", "tobscure/json-api": "^0.1.1", "oyejorge/less.php": "~1.5", "intervention/image": "^2.3.0", @@ -20,13 +32,15 @@ "dflydev/fig-cookies": "^1.0" }, "require-dev": { - "fzaninotto/faker": "1.4.0", "squizlabs/php_codesniffer": "2.*", "phpspec/phpspec": "^2.2" }, "autoload": { "psr-4": { "Flarum\\": "src/" - } + }, + "files": [ + "src/helpers.php" + ] } } diff --git a/framework/core/src/Core/Application.php b/framework/core/src/Core/Application.php index 3c6827c1c..70894fda7 100644 --- a/framework/core/src/Core/Application.php +++ b/framework/core/src/Core/Application.php @@ -1,40 +1,146 @@ -basePath = $basePath; + $this->registerBaseBindings(); + + $this->registerBaseServiceProviders(); + + $this->registerCoreContainerAliases(); + + if ($basePath) { + $this->setBasePath($basePath); + } } /** @@ -44,7 +150,136 @@ class Application extends Container implements LaravelApplication */ public function version() { - return '1.0.dev'; + return static::VERSION; + } + + /** + * Register the basic bindings into the container. + * + * @return void + */ + protected function registerBaseBindings() + { + static::setInstance($this); + + $this->instance('app', $this); + + $this->instance('Illuminate\Container\Container', $this); + } + + /** + * Register all of the base service providers. + * + * @return void + */ + protected function registerBaseServiceProviders() + { + $this->register(new EventServiceProvider($this)); + } + + /** + * Run the given array of bootstrap classes. + * + * @param array $bootstrappers + * @return void + */ + public function bootstrapWith(array $bootstrappers) + { + $this->hasBeenBootstrapped = true; + + foreach ($bootstrappers as $bootstrapper) { + $this['events']->fire('bootstrapping: '.$bootstrapper, [$this]); + + $this->make($bootstrapper)->bootstrap($this); + + $this['events']->fire('bootstrapped: '.$bootstrapper, [$this]); + } + } + + /** + * Register a callback to run after loading the environment. + * + * @param \Closure $callback + * @return void + */ + public function afterLoadingEnvironment(Closure $callback) + { + return $this->afterBootstrapping( + 'Illuminate\Foundation\Bootstrap\DetectEnvironment', $callback + ); + } + + /** + * Register a callback to run before a bootstrapper. + * + * @param string $bootstrapper + * @param Closure $callback + * @return void + */ + public function beforeBootstrapping($bootstrapper, Closure $callback) + { + $this['events']->listen('bootstrapping: '.$bootstrapper, $callback); + } + + /** + * Register a callback to run after a bootstrapper. + * + * @param string $bootstrapper + * @param Closure $callback + * @return void + */ + public function afterBootstrapping($bootstrapper, Closure $callback) + { + $this['events']->listen('bootstrapped: '.$bootstrapper, $callback); + } + + /** + * Determine if the application has been bootstrapped before. + * + * @return bool + */ + public function hasBeenBootstrapped() + { + return $this->hasBeenBootstrapped; + } + + /** + * Set the base path for the application. + * + * @param string $basePath + * @return $this + */ + public function setBasePath($basePath) + { + $this->basePath = rtrim($basePath, '\/'); + + $this->bindPathsInContainer(); + + return $this; + } + + /** + * Bind all of the application paths in the container. + * + * @return void + */ + protected function bindPathsInContainer() + { + $this->instance('path', $this->path()); + + foreach (['base', 'config', 'database', 'lang', 'public', 'storage'] as $path) { + $this->instance('path.'.$path, $this->{$path.'Path'}()); + } + } + + /** + * Get the path to the application "app" directory. + * + * @return string + */ + public function path() + { + return $this->basePath.DIRECTORY_SEPARATOR.'app'; } /** @@ -57,6 +292,132 @@ class Application extends Container implements LaravelApplication return $this->basePath; } + /** + * Get the path to the application configuration files. + * + * @return string + */ + public function configPath() + { + return $this->basePath.DIRECTORY_SEPARATOR.'config'; + } + + /** + * Get the path to the database directory. + * + * @return string + */ + public function databasePath() + { + return $this->databasePath ?: $this->basePath.DIRECTORY_SEPARATOR.'database'; + } + + /** + * Set the database directory. + * + * @param string $path + * @return $this + */ + public function useDatabasePath($path) + { + $this->databasePath = $path; + + $this->instance('path.database', $path); + + return $this; + } + + /** + * Get the path to the language files. + * + * @return string + */ + public function langPath() + { + return $this->basePath.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'lang'; + } + + /** + * Get the path to the public / web directory. + * + * @return string + */ + public function publicPath() + { + return $this->basePath.DIRECTORY_SEPARATOR.'public'; + } + + /** + * Get the path to the storage directory. + * + * @return string + */ + public function storagePath() + { + return $this->storagePath ?: $this->basePath.DIRECTORY_SEPARATOR.'storage'; + } + + /** + * Set the storage directory. + * + * @param string $path + * @return $this + */ + public function useStoragePath($path) + { + $this->storagePath = $path; + + $this->instance('path.storage', $path); + + return $this; + } + + /** + * Get the path to the environment file directory. + * + * @return string + */ + public function environmentPath() + { + return $this->environmentPath ?: $this->basePath; + } + + /** + * Set the directory for the environment file. + * + * @param string $path + * @return $this + */ + public function useEnvironmentPath($path) + { + $this->environmentPath = $path; + + return $this; + } + + /** + * Set the environment file to be loaded during bootstrapping. + * + * @param string $file + * @return $this + */ + public function loadEnvironmentFrom($file) + { + $this->environmentFile = $file; + + return $this; + } + + /** + * Get the environment file the application is using. + * + * @return string + */ + public function environmentFile() + { + return $this->environmentFile ?: '.env'; + } + /** * Get or check the current application environment. * @@ -69,13 +430,46 @@ class Application extends Container implements LaravelApplication } /** - * Determine if the application is currently down for maintenance. + * Determine if application is in local environment. * * @return bool */ - public function isDownForMaintenance() + public function isLocal() { - return false; + return $this['env'] == 'local'; + } + + /** + * Detect the application's current environment. + * + * @param \Closure $callback + * @return string + */ + public function detectEnvironment(Closure $callback) + { + $args = isset($_SERVER['argv']) ? $_SERVER['argv'] : null; + + return $this['env'] = (new EnvironmentDetector())->detect($callback, $args); + } + + /** + * Determine if we are running in the console. + * + * @return bool + */ + public function runningInConsole() + { + return php_sapi_name() == 'cli'; + } + + /** + * Determine if we are running unit tests. + * + * @return bool + */ + public function runningUnitTests() + { + return $this['env'] == 'testing'; } /** @@ -85,34 +479,200 @@ class Application extends Container implements LaravelApplication */ public function registerConfiguredProviders() { - foreach ($this->serviceProviders as $provider) { - $provider->register(); - } + $manifestPath = $this->getCachedServicesPath(); + + (new ProviderRepository($this, new Filesystem, $manifestPath)) + ->load($this->config['app.providers']); } /** * Register a service provider with the application. * - * @param \Illuminate\Support\ServiceProvider|string $provider - * @param array $options - * @param bool $force + * @param \Illuminate\Support\ServiceProvider|string $provider + * @param array $options + * @param bool $force * @return \Illuminate\Support\ServiceProvider */ - public function register($provider, $options = array(), $force = false) + public function register($provider, $options = [], $force = false) { + if ($registered = $this->getProvider($provider) && ! $force) { + return $registered; + } + + // If the given "provider" is a string, we will resolve it, passing in the + // application instance automatically for the developer. This is simply + // a more convenient way of specifying your service provider classes. + if (is_string($provider)) { + $provider = $this->resolveProviderClass($provider); + } + + $provider->register(); + + // Once we have registered the service we will iterate through the options + // and set each of them on the application so they will be available on + // the actual loading of the service objects and for developer usage. + foreach ($options as $key => $value) { + $this[$key] = $value; + } + + $this->markAsRegistered($provider); + + // If the application has already booted, we will call this boot method on + // the provider class so it has an opportunity to do its boot logic and + // will be ready for any usage by the developer's application logics. + if ($this->booted) { + $this->bootProvider($provider); + } + + return $provider; + } + + /** + * Get the registered service provider instance if it exists. + * + * @param \Illuminate\Support\ServiceProvider|string $provider + * @return \Illuminate\Support\ServiceProvider|null + */ + public function getProvider($provider) + { + $name = is_string($provider) ? $provider : get_class($provider); + + return Arr::first($this->serviceProviders, function ($key, $value) use ($name) { + return $value instanceof $name; + }); + } + + /** + * Resolve a service provider instance from the class name. + * + * @param string $provider + * @return \Illuminate\Support\ServiceProvider + */ + public function resolveProviderClass($provider) + { + return new $provider($this); + } + + /** + * Mark the given provider as registered. + * + * @param \Illuminate\Support\ServiceProvider $provider + * @return void + */ + protected function markAsRegistered($provider) + { + $this['events']->fire($class = get_class($provider), [$provider]); + $this->serviceProviders[] = $provider; + + $this->loadedProviders[$class] = true; + } + + /** + * Load and boot all of the remaining deferred providers. + * + * @return void + */ + public function loadDeferredProviders() + { + // We will simply spin through each of the deferred providers and register each + // one and boot them if the application has booted. This should make each of + // the remaining services available to this application for immediate use. + foreach ($this->deferredServices as $service => $provider) { + $this->loadDeferredProvider($service); + } + + $this->deferredServices = []; + } + + /** + * Load the provider for a deferred service. + * + * @param string $service + * @return void + */ + public function loadDeferredProvider($service) + { + if (! isset($this->deferredServices[$service])) { + return; + } + + $provider = $this->deferredServices[$service]; + + // If the service provider has not already been loaded and registered we can + // register it with the application and remove the service from this list + // of deferred services, since it will already be loaded on subsequent. + if (! isset($this->loadedProviders[$provider])) { + $this->registerDeferredProvider($provider, $service); + } } /** * Register a deferred provider and service. * - * @param string $provider - * @param string $service + * @param string $provider + * @param string $service * @return void */ public function registerDeferredProvider($provider, $service = null) { - $this->register($provider); + // Once the provider that provides the deferred service has been registered we + // will remove it from our local list of the deferred services with related + // providers so that this container does not try to resolve it out again. + if ($service) { + unset($this->deferredServices[$service]); + } + + $this->register($instance = new $provider($this)); + + if (! $this->booted) { + $this->booting(function () use ($instance) { + $this->bootProvider($instance); + }); + } + } + + /** + * Resolve the given type from the container. + * + * (Overriding Container::make) + * + * @param string $abstract + * @param array $parameters + * @return mixed + */ + public function make($abstract, array $parameters = []) + { + $abstract = $this->getAlias($abstract); + + if (isset($this->deferredServices[$abstract])) { + $this->loadDeferredProvider($abstract); + } + + return parent::make($abstract, $parameters); + } + + /** + * Determine if the given abstract type has been bound. + * + * (Overriding Container::bound) + * + * @param string $abstract + * @return bool + */ + public function bound($abstract) + { + return isset($this->deferredServices[$abstract]) || parent::bound($abstract); + } + + /** + * Determine if the application has booted. + * + * @return bool + */ + public function isBooted() + { + return $this->booted; } /** @@ -122,47 +682,418 @@ class Application extends Container implements LaravelApplication */ public function boot() { - $this->fireListeners($this->bootingListeners); - - foreach ($this->serviceProviders as $provider) { - $this->call([$provider, 'boot']); + if ($this->booted) { + return; } - $this->fireListeners($this->bootedListeners); + // Once the application has booted we will also fire some "booted" callbacks + // for any listeners that need to do work after this initial booting gets + // finished. This is useful when ordering the boot-up processes we run. + $this->fireAppCallbacks($this->bootingCallbacks); + + array_walk($this->serviceProviders, function ($p) { + $this->bootProvider($p); + }); + + $this->booted = true; + + $this->fireAppCallbacks($this->bootedCallbacks); + } + + /** + * Boot the given service provider. + * + * @param \Illuminate\Support\ServiceProvider $provider + * @return void + */ + protected function bootProvider(ServiceProvider $provider) + { + if (method_exists($provider, 'boot')) { + return $this->call([$provider, 'boot']); + } } /** * Register a new boot listener. * - * @param mixed $callback + * @param mixed $callback * @return void */ public function booting($callback) { - $this->bootingListeners[] = $callback; + $this->bootingCallbacks[] = $callback; } /** * Register a new "booted" listener. * - * @param mixed $callback + * @param mixed $callback * @return void */ public function booted($callback) { - $this->bootedListeners[] = $callback; + $this->bootedCallbacks[] = $callback; + + if ($this->isBooted()) { + $this->fireAppCallbacks([$callback]); + } } /** - * Fire the given array of listener callbacks. + * Call the booting callbacks for the application. * - * @param array $listeners + * @param array $callbacks * @return void */ - protected function fireListeners(array $listeners) + protected function fireAppCallbacks(array $callbacks) { - foreach ($listeners as $listener) { - $listener($this); + foreach ($callbacks as $callback) { + call_user_func($callback, $this); } } + + /** + * {@inheritdoc} + */ + public function handle(SymfonyRequest $request, $type = self::MASTER_REQUEST, $catch = true) + { + return $this['Illuminate\Contracts\Http\Kernel']->handle(Request::createFromBase($request)); + } + + /** + * Determine if middleware has been disabled for the application. + * + * @return bool + */ + public function shouldSkipMiddleware() + { + return $this->bound('middleware.disable') && + $this->make('middleware.disable') === true; + } + + /** + * Determine if the application configuration is cached. + * + * @return bool + */ + public function configurationIsCached() + { + return $this['files']->exists($this->getCachedConfigPath()); + } + + /** + * Get the path to the configuration cache file. + * + * @return string + */ + public function getCachedConfigPath() + { + return $this->basePath().'/bootstrap/cache/config.php'; + } + + /** + * Determine if the application routes are cached. + * + * @return bool + */ + public function routesAreCached() + { + return $this['files']->exists($this->getCachedRoutesPath()); + } + + /** + * Get the path to the routes cache file. + * + * @return string + */ + public function getCachedRoutesPath() + { + return $this->basePath().'/bootstrap/cache/routes.php'; + } + + /** + * Get the path to the cached "compiled.php" file. + * + * @return string + */ + public function getCachedCompilePath() + { + return $this->basePath().'/bootstrap/cache/compiled.php'; + } + + /** + * Get the path to the cached services.json file. + * + * @return string + */ + public function getCachedServicesPath() + { + return $this->basePath().'/bootstrap/cache/services.json'; + } + + /** + * Determine if the application is currently down for maintenance. + * + * @return bool + */ + public function isDownForMaintenance() + { + return file_exists($this->storagePath().'/framework/down'); + } + + /** + * Throw an HttpException with the given data. + * + * @param int $code + * @param string $message + * @param array $headers + * @return void + * + * @throws \Symfony\Component\HttpKernel\Exception\HttpException + */ + public function abort($code, $message = '', array $headers = []) + { + if ($code == 404) { + throw new NotFoundHttpException($message); + } + + throw new HttpException($code, $message, null, $headers); + } + + /** + * Register a terminating callback with the application. + * + * @param \Closure $callback + * @return $this + */ + public function terminating(Closure $callback) + { + $this->terminatingCallbacks[] = $callback; + + return $this; + } + + /** + * Terminate the application. + * + * @return void + */ + public function terminate() + { + foreach ($this->terminatingCallbacks as $terminating) { + $this->call($terminating); + } + } + + /** + * Get the service providers that have been loaded. + * + * @return array + */ + public function getLoadedProviders() + { + return $this->loadedProviders; + } + + /** + * Get the application's deferred services. + * + * @return array + */ + public function getDeferredServices() + { + return $this->deferredServices; + } + + /** + * Set the application's deferred services. + * + * @param array $services + * @return void + */ + public function setDeferredServices(array $services) + { + $this->deferredServices = $services; + } + + /** + * Add an array of services to the application's deferred services. + * + * @param array $services + * @return void + */ + public function addDeferredServices(array $services) + { + $this->deferredServices = array_merge($this->deferredServices, $services); + } + + /** + * Determine if the given service is a deferred service. + * + * @param string $service + * @return bool + */ + public function isDeferredService($service) + { + return isset($this->deferredServices[$service]); + } + + /** + * Define a callback to be used to configure Monolog. + * + * @param callable $callback + * @return $this + */ + public function configureMonologUsing(callable $callback) + { + $this->monologConfigurator = $callback; + + return $this; + } + + /** + * Determine if the application has a custom Monolog configurator. + * + * @return bool + */ + public function hasMonologConfigurator() + { + return ! is_null($this->monologConfigurator); + } + + /** + * Get the custom Monolog configurator for the application. + * + * @return callable + */ + public function getMonologConfigurator() + { + return $this->monologConfigurator; + } + + /** + * Get the current application locale. + * + * @return string + */ + public function getLocale() + { + return $this['config']->get('app.locale'); + } + + /** + * Set the current application locale. + * + * @param string $locale + * @return void + */ + public function setLocale($locale) + { + $this['config']->set('app.locale', $locale); + + $this['translator']->setLocale($locale); + + $this['events']->fire('locale.changed', [$locale]); + } + + /** + * Register the core class aliases in the container. + * + * @return void + */ + public function registerCoreContainerAliases() + { + $aliases = [ + 'app' => ['Illuminate\Foundation\Application', 'Illuminate\Contracts\Container\Container', 'Illuminate\Contracts\Foundation\Application'], + 'auth' => 'Illuminate\Auth\AuthManager', + 'auth.driver' => ['Illuminate\Auth\Guard', 'Illuminate\Contracts\Auth\Guard'], + 'auth.password.tokens' => 'Illuminate\Auth\Passwords\TokenRepositoryInterface', + 'blade.compiler' => 'Illuminate\View\Compilers\BladeCompiler', + 'cache' => ['Illuminate\Cache\CacheManager', 'Illuminate\Contracts\Cache\Factory'], + 'cache.store' => ['Illuminate\Cache\Repository', 'Illuminate\Contracts\Cache\Repository'], + 'config' => ['Illuminate\Config\Repository', 'Illuminate\Contracts\Config\Repository'], + 'cookie' => ['Illuminate\Cookie\CookieJar', 'Illuminate\Contracts\Cookie\Factory', 'Illuminate\Contracts\Cookie\QueueingFactory'], + 'encrypter' => ['Illuminate\Encryption\Encrypter', 'Illuminate\Contracts\Encryption\Encrypter'], + 'db' => 'Illuminate\Database\DatabaseManager', + 'events' => ['Illuminate\Events\Dispatcher', 'Illuminate\Contracts\Events\Dispatcher'], + 'files' => 'Illuminate\Filesystem\Filesystem', + 'filesystem' => ['Illuminate\Filesystem\FilesystemManager', 'Illuminate\Contracts\Filesystem\Factory'], + 'filesystem.disk' => 'Illuminate\Contracts\Filesystem\Filesystem', + 'filesystem.cloud' => 'Illuminate\Contracts\Filesystem\Cloud', + 'hash' => 'Illuminate\Contracts\Hashing\Hasher', + 'translator' => ['Illuminate\Translation\Translator', 'Symfony\Component\Translation\TranslatorInterface'], + 'log' => ['Illuminate\Log\Writer', 'Illuminate\Contracts\Logging\Log', 'Psr\Log\LoggerInterface'], + 'mailer' => ['Illuminate\Mail\Mailer', 'Illuminate\Contracts\Mail\Mailer', 'Illuminate\Contracts\Mail\MailQueue'], + 'auth.password' => ['Illuminate\Auth\Passwords\PasswordBroker', 'Illuminate\Contracts\Auth\PasswordBroker'], + 'queue' => ['Illuminate\Queue\QueueManager', 'Illuminate\Contracts\Queue\Factory', 'Illuminate\Contracts\Queue\Monitor'], + 'queue.connection' => 'Illuminate\Contracts\Queue\Queue', + 'redirect' => 'Illuminate\Routing\Redirector', + 'redis' => ['Illuminate\Redis\Database', 'Illuminate\Contracts\Redis\Database'], + 'request' => 'Illuminate\Http\Request', + 'router' => ['Illuminate\Routing\Router', 'Illuminate\Contracts\Routing\Registrar'], + 'session' => 'Illuminate\Session\SessionManager', + 'session.store' => ['Illuminate\Session\Store', 'Symfony\Component\HttpFoundation\Session\SessionInterface'], + 'url' => ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'], + 'validator' => ['Illuminate\Validation\Factory', 'Illuminate\Contracts\Validation\Factory'], + 'view' => ['Illuminate\View\Factory', 'Illuminate\Contracts\View\Factory'], + ]; + + foreach ($aliases as $key => $aliases) { + foreach ((array) $aliases as $alias) { + $this->alias($key, $alias); + } + } + } + + /** + * Flush the container of all bindings and resolved instances. + * + * @return void + */ + public function flush() + { + parent::flush(); + + $this->loadedProviders = []; + } + + /** + * Get the used kernel object. + * + * @return \Illuminate\Contracts\Console\Kernel|\Illuminate\Contracts\Http\Kernel + */ + protected function getKernel() + { + $kernelContract = $this->runningInConsole() + ? 'Illuminate\Contracts\Console\Kernel' + : 'Illuminate\Contracts\Http\Kernel'; + + return $this->make($kernelContract); + } + + /** + * Get the application namespace. + * + * @return string + * + * @throws \RuntimeException + */ + public function getNamespace() + { + if (! is_null($this->namespace)) { + return $this->namespace; + } + + $composer = json_decode(file_get_contents(base_path().'/composer.json'), true); + + foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) { + foreach ((array) $path as $pathChoice) { + if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) { + return $this->namespace = $namespace; + } + } + } + + throw new RuntimeException('Unable to detect application namespace.'); + } } diff --git a/framework/core/src/Locale/LocaleManager.php b/framework/core/src/Locale/LocaleManager.php index 7fe1051b9..82a73d183 100644 --- a/framework/core/src/Locale/LocaleManager.php +++ b/framework/core/src/Locale/LocaleManager.php @@ -27,28 +27,16 @@ class LocaleManager public function addTranslations($locale, $translations) { - if (! isset($this->translations[$locale])) { - $this->translations[$locale] = []; - } - $this->translations[$locale][] = $translations; } public function addJsFile($locale, $js) { - if (! isset($this->js[$locale])) { - $this->js[$locale] = []; - } - $this->js[$locale][] = $js; } public function addConfig($locale, $config) { - if (! isset($this->config[$locale])) { - $this->config[$locale] = []; - } - $this->config[$locale][] = $config; } @@ -79,4 +67,19 @@ class LocaleManager return $files; } + + public function getConfig($locale) + { + if (empty($this->config[$locale])) { + return []; + } + + $config = []; + + foreach ($this->config[$locale] as $file) { + $config = array_merge($config, include $file); + } + + return $config; + } } diff --git a/framework/core/src/Locale/LocaleServiceProvider.php b/framework/core/src/Locale/LocaleServiceProvider.php index 1cc846b2f..5806012b5 100644 --- a/framework/core/src/Locale/LocaleServiceProvider.php +++ b/framework/core/src/Locale/LocaleServiceProvider.php @@ -34,5 +34,11 @@ class LocaleServiceProvider extends ServiceProvider $this->app->singleton('Flarum\Locale\LocaleManager'); $this->app->alias('Flarum\Locale\LocaleManager', 'flarum.localeManager'); + + $this->app->bind('translator', function ($app) { + $locales = $app->make('flarum.localeManager'); + + return new Translator($locales->getTranslations('en'), $locales->getConfig('en')['plural']); + }); } } diff --git a/framework/core/src/Locale/Translator.php b/framework/core/src/Locale/Translator.php index f01764f85..ace8f609f 100644 --- a/framework/core/src/Locale/Translator.php +++ b/framework/core/src/Locale/Translator.php @@ -1,11 +1,9 @@ plural = $plural; } - public function plural($count) + protected function plural($count) { - return {$this->plural}($count); + $plural = $this->plural; + + return $plural($count); } - public function translate($key, array $input = []) + public function getLocale() { - $translation = array_get($this->translations, $key); + // + } - if (is_array($translation) && isset($input['count'])) { - $translation = $translation[$this->plural($input['count'])]; + public function setLocale($locale) + { + // + } + + public function trans($id, array $parameters = [], $domain = null, $locale = null) + { + $translation = array_get($this->translations, $id); + + if (is_array($translation) && isset($parameters['count'])) { + $translation = $translation[$this->plural($parameters['count'])]; } if (is_string($translation)) { - foreach ($input as $k => $v) { + foreach ($parameters as $k => $v) { $translation = str_replace('{'.$k.'}', $v, $translation); } return $translation; - } else { - return $key; } + + return $id; + } + + public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) + { + $parameters['count'] = $number; + + return $this->trans($id, $parameters, $domain, $locale); } } diff --git a/framework/core/src/helpers.php b/framework/core/src/helpers.php new file mode 100644 index 000000000..1fe28f1d1 --- /dev/null +++ b/framework/core/src/helpers.php @@ -0,0 +1,88 @@ +make($make, $parameters); + } +} + +if (! function_exists('app_path')) { + /** + * Get the path to the application folder. + * + * @param string $path + * @return string + */ + function app_path($path = '') + { + return app('path').($path ? DIRECTORY_SEPARATOR.$path : $path); + } +} + +if (! function_exists('base_path')) { + /** + * Get the path to the base of the install. + * + * @param string $path + * @return string + */ + function base_path($path = '') + { + return app()->basePath().($path ? DIRECTORY_SEPARATOR.$path : $path); + } +} + +if (! function_exists('public_path')) { + /** + * Get the path to the public folder. + * + * @param string $path + * @return string + */ + function public_path($path = '') + { + return app()->make('path.public').($path ? DIRECTORY_SEPARATOR.$path : $path); + } +} + +if (! function_exists('storage_path')) { + /** + * Get the path to the storage folder. + * + * @param string $path + * @return string + */ + function storage_path($path = '') + { + return app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path); + } +} + +if (! function_exists('event')) { + /** + * Fire an event and call the listeners. + * + * @param string|object $event + * @param mixed $payload + * @param bool $halt + * @return array|null + */ + function event($event, $payload = [], $halt = false) + { + return app('events')->fire($event, $payload, $halt); + } +}