Skip to content

Commit 9920bf2

Browse files
committed
Support for classes in global namespace
1 parent 323f225 commit 9920bf2

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,20 @@ That is it. You are done!
6969
### Adding New Classes
7070
PHPFUI\InstaDoc saves the classes to display in PHP serialized files. Delete those files (.serial extension) when you want to display new classes. PHPFUI\InstaDoc will regenerate automatically if the files are missing.
7171

72+
### Add Child Classes to the Docs
73+
```php
74+
\PHPFUI\InstaDoc\ChildClasses::load('../ChildClasses.serial');
75+
```
76+
77+
### Add a Global Namespace Class
78+
The git repo path defaults to the composer directory, but you can change the path by calling:
79+
```php
80+
$fileManager->addGlobalNameSpaceClass(__DIR__ . '/global/FPDF.php');
81+
```
82+
7283
### Removing a Namespace
7384
```php
74-
\PHPFUI\InstaDoc\NamespaceTree::deleteNameSpace('cebe\markdown\tests');
85+
$fileManager->excludeNamespace('Carbon');
7586
```
7687

7788
### Add git Repository Page

src/PHPFUI/InstaDoc/Controller.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,15 @@ public function display(array $classPagesToShow = Controller::VALID_CLASS_PAGES,
130130
{
131131
$mainColumn->add($this->getSection('Landing')->generate($page, $this->getParameter(Controller::NAMESPACE)));
132132
}
133-
elseif ($this->getParameter(Controller::CLASS_NAME) && $this->getParameter(Controller::NAMESPACE))
133+
elseif ($this->getParameter(Controller::CLASS_NAME))
134134
{
135-
$fullClassName = $this->getParameter(Controller::NAMESPACE) . '\\' . $this->getParameter(Controller::CLASS_NAME);
135+
$nameSpace = $this->getParameter(Controller::NAMESPACE);
136+
if ($nameSpace)
137+
{
138+
$nameSpace .= '\\';
139+
}
140+
141+
$fullClassName = $nameSpace . $this->getParameter(Controller::CLASS_NAME);
136142
$tree = NamespaceTree::findNamespace($this->getParameter(Controller::NAMESPACE));
137143
$files = $tree->getClassFilenames();
138144
$fullClassPath = $files[$fullClassName] ?? '';

src/PHPFUI/InstaDoc/FileManager.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function __construct(string $composerJsonPath = '')
3232

3333
/**
3434
* You can add a Namespace directly. Specify the namespace (no
35-
* leading \) and the directory containing the class files.
36-
* This is realitive to the current script directory. You can
37-
* also pass an option localGit flag indicating this directory
38-
* is in the project git repo. This will allow you to see the
39-
* git history on the file.
35+
* leading backslash) and the directory containing the class
36+
* files. This is realitive to the current script directory.
37+
* You can also pass an option localGit flag indicating this
38+
* directory is in the project git repo. This will allow you to
39+
* see the git history on the file.
4040
*/
4141
public function addNamespace(string $namespace, string $directory, bool $localGit = false) : FileManager
4242
{
@@ -45,6 +45,21 @@ public function addNamespace(string $namespace, string $directory, bool $localGi
4545
return $this;
4646
}
4747

48+
/**
49+
* The classes in the global namespace are handled slightly
50+
* differently, as this should be the exception rather than the
51+
* rule.
52+
*
53+
* @param string $filename Pass in the full file path
54+
* @param bool $localGit the git flag is attached to the global
55+
* namespace, not the individual file and will use
56+
* the setting of the last file added.
57+
*/
58+
public function addGlobalNameSpaceClass(string $filename, bool $localGit = false) : void
59+
{
60+
NamespaceTree::addGlobalNameSpaceClass($filename, $localGit);
61+
}
62+
4863
/**
4964
* Set file extension for saving index file
5065
*/
@@ -66,7 +81,8 @@ public function setBaseFile(string $fileName) : self
6681
}
6782

6883
/**
69-
* Delete config files
84+
* Delete config files. This should be done when new classes
85+
* have been added to the project.
7086
*
7187
* @return int number of files deleted
7288
*/

src/PHPFUI/InstaDoc/NamespaceTree.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,22 @@ private function __construct()
4747
{
4848
}
4949

50-
public static function addNamespace(string $namespace, string $directory, bool $localGit = false) : void
50+
public static function addGlobalNameSpaceClass(string $filename, bool $localGit = false) : void
5151
{
52-
if (! $namespace)
52+
$filenameLength = \strlen($filename);
53+
if (\strpos($filename, '.php') == $filenameLength - 4)
5354
{
54-
return;
55+
$root = self::getRoot();
56+
$file = \str_replace('/', '\\', \str_replace('.php', '', $filename));
57+
$parts = \explode('\\', $file);
58+
$class = \array_pop($parts);
59+
$root->classes[$class] = $filename;
60+
$root->localGit = $localGit;
5561
}
62+
}
63+
64+
public static function addNamespace(string $namespace, string $directory, bool $localGit = false) : void
65+
{
5666
$namespaceLength = \strlen($namespace);
5767

5868
if ($namespaceLength && '\\' == $namespace[$namespaceLength - 1])
@@ -63,17 +73,17 @@ public static function addNamespace(string $namespace, string $directory, bool $
6373
$node = self::findNamespace($namespace);
6474
$node->localGit = $localGit;
6575

66-
$iterator = new \DirectoryIterator($directory);
76+
$iterator = new \DirectoryIterator($directory);
6777

68-
foreach ($iterator as $fileinfo)
78+
foreach ($iterator as $fileinfo)
6979
{
7080
$filename = $fileinfo->getFilename();
7181
$filenameLength = \strlen($filename);
7282

73-
if ($fileinfo->isDir() && false === \strpos($filename, '.'))
83+
if ($fileinfo->isDir() && false === \strpos($filename, '.'))
7484
{
7585
self::addNamespace($namespace . '\\' . $filename, $directory . '/' . $filename, $localGit);
76-
}
86+
}
7787
elseif (\strpos($filename, '.php') == $filenameLength - 4)
7888
{
7989
$class = \substr($filename, 0, $filenameLength - 4);
@@ -253,6 +263,27 @@ public static function populateMenu(\PHPFUI\Menu $menu) : void
253263
{
254264
self::sort(self::getRoot());
255265

266+
// add no namespace stuff first
267+
if (self::$root->classes)
268+
{
269+
$namespace = '\\';
270+
$rootMenu = new \PHPFUI\Menu();
271+
foreach (self::$root->classes as $class => $path)
272+
{
273+
$activeClass = self::$activeClass;
274+
$activeNamespace = self::$activeNamespace;
275+
276+
$menuItem = new \PHPFUI\MenuItem($class, self::$controller->getClassUrl($class));
277+
if ($class == self::$activeClass)
278+
{
279+
$menuItem->setActive();
280+
}
281+
$rootMenu->addMenuItem($menuItem);
282+
}
283+
$menuItem = new \PHPFUI\MenuItem($namespace);
284+
$menu->addSubMenu($menuItem, $rootMenu);
285+
}
286+
256287
foreach (self::$root->children as $child)
257288
{
258289
$child->getMenuTree($child, $menu);

0 commit comments

Comments
 (0)