protected function mapDirectory(ContainerBuilder $container, $dir, $prefix)
{
if (!is_dir($dir)) {
throw new \RuntimeException(sprintf("Invalid directory %s", $dir));
}
$reader = $container->get('annotation_reader');
$registry = $container->getDefinition('lemon_rest.object_registry');
$finder = new Finder();
$iterator = $finder->files()->name('*.php')->in($dir);
if (substr($prefix, -1, 1) !== "\\") {
$prefix .= "\\";
}
/** @var \SplFileInfo $file */
foreach ($iterator as $file) {
// Translate the directory path from our starting namespace forward
$expandedNamespace = substr($file->getPath(), strlen($dir) + 1);
// If we are in a sub namespace add a trailing separation
$expandedNamespace = $expandedNamespace === false ? '' : $expandedNamespace . '\\';
$className = $prefix . $expandedNamespace . $file->getBasename('.php');
if (class_exists($className)) {
$reflectionClass = new \ReflectionClass($className);
foreach ($reader->getClassAnnotations($reflectionClass) as $annotation) {
if ($annotation instanceof Resource) {
$name = $annotation->name ?: Inflector::pluralize(lcfirst($reflectionClass->getShortName()));
$definition = new Definition('Lemon\\RestBundle\\Object\\Definition', array($name, $className, $annotation->search, $annotation->create, $annotation->update, $annotation->delete, $annotation->partialUpdate));
$container->setDefinition('lemon_rest.object_resources.' . $name, $definition);
$registry->addMethodCall('add', array($definition));
}
}
}
}
}