Skip to content

Commit

Permalink
Merge pull request #6 from spryker/feature/autoload-test
Browse files Browse the repository at this point in the history
Feature/autoload test
  • Loading branch information
gechetspr authored Dec 9, 2024
2 parents 345a717 + 61a4595 commit e16c944
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
18 changes: 0 additions & 18 deletions otel-autoload-example.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,22 @@ protected static function registerAdditionalHooks(): void
return;
}

include_once sprintf('%s/src/Generated/OpenTelemetry/Hooks/%s.php', APPLICATION_ROOT_DIR, HookGenerator::GLOBAL_HOOK_FILE_NAME);
$classmapFileName = APPLICATION_ROOT_DIR . '/src/Generated/OpenTelemetry/Hooks/classmap.php';
if (!file_exists($classmapFileName)) {
return;
}

require_once $classmapFileName;

$autoload = function (string $class) use ($classmap)
{
if (!isset($classmap[$class])) {
return;
}
include_once $classmap[$class];
};

spl_autoload_register($autoload, true, true);
}

/**
Expand Down
33 changes: 20 additions & 13 deletions src/Spryker/Zed/Opentelemetry/Business/Generator/HookGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
class HookGenerator implements HookGeneratorInterface
{
/**
* @var string
* @var int
*/
public const GLOBAL_HOOK_FILE_NAME = 'GlobalHook';
protected const DIRECTORY_PERMISSIONS = 0755;

/**
* @var int
* @var string
*/
protected const DIRECTORY_PERMISSIONS = 0755;
protected const OUTPUT_FILE_PATH_PLACEHOLDER = '%s%s-%sHook.php';

/**
* @var string
*/
protected const OUTPUT_FILE_PATH_PLACEHOLDER_GLOBAL = '%s/%s.php';
protected const CLASSMAP_FILE_NAME_PATTERN = '%sclassmap.php';

/**
* @var string
Expand Down Expand Up @@ -84,23 +84,31 @@ public function generate(): void
{
$collectedClasses = $this->collector->collectClasses();

$this->ensureDirectoryExists();
$classMapFileName = sprintf(static::CLASSMAP_FILE_NAME_PATTERN, $this->config->getOutputDir());
$classMap = fopen($classMapFileName, 'a');
fwrite($classMap, '<?php' . PHP_EOL . '$classmap = [' . PHP_EOL);
foreach ($collectedClasses as $class) {
$content = PHP_EOL;
$this->ensureDirectoryExists();

if (!file_exists($this->getOutputFilepathByModule($class))) {
$hookFile = $this->getOutputFilepath($class);
if (!file_exists($hookFile)) {
$content = '<?php
if (\Spryker\Service\Opentelemetry\Instrumentation\Sampler\TraceSampleResult::shouldSkipTraceBody()) {
return;
}
' . PHP_EOL;
}
$file = fopen($this->getOutputFilepathByModule($class), 'a');

$file = fopen($hookFile, 'a');

fwrite($file, $content);
fwrite($file, $this->contentCreator->createHookContent($class));
fclose($file);
fwrite($classMap, '\'' . $class[static::NAMESPACE_KEY] . '\\' . $class[static::CLASS_NAME_KEY] . '\' => \'' . $hookFile . '\',' . PHP_EOL);
}
fwrite($classMap, '];' . PHP_EOL);
fclose($classMap);
}

/**
Expand All @@ -120,14 +128,13 @@ protected function ensureDirectoryExists(): void
*
* @return string
*/
protected function getOutputFilepathByModule(array $class): string
protected function getOutputFilepath(array $class): string
{
$namespace = explode('\\', $class[static::NAMESPACE_KEY]);
$moduleNamePattern = $namespace[0] . $namespace[1] . $namespace[2];
return sprintf(
static::OUTPUT_FILE_PATH_PLACEHOLDER_GLOBAL,
static::OUTPUT_FILE_PATH_PLACEHOLDER,
$this->config->getOutputDir(),
static::GLOBAL_HOOK_FILE_NAME,
str_replace('\\', '-', $class[static::NAMESPACE_KEY]),
$class[static::CLASS_NAME_KEY],
);
}
}

0 comments on commit e16c944

Please sign in to comment.