basic.php

A 'basic' implementation of the 'example' service type.

Classes

Namesort descending Description
Provision_Service_example_basic A class containing the 'basic' implementation of the 'example' service.

File

example/Provision/Service/example/basic.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * A 'basic' implementation of the 'example' service type.
  5. */
  6. /**
  7. * A class containing the 'basic' implementation of the 'example' service.
  8. *
  9. * This class is conditionally loaded when the "--example_service_type=basic"
  10. * option is passed to provision-save commands run on servers.
  11. *
  12. * The above flag is generated by the hosting counterpart of this class, which
  13. * provides the front end to configure all these fields.
  14. *
  15. * The responsibilities of this class include responding and saving any
  16. * values that are passed to it, and also to override the portions of
  17. * the public API for this service that are necessary.
  18. */
  19. class Provision_Service_example_basic extends Provision_Service_example {
  20. /**
  21. * Some common options handled upstream by the base service classes.
  22. */
  23. /**
  24. * This service needs to have a port specified for it.
  25. */
  26. public $has_port = TRUE;
  27. /**
  28. * The default value for the port input.
  29. */
  30. function default_port() {
  31. return 12345;
  32. }
  33. /**
  34. * This service needs to be restarted with a shell command.
  35. */
  36. public $has_restart_cmd = TRUE;
  37. /**
  38. * The default value for the restart command input.
  39. */
  40. function default_restart_cmd() {
  41. return "/usr/bin/true";
  42. }
  43. /**
  44. * Initialize this class, including option handling.
  45. */
  46. function init_server() {
  47. // REMEMBER TO CALL THE PARENT!
  48. parent::init_server();
  49. /**
  50. * Register configuration classes for the create_config / delete_config methods.
  51. */
  52. $this->configs['server'][] = 'Provision_Config_Example';
  53. /**
  54. * Setting and storing a value.
  55. *
  56. * You will most commonly use :
  57. * $this->server->setProperty('example_field', 'default');
  58. *
  59. * This helper will check for an existing saved value, overridden
  60. * by a command line option falling back to the default.
  61. *
  62. * This is the format used by everything you want configurable from
  63. * the front end or command line.
  64. *
  65. * These values will be saved in ~/.drush/server_name.drush.alias.inc.
  66. */
  67. $this->server->setProperty('example_field', 'default');
  68. /**
  69. * Non configurable values.
  70. *
  71. * If you want to generate values for use in your code or templates,
  72. * but don't want them to be overridden you would use the following format.
  73. *
  74. * $this->server->example_config_path = $this->server->config_path . '/example.d'
  75. *
  76. * This will mean the value will change if you change the config path, but
  77. * you dont need to pass the right input to your command to get there,
  78. * and it's impossible to change the values.
  79. */
  80. $this->server->example_config_path = $this->server->config_path . '/example.d';
  81. }
  82. /**
  83. * Pass additional values to the config file templates.
  84. *
  85. * Even though the $server variable will be available in your template files,
  86. * you may wish to pass additional calculated values to your template files.
  87. *
  88. * Consider this something like the hook_preprocess stuff in drupal.
  89. */
  90. function config_data($config = null, $class = null) {
  91. // This format of calling the parent is very important!
  92. $data = parent::config_data($config, $class);
  93. /**
  94. * This value will become available as $example_current_time
  95. * in all the config files generated by this service.
  96. *
  97. * You could also choose to only conditionally pass values based on
  98. * the parameters.
  99. */
  100. $data['example_current_time'] = date(DATE_COOKIE, time());
  101. return $data;
  102. }
  103. /**
  104. * Implementation of service verify.
  105. */
  106. function verify() {
  107. parent::verify();
  108. if ($this->context->type == 'server') {
  109. // Create the configuration file directory.
  110. provision_file()->create_dir($this->server->example_config_path, dt("Example configuration"), 0700);
  111. // Sync the directory to the remote server if needed.
  112. $this->sync($this->server->example_config_path);
  113. }
  114. }
  115. }