provision.context.inc

Provision named context base classes and the function d().

Functions

Namesort descending Description
d Store and access context objects by alias name.
provision_context_factory Create a new context object.
provision_sitealias_get_record Simple access layer for drush_sitealias_get_record.

File

provision.context.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provision named context base classes and the function d().
  5. */
  6. /**
  7. * Store and access context objects by alias name.
  8. *
  9. * @param $name
  10. * A Drush alias name, including leading @.
  11. * @param $_root_object
  12. * Internal use only, set default object returned by d().
  13. * @param $allow_creation
  14. * Defaults to TRUE. Allows creating a new context object with the specified
  15. * $name.
  16. *
  17. * @return
  18. * provision_Context object (by reference) or NULL if it can't be loaded and
  19. * $allow_creation == FALSE.
  20. */
  21. function & d($name = NULL, $_root_object = FALSE, $allow_creation = TRUE) {
  22. static $instances = null;
  23. static $default_instance = '@self';
  24. /*
  25. * add safety check so that we don't get caught running d() before drush is
  26. * fully initialized. otherwise, the proper services hooks may not be
  27. * properly declared and found by drush, so the object cache ($instances)
  28. * would be initialised with incomplete data.
  29. *
  30. * it is possible that more initialisation we need happens after
  31. * DRUSH_BOOTSTRAP_NONE, but this is already better than nothing.
  32. *
  33. * if something is weird with d(), it may be that it is being called too
  34. * early and that check is failing to detect that, so watch out, it took two
  35. * senior Aegir developpers three full days of headbanging on keyboards to
  36. * figure that stuff out.
  37. */
  38. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') == DRUSH_BOOTSTRAP_NONE) {
  39. $ret = drush_set_error('DRUSH_BOOTSTRAPPING', 'drush is still bootstrapping, d() should be ran only within a hook or a function');
  40. return $ret;
  41. }
  42. if (is_object($name)) {
  43. return $name;
  44. }
  45. if ($name == 'all') {
  46. return $instances;
  47. }
  48. if (is_null($name)) {
  49. $name = $default_instance;
  50. }
  51. if ($_root_object) {
  52. $default_instance = $name;
  53. }
  54. $name = provision_normalise_context_name($name);
  55. if (isset($instances[$name])) {
  56. return $instances[$name];
  57. }
  58. else {
  59. $instances[$name] = provision_context_factory($name, $allow_creation);
  60. if (!is_null($instances[$name])) {
  61. $instances[$name]->method_invoke('init');
  62. $instances[$name]->type_invoke('init');
  63. }
  64. return $instances[$name];
  65. }
  66. }
  67. /**
  68. * Simple access layer for drush_sitealias_get_record.
  69. *
  70. * Everytime sitealiases are fetched a lot of processing happens, but if the
  71. * file doesnt exist yet there's a whole lot of unnecesary stuff happening.
  72. *
  73. * We cache the result locally here.
  74. */
  75. function provision_sitealias_get_record($name) {
  76. static $cache = array();
  77. $name = provision_normalise_context_name($name);
  78. if (!isset($cache[$name])) {
  79. $cache[$name] = drush_sitealias_get_record($name);
  80. }
  81. return $cache[$name];
  82. }
  83. /**
  84. * Create a new context object.
  85. *
  86. * @param $name
  87. * The name of the context object to instantiate.
  88. * @param $allow_creation
  89. * Defaults to TRUE. Allows creating a new context object with the specified
  90. * $name.
  91. *
  92. * @return
  93. * An instance of the specified context, or NULL if it could not be loaded.
  94. */
  95. function provision_context_factory($name, $allow_creation = TRUE) {
  96. // the default type, can also be 'platform' or 'site'
  97. $type = 'server';
  98. $record = provision_sitealias_get_record($name);
  99. if (!$allow_creation && empty($record)) {
  100. drush_set_error('PROVISION_MISSING_CONTEXT', dt('Could not find provision alias named: @name' , array('@name' => $name)));
  101. return NULL;
  102. }
  103. $options = array_merge(drush_get_context('stdin'), drush_get_context('options'), drush_get_context('cli'));
  104. if (isset($record['context_type'])) {
  105. $type = $record['context_type'];
  106. }
  107. elseif (isset($options['context_type'])) {
  108. $type = $options['context_type'];
  109. }
  110. $classname = "Provision_Context_{$type}";
  111. return new $classname($name);
  112. }