Advertisement
Serafim

Untitled

Dec 17th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2. namespace app\support\lib;
  3.  
  4. use Exception;
  5. use ClassLoader;
  6. use BadMethodCallException;
  7. use Illuminate\Support\ServiceProvider;
  8. use Symfony\Component\Debug\Exception\ClassNotFoundException;
  9.  
  10.  
  11.  
  12. /**
  13.  * Class StartupServiceProvider
  14.  * @package app\lib
  15.  *
  16.  * Класс регистрации классов для автозагрузки и автозапуска
  17.  */
  18. class StartupServiceProvider extends ServiceProvider
  19. {
  20.     /**
  21.      * @var array
  22.      */
  23.     protected $startups = [];
  24.  
  25.     /**
  26.      * @var bool
  27.      */
  28.     protected $defer = false;
  29.  
  30.     /**
  31.      * Register package
  32.      */
  33.     public function boot()
  34.     {
  35.         $this->package('app/startup', 'startup');
  36.     }
  37.  
  38.     /**
  39.      * @param $path
  40.      * @return $this
  41.      */
  42.     public function registerPath($path)
  43.     {
  44.         ClassLoader::addDirectories([$path]);
  45.         return $this;
  46.     }
  47.  
  48.     /**
  49.      * @param array $paths
  50.      * @return $this
  51.      */
  52.     public function registerPaths(array $paths)
  53.     {
  54.         ClassLoader::addDirectories($paths);
  55.         return $this;
  56.     }
  57.  
  58.     /**
  59.      * @param $namespace
  60.      * @param $path
  61.      * @return $this
  62.      * @throws ClassNotFoundException
  63.      */
  64.     public function registerStartupPath($namespace, $path)
  65.     {
  66.         $this->startups[$path] = $namespace;
  67.  
  68.         ClassLoader::addDirectories([$path]);
  69.  
  70.         foreach (glob($path . '/*.php') as $file) {
  71.             $class = pathinfo($file, PATHINFO_FILENAME);
  72.             $class = str_replace(['//', '\\\\'], ['\\'], $namespace . '\\' . $class);
  73.  
  74.             try {
  75.                 $instance = new $class;
  76.             } catch (Exception $e) {
  77.                 throw new ClassNotFoundException('Can not load class ' . $class . '. Class not found.', $e);
  78.             }
  79.  
  80.             try {
  81.                 $instance->boot(app('app'));
  82.             } catch (Exception $e) {
  83.                 throw new BadMethodCallException('Can not call boot method for class ' . $class . ': ' . $e->getMessage());
  84.             }
  85.  
  86.         }
  87.         return $this;
  88.     }
  89.  
  90.     /**
  91.      * @param array $paths
  92.      * @return $this
  93.      * @throws ClassNotFoundException
  94.      */
  95.     public function registerStartupPaths(array $paths)
  96.     {
  97.         foreach ($paths as $ns => $path) {
  98.             $this->registerStartupPath($ns, $path);
  99.         }
  100.         return $this;
  101.     }
  102.  
  103.     /**
  104.      * @return array
  105.      */
  106.     public function getStartupPaths()
  107.     {
  108.         return $this->startups;
  109.     }
  110.  
  111.     /**
  112.      * @return $this|void
  113.      */
  114.     public function register()
  115.     {
  116.         $this->app['startup'] = $this->app->share(function ($app) {
  117.             return $this;
  118.         });
  119.         return $this;
  120.     }
  121.  
  122.     /**
  123.      * @return array
  124.      */
  125.     public function provides() { return []; }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement