Advertisement
dead__

Untitled

Jan 5th, 2014
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.73 KB | None | 0 0
  1. <?php
  2. namespace App\Lib;
  3.  
  4. use App\Application;
  5.  
  6. class ServerManager
  7. {
  8.     protected $app = null;
  9.  
  10.     public function __construct(Application $app)
  11.     {
  12.         $this->app = $app;
  13.  
  14.         if (empty($this->app['config']['path'])) {
  15.             throw new \Exception('Invalid or empty server path!');
  16.         }
  17.  
  18.         if (empty($this->app['config']['file'])) {
  19.             throw new \Exception('Invalid or empty server file!');
  20.         }
  21.     }
  22.  
  23.     public function getStartCommand()
  24.     {
  25.         if (isset($this->app['config']['commands']) && isset($this->app['config']['commands']['start'])) {
  26.             return $this->parse($this->app['config']['commands']['start']);
  27.         }
  28.  
  29.         return $this->parse('cd %path% && screen -dmS %prefix%%file% ./%file%');
  30.     }
  31.  
  32.     public function start()
  33.     {
  34.         if ($this->isRunning()) {
  35.             throw new \RuntimeException('Server is already running, can\'t start twice.');
  36.         }
  37.  
  38.         $cmd = $this->getStartCommand();
  39.  
  40.         $this->app['system']->execute($cmd);
  41.  
  42.         $this->app['monolog']->addInfo('Server was started (' . $cmd . ')');
  43.     }
  44.  
  45.     public function getStopCommand()
  46.     {
  47.         if (isset($this->app['config']['commands']) && isset($this->app['config']['commands']['stop'])) {
  48.             return $this->parse($this->app['config']['commands']['stop']);
  49.         }
  50.  
  51.         return $this->parse('screen -S %prefix%%file% -p 0 -X quit');
  52.     }
  53.  
  54.     public function stop()
  55.     {
  56.         $cmd = $this->getStopCommand();
  57.         $this->app['system']->execute($cmd);
  58.  
  59.         $this->app['monolog']->addInfo('Server was stopped (' . $cmd . ')');
  60.     }
  61.  
  62.     public function restart()
  63.     {
  64.         if ($this->isRunning()) {
  65.             $this->stop();
  66.             sleep(1);
  67.         }
  68.  
  69.         $this->start();
  70.  
  71.         $this->app['monolog']->addInfo('Server was restarted');
  72.     }
  73.  
  74.     public function getIsRunningCommand()
  75.     {
  76.         if (isset($this->app['config']['commands']) && isset($this->app['config']['commands']['is_running'])) {
  77.             return $this->parse($this->app['config']['commands']['is_running']);
  78.         }
  79.  
  80.         return $this->parse('screen -ls | grep -c %prefix%%file%');
  81.     }
  82.  
  83.     public function isRunning()
  84.     {
  85.         $cmd = $this->getIsRunningCommand();
  86.  
  87.         return (int) $this->app['system']->execute($cmd);
  88.     }
  89.  
  90.     protected function parse($content)
  91.     {
  92.         return str_replace(array('%path%', '%file%', '%prefix%'),
  93.             array(
  94.                 $this->app['config']['path'],
  95.                 $this->app['config']['file'],
  96.                 isset($this->app['config']['prefix']) ? $this->app['config']['prefix'] : ''
  97.             ),
  98.             $content);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement