GearmanSettings.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Mmoreramerino\GearmanBundle\Service;
  3. use Symfony\Component\Yaml\Parser;
  4. use Symfony\Component\DependencyInjection\ContainerAware;
  5. use Mmoreramerino\GearmanBundle\Exceptions\SettingsNotLoadedException;
  6. use Mmoreramerino\GearmanBundle\Exceptions\NoSettingsFileExistsException;
  7. /**
  8. * Class GearmanSettings
  9. *
  10. * @author Marc Morera <marc@ulabox.com>
  11. */
  12. class GearmanSettings extends ContainerAware
  13. {
  14. /**
  15. * Settings defined into settings file
  16. *
  17. * @var Array
  18. */
  19. private $settings = null;
  20. /**
  21. * Return Gearman settings, previously loaded by method load()
  22. * If settings are not loaded, a SettingsNotLoadedException Exception is thrown
  23. *
  24. * @return array Settings getted from file
  25. */
  26. public function getSettings()
  27. {
  28. if (null === $this->settings) {
  29. throw new SettingsNotLoadedException();
  30. }
  31. return $this->settings;
  32. }
  33. /**
  34. * Get yaml file and load all settings for Gearman engine
  35. *
  36. * @param type $settingsPath Resource path to get settings
  37. * @return array Settings
  38. */
  39. public function loadSettings($settingsPath)
  40. {
  41. if (!file_exists($settingsPath)) {
  42. throw new NoSettingsFileExistsException($settingsPath);
  43. }
  44. $yaml = new Parser();
  45. $this->settings = $yaml->parse(file_get_contents($settingsPath));
  46. return $this->settings;
  47. }
  48. }