Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

New plugin for converting legacy namespaces (resolves #666) #1135

Merged
merged 1 commit into from

2 participants

@david0

Basic support for legacy namespaces.
This implements #666.

@mvriel
Owner

Awesome stuff! You have made excellent use of the hooks and options that phpDocumentor provides to extend its functionality!

@mvriel mvriel merged commit 4147557 into phpDocumentor:develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
77 src/phpDocumentor/Plugin/LegacyNamespaceConverter/LegacyNamespaceFilter.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * phpDocumentor
+ *
+ * PHP Version 5.3
+ *
+ * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
+ * @license http://www.opensource.org/licenses/mit-license.php MIT
+ * @link http://phpdoc.org
+ */
+
+namespace phpDocumentor\Plugin\LegacyNamespaceConverter;
+
+use phpDocumentor\Descriptor\DescriptorAbstract;
+use phpDocumentor\Descriptor\ProjectDescriptor\Settings;
+use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
+use Zend\Filter\AbstractFilter;
+
+/**
+ * Converts legacy namespaces
+ */
+class LegacyNamespaceFilter extends AbstractFilter
+{
+ /** @var ProjectDescriptorBuilder $builder */
+ protected $builder;
+
+
+ /**
+ * Initializes this filter with an instance of the builder to retrieve the latest ProjectDescriptor from.
+ *
+ * @param ProjectDescriptorBuilder $builder
+ */
+ public function __construct(ProjectDescriptorBuilder $builder)
+ {
+ $this->builder = $builder;
+ }
+
+
+ /**
+ * Will extract the namespace from the Descriptors name
+ *
+ * @param DescriptorAbstract $value
+ *
+ * @return DescriptorAbstract|null
+ */
+ public function filter($value)
+ {
+ if($value) {
+ $className = $value->getName();
+ $value->setNamespace($this->namespaceFromLegacyNamespace($value->getNamespace(), $className));
+ $value->setName($this->classNameFromLegacyNamespace($className));
+
+ }
+
+ return $value;
+ }
+
+
+ private function namespaceFromLegacyNamespace($namespace, $className)
+ {
+ $qcn = str_replace('_', '\\', $className);
+ if($lastBackslash = strrpos($qcn, '\\')) {
+ $namespace = rtrim($namespace, '\\');
+ $namespace .= '\\' . substr($qcn, 0, $lastBackslash) ;
+ }
+ return $namespace;
+ }
+
+
+ private function classNameFromLegacyNamespace($className)
+ {
+ if($lastUnderscore = strrpos($className, '_'))
+ return substr($className, $lastUnderscore+1) ;
+ else
+ return $className;
+ }
+}
View
15 src/phpDocumentor/Plugin/LegacyNamespaceConverter/README.md
@@ -0,0 +1,15 @@
+Legacy Namespace Converter Plugin
+=================================
+
+This plugin will convert legacy/[PEAR-style namespaces](http://pear.php.net/manual/en/standards.naming.php) to real namespaces for phpDocumentor.
+
+Example configuration file::
+
+ <?xml version="1.0" encoding="UTF-8" ?>
+ <phpdocumentor>
+ <plugins>
+ <plugin path="LegacyNamespaceConverter" />
+ </plugins>
+ </phpdocumentor>
+
+
View
51 src/phpDocumentor/Plugin/LegacyNamespaceConverter/ServiceProvider.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * phpDocumentor
+ *
+ * PHP Version 5.3
+ *
+ * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
+ * @license http://www.opensource.org/licenses/mit-license.php MIT
+ * @link http://phpdoc.org
+ */
+
+namespace phpDocumentor\Plugin\LegacyNamespaceConverter;
+
+use Cilex\Application;
+use Cilex\ServiceProviderInterface;
+use phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter;
+
+class ServiceProvider implements ServiceProviderInterface
+{
+ /**
+ * Registers services on the given app.
+ *
+ * @param Application $app An Application instance.
+ */
+ public function register(Application $app)
+ {
+ $filterManager = $app['descriptor.filter'];
+ $builder = $app['descriptor.builder'];
+
+ $this->addNamespaceFilter($builder, $filterManager);
+ }
+
+ /**
+ * @param $builder
+ * @param $filterManager
+ */
+ private function addNamespaceFilter($builder, $filterManager)
+ {
+ $filter = new LegacyNamespaceFilter($builder);
+
+ $filterManager->attach('phpDocumentor\Descriptor\ConstantDescriptor', $filter);
+ $filterManager->attach('phpDocumentor\Descriptor\FunctionDescriptor', $filter);
+ $filterManager->attach('phpDocumentor\Descriptor\InterfaceDescriptor', $filter);
+ $filterManager->attach('phpDocumentor\Descriptor\TraitDescriptor', $filter);
+ $filterManager->attach('phpDocumentor\Descriptor\PropertyDescriptor', $filter);
+ $filterManager->attach('phpDocumentor\Descriptor\FileDescriptor', $filter);
+ $filterManager->attach('phpDocumentor\Descriptor\ClassDescriptor', $filter);
+ }
+
+
+}
View
94 tests/unit/phpDocumentor/Plugin/LegacyNamespaceConverter/LegacyNamespaceFilterTest.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace phpDocumentor\Plugin\LegacyNamespaceConverter;
+use Mockery as m;
+
+class LegacyNamespaceFilterTest extends \PHPUnit_Framework_TestCase {
+
+ /** @var LegacyNamespaceFilter */
+ private $filter;
+
+ /** @var ProjectDescriptorBuilder $builder */
+ protected $builder;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
+ $this->filter = new LegacyNamespaceFilter($this->builderMock);
+ }
+
+
+ public function testConvertClassNameWithUnderscoreWillBeConvertedToNamespace()
+ {
+ $descriptor = $this->descriptorMock();
+ $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_ClassName');
+ $descriptor->shouldReceive('getNamespace')->andReturn('\\');
+
+ $descriptor->shouldReceive('setName')->with('ClassName')->once();
+ $descriptor->shouldReceive('setNamespace')->with('\LegacyNamespace')->once();
+ $this->filter->filter($descriptor);
+ $this->assertTrue(true);
+ }
+
+
+
+ public function testMultiLevelLegacyNamespace()
+ {
+ $descriptor = $this->descriptorMock();
+ $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_Sub_ClassName');
+ $descriptor->shouldReceive('getNamespace')->andReturn('\\');
+
+ $descriptor->shouldReceive('setName')->with('ClassName')->once();
+ $descriptor->shouldReceive('setNamespace')->with('\LegacyNamespace\Sub')->once();
+ $this->filter->filter($descriptor);
+ $this->assertTrue(true);
+ }
+
+
+ public function testMixedNamespacesCanBeUnified()
+ {
+ $descriptor = $this->descriptorMock();
+ $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_ClassName');
+ $descriptor->shouldReceive('getNamespace')->andReturn('\\NewNamespace');
+
+ $descriptor->shouldReceive('setName')->with('ClassName')->once();
+ $descriptor->shouldReceive('setNamespace')->with('\\NewNamespace\\LegacyNamespace')->once();
+ $this->filter->filter($descriptor);
+ $this->assertTrue(true);
+ }
+
+
+ public function testClassNameWithNewNamespaceWillNotBeModified()
+ {
+ $descriptor = $this->descriptorMock();
+ $descriptor->shouldReceive('getName')->andReturn('ClassName');
+ $descriptor->shouldReceive('getNamespace')->andReturn('\\NewNamespace');
+
+ $descriptor->shouldReceive('setName')->with('ClassName')->once();
+ $descriptor->shouldReceive('setNamespace')->with('\\NewNamespace')->once();
+ $this->filter->filter($descriptor);
+ $this->assertTrue(true);
+ }
+
+
+ public function testClassNameWithEmptyNamespace()
+ {
+ $descriptor = $this->descriptorMock();
+ $descriptor->shouldReceive('getName')->andReturn('ClassName');
+ $descriptor->shouldReceive('getNamespace')->andReturn('\\');
+
+ $descriptor->shouldReceive('setName')->with('ClassName')->once();
+ $descriptor->shouldReceive('setNamespace')->with('\\')->once();
+ $this->filter->filter($descriptor);
+ $this->assertTrue(true);
+ }
+
+
+ private function descriptorMock()
+ {
+ return m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
+ }
+
+}
Something went wrong with that request. Please try again.