016a7eb4e0cc56432761487942f3398605c891e6
Author: Mark Story
Date: 2009-06-14 19:08:05 -0400
diff --git a/api_generator_app_model.php b/api_generator_app_model.php
index bb35a59..31837ee 100644
--- a/api_generator_app_model.php
+++ b/api_generator_app_model.php
@@ -34,5 +34,16 @@ class ApiGeneratorAppModel extends AppModel {
$slugPath = strtolower(Inflector::slug($slashPath, '-'));
return $slugPath;
}
+
+/**
+ * Make a slug
+ *
+ * @param string $name Make a slug
+ * @return string
+ **/
+ protected function _makeSlug($name) {
+ return str_replace('_', '-', Inflector::underscore($name));
+ }
+
}
?>
\ No newline at end of file
diff --git a/models/api_class.php b/models/api_class.php
index d79c484..2ccaff9 100644
--- a/models/api_class.php
+++ b/models/api_class.php
@@ -88,7 +88,7 @@ class ApiClass extends ApiGeneratorAppModel {
**/
public function saveClassDocs(ClassDocumentor $classDoc) {
$classDoc->getAll();
- $slug = str_replace('_', '-', Inflector::underscore($classDoc->name));
+ $slug = $this->_makeSlug($classDoc->name);
$new = array(
'name' => $classDoc->name,
'slug' => $slug,
@@ -111,7 +111,7 @@ class ApiClass extends ApiGeneratorAppModel {
public function savePseudoClassDocs($functions, $filename) {
$methodList = array();
$name = basename($filename);
- $slug = str_replace('_', '-', Inflector::underscore($name));
+ $slug = $this->_makeSlug($name);
foreach ($functions as $func) {
if ($func instanceof FunctionDocumentor) {
$methodList[] = $func->getName();
diff --git a/models/api_package.php b/models/api_package.php
index e496816..8af7aa5 100644
--- a/models/api_package.php
+++ b/models/api_package.php
@@ -94,4 +94,37 @@ class ApiPackage extends ApiGeneratorAppModel {
}
return array_values(array_unique($packages));
}
+
+/**
+ * Updates the package tree with new entries if they exist.
+ * Requires a full package array.
+ *
+ * @param array $packages Array of packages to check / insert into the tree.
+ * @return boolean
+ **/
+ public function updatePackageTree($packages) {
+ $parentId = null;
+ foreach ($packages as $package) {
+ $slug = $this->_makeSlug($package);
+ $existing = $this->findBySlug($slug, null, null, -1);
+ if ($existing) {
+ $parentId = $existing['ApiPackage']['id'];
+ continue;
+ }
+ $new = array(
+ 'ApiPackage' => array(
+ 'parent_id' => $parentId,
+ 'slug' => $slug,
+ 'name' => $package
+ )
+ );
+ $this->create($new);
+ if (!$this->save()) {
+ return false;
+ }
+ $parent = $new;
+ $parentId = $this->id;
+ }
+ return true;
+ }
}
\ No newline at end of file
diff --git a/tests/cases/models/api_package.test.php b/tests/cases/models/api_package.test.php
index 0343199..24b2754 100644
--- a/tests/cases/models/api_package.test.php
+++ b/tests/cases/models/api_package.test.php
@@ -120,4 +120,18 @@ class ApiPackageTestCase extends CakeTestCase {
$this->assertEqual($result, $expected, 'Duplicates not removed %s');
}
+/**
+ * test updating the package tree and ensure that duplicate named packages do not get inserted.
+ *
+ * @return void
+ **/
+ function testUpdatePackageTree() {
+ $packages = array('cake', 'model', 'datasource', 'dbo');
+ $result = $this->ApiPackage->updatePackageTree($packages);
+ $this->assertTrue($result);
+
+ $result = $this->ApiPackage->findAllByParentId(4);
+ $this->assertEqual(count($result), 2);
+ }
+
}
diff --git a/tests/fixtures/api_package_fixture.php b/tests/fixtures/api_package_fixture.php
index 210d42a..38031f4 100644
--- a/tests/fixtures/api_package_fixture.php
+++ b/tests/fixtures/api_package_fixture.php
@@ -67,7 +67,7 @@ var $records = array(
'name' => 'model',
'slug' => 'model',
'lft' => 6,
- 'rght' => 7,
+ 'rght' => 9,
'created' => '2009-01-01 12:00:00',
'modified' => '2009-01-01 12:00:00'
),
@@ -76,8 +76,8 @@ var $records = array(
'parent_id' => 4,
'name' => 'behavior',
'slug' => 'behavior',
- 'lft' => 8,
- 'rght' => 9,
+ 'lft' => 7,
+ 'rght' => 8,
'created' => '2009-01-01 12:00:00',
'modified' => '2009-01-01 12:00:00'
)
