fontyourface.drush.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for @font-your-face.
  5. */
  6. /**
  7. * Implements hook_drush_help().
  8. */
  9. function fontyourface_drush_help($section) {
  10. switch ($section) {
  11. case 'drush:fyf-status':
  12. $help = dt('Displays @font-your-face status');
  13. return $help;
  14. case 'drush:fyf-import':
  15. $help = dt('Imports @font-your-face fonts');
  16. return $help;
  17. case 'meta:fontyourface:title':
  18. $help = dt('@font-your-face commands');
  19. return $help;
  20. }
  21. }
  22. /**
  23. * Implements hook_drush_command().
  24. */
  25. function fontyourface_drush_command() {
  26. $items = array();
  27. $items['fyf-status'] = array(
  28. 'callback' => 'fontyourface_fyf_status',
  29. 'drupal dependencies' => array('fontyourface'),
  30. 'description' => 'Displays general @font-your-face status information',
  31. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  32. 'aliases' => array('@s', 'fyfs'),
  33. );
  34. $items['fyf-import'] = array(
  35. 'callback' => 'fontyourface_fyf_import',
  36. 'drupal dependencies' => array('fontyourface'),
  37. 'description' => 'Imports fonts from one or more providers',
  38. 'arguments' => array(
  39. 'providers' => '(optional) A space delimited list of providers.',
  40. ),
  41. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  42. 'aliases' => array('@i', 'fyfi'),
  43. 'examples' => array(
  44. 'drush @i' => 'Import fonts from all enabled providers.',
  45. 'drush @i google_fonts_api fontdeck' => 'Import fonts from Google Fonts API and Fontdeck.',
  46. ),
  47. );
  48. return $items;
  49. }
  50. /**
  51. * Callback for Drush command 'fyf-status'.
  52. */
  53. function fontyourface_fyf_status() {
  54. // Provider info.
  55. $modules = module_implements('fontyourface_info', TRUE);
  56. if (count($modules) > 0) {
  57. drush_print(dt('Providers enabled (@count):', array('@count' => count($modules))));
  58. $rows = array();
  59. $rows[] = array(
  60. dt('Provider'),
  61. dt('Total fonts'),
  62. dt('Enabled fonts'),
  63. );
  64. foreach ($modules as $module) {
  65. $row = array();
  66. $row[] = $module;
  67. $row[] = fontyourface_count_fonts($module);
  68. $row[] = fontyourface_count_fonts($module, TRUE);
  69. $rows[] = $row;
  70. }
  71. drush_print_table($rows, TRUE);
  72. }
  73. else {
  74. drush_print(dt('No font providers are enabled.'));
  75. // If there are no providers, we can call it quits as there will be no
  76. // fonts either.
  77. return;
  78. }
  79. // Font info.
  80. $fonts = fontyourface_get_fonts('enabled = 1');
  81. if (count($fonts) > 0) {
  82. drush_print(dt('Fonts enabled (@count):', array('@count' => count($fonts))));
  83. $rows = array();
  84. $rows[] = array(
  85. dt('Font'),
  86. dt('Provider'),
  87. dt('CSS selector'),
  88. );
  89. foreach ($fonts as $font) {
  90. $row = array();
  91. $row[] = $font->name;
  92. $row[] = $font->provider;
  93. $row[] = $font->css_selector;
  94. $rows[] = $row;
  95. }
  96. drush_print_table($rows, TRUE);
  97. }
  98. else {
  99. drush_print(dt('No fonts are enabled.'));
  100. }
  101. }
  102. /**
  103. * Callback for Drush command 'fyf-import'.
  104. */
  105. function fontyourface_fyf_import() {
  106. $providers = _convert_csv_to_array(func_get_args());
  107. if (empty($providers)) {
  108. // If no providers were specified, get all existing import functions.
  109. $providers = module_implements('fontyourface_import');
  110. }
  111. foreach ($providers as $provider) {
  112. $import_function = $provider . '_fontyourface_import';
  113. if ($provider == 'fontsquirrel') {
  114. drush_set_error(dt("Font Squirrel import is not supported yet. We're working on that."));
  115. }
  116. elseif (!function_exists($import_function)) {
  117. drush_set_error(dt("Unknown provider '@provider', or there is no import function for it; run 'drush fyf-status' to see enabled providers", array('@provider' => $provider)));
  118. }
  119. else {
  120. $before = fontyourface_count_fonts($provider);
  121. if ($import_function()) {
  122. $message = dt('Imported fonts from @provider.', array('@provider' => $provider));
  123. drush_log($message, 'success');
  124. $after = fontyourface_count_fonts($provider);
  125. drush_print(dt('@provider now has @fonts fonts (+@diff).', array('@provider' => $provider, '@fonts' => $after, '@diff' => $after - $before)));
  126. }
  127. else {
  128. drush_set_error(dt('Font import from @provider failed.', array('@provider' => $provider)));
  129. }
  130. }
  131. }
  132. }