Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Project.php

function Project::getServicesYmlFile

Determines the *.services.yml file in a module.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

Return value

string|false The Services YML file name or false if it could not be derived.

1 call to Project::getServicesYmlFile()
Project::isServiceClass in vendor/drupal/coder/coder_sniffer/DrupalPractice/Project.php
Return true if the given class is a Drupal service registered in *.services.yml.

File

vendor/drupal/coder/coder_sniffer/DrupalPractice/Project.php, line 127

Class

Project
Helper class to retrieve project information like module/theme name for a file.

Namespace

DrupalPractice

Code

public static function getServicesYmlFile(File $phpcsFile) {
    // Cache the services file per file as this might get called often.
    static $cache;
    if (isset($cache[$phpcsFile->getFilename()]) === true) {
        return $cache[$phpcsFile->getFilename()];
    }
    $pathParts = pathinfo($phpcsFile->getFilename());
    // Search for an info file.
    $dir = $pathParts['dirname'];
    do {
        $ymlFiles = glob("{$dir}/*.services.yml");
        // Go one directory up if we do not find an info file here.
        $dir = dirname($dir);
    } while (empty($ymlFiles) === true && $dir !== dirname($dir));
    // No YML file found, so we give up.
    if (empty($ymlFiles) === true) {
        $cache[$phpcsFile->getFilename()] = false;
        return false;
    }
    // Sort the YML file names and take the shortest info file.
    usort($ymlFiles, [
        __NAMESPACE__ . '\\Project',
        'compareLength',
    ]);
    $ymlFile = $ymlFiles[0];
    $cache[$phpcsFile->getFilename()] = $ymlFile;
    return $ymlFile;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal