Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\support\lib;
- use Exception;
- use ClassLoader;
- use BadMethodCallException;
- use Illuminate\Support\ServiceProvider;
- use Symfony\Component\Debug\Exception\ClassNotFoundException;
- /**
- * Class StartupServiceProvider
- * @package app\lib
- *
- * Класс регистрации классов для автозагрузки и автозапуска
- */
- class StartupServiceProvider extends ServiceProvider
- {
- /**
- * @var array
- */
- protected $startups = [];
- /**
- * @var bool
- */
- protected $defer = false;
- /**
- * Register package
- */
- public function boot()
- {
- $this->package('app/startup', 'startup');
- }
- /**
- * @param $path
- * @return $this
- */
- public function registerPath($path)
- {
- ClassLoader::addDirectories([$path]);
- return $this;
- }
- /**
- * @param array $paths
- * @return $this
- */
- public function registerPaths(array $paths)
- {
- ClassLoader::addDirectories($paths);
- return $this;
- }
- /**
- * @param $namespace
- * @param $path
- * @return $this
- * @throws ClassNotFoundException
- */
- public function registerStartupPath($namespace, $path)
- {
- $this->startups[$path] = $namespace;
- ClassLoader::addDirectories([$path]);
- foreach (glob($path . '/*.php') as $file) {
- $class = pathinfo($file, PATHINFO_FILENAME);
- $class = str_replace(['//', '\\\\'], ['\\'], $namespace . '\\' . $class);
- try {
- $instance = new $class;
- } catch (Exception $e) {
- throw new ClassNotFoundException('Can not load class ' . $class . '. Class not found.', $e);
- }
- try {
- $instance->boot(app('app'));
- } catch (Exception $e) {
- throw new BadMethodCallException('Can not call boot method for class ' . $class . ': ' . $e->getMessage());
- }
- }
- return $this;
- }
- /**
- * @param array $paths
- * @return $this
- * @throws ClassNotFoundException
- */
- public function registerStartupPaths(array $paths)
- {
- foreach ($paths as $ns => $path) {
- $this->registerStartupPath($ns, $path);
- }
- return $this;
- }
- /**
- * @return array
- */
- public function getStartupPaths()
- {
- return $this->startups;
- }
- /**
- * @return $this|void
- */
- public function register()
- {
- $this->app['startup'] = $this->app->share(function ($app) {
- return $this;
- });
- return $this;
- }
- /**
- * @return array
- */
- public function provides() { return []; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement