ProcedureLoader.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Procedure;
  9. use Symfony\Component\Config\FileLocatorInterface;
  10. use JonnyW\PhantomJs\Exception\NotExistsException;
  11. /**
  12. * PHP PhantomJs
  13. *
  14. * @author Jon Wenmoth <contact@jonnyw.me>
  15. */
  16. class ProcedureLoader implements ProcedureLoaderInterface
  17. {
  18. /**
  19. * Procedure factory.
  20. *
  21. * @var mixed
  22. * @access protected
  23. */
  24. protected $procedureFactory;
  25. /**
  26. * File locator.
  27. *
  28. * @var \Symfony\Component\Config\FileLocatorInterface
  29. * @access protected
  30. */
  31. protected $locator;
  32. /**
  33. * Internal constructor.
  34. *
  35. * @access public
  36. * @param \JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface $procedureFactory
  37. * @param \Symfony\Component\Config\FileLocatorInterface $locator
  38. */
  39. public function __construct(ProcedureFactoryInterface $procedureFactory, FileLocatorInterface $locator)
  40. {
  41. $this->procedureFactory = $procedureFactory;
  42. $this->locator = $locator;
  43. }
  44. /**
  45. * Load procedure instance by id.
  46. *
  47. * @access public
  48. * @param string $id
  49. * @return \JonnyW\PhantomJs\Procedure\ProcedureInterface
  50. */
  51. public function load($id)
  52. {
  53. $path = $this->locator->locate(sprintf('%s.proc', $id));
  54. $content = $this->loadFile($path);
  55. $procedure = $this->procedureFactory->createProcedure();
  56. $procedure->load($content);
  57. return $procedure;
  58. }
  59. /**
  60. * Load procedure file content.
  61. *
  62. * @access protected
  63. * @param string $file
  64. * @return string
  65. * @throws \InvalidArgumentException
  66. * @throws \JonnyW\PhantomJs\Exception\NotExistsException
  67. */
  68. protected function loadFile($file)
  69. {
  70. if (!stream_is_local($file)) {
  71. throw new \InvalidArgumentException(sprintf('Procedure file is not a local file: "%s"', $file));
  72. }
  73. if (!file_exists($file)) {
  74. throw new NotExistsException(sprintf('Procedure file does not exist: "%s"', $file));
  75. }
  76. return file_get_contents($file);
  77. }
  78. }