From ac81ab6d556ce52e98acf636bbcd8ad4d60ecf4e Mon Sep 17 00:00:00 2001 From: David Date: Mon, 10 Aug 2015 17:58:59 +0200 Subject: First support for pages with namespaced page class files --- framework/Web/Services/TPageService.php | 15 ++++++++++----- .../protected/pages/Namespaces/WithNamespace.page | 3 +++ .../protected/pages/Namespaces/WithNamespace.php | 14 ++++++++++++++ .../protected/pages/Namespaces/WithoutNamespace.page | 3 +++ .../protected/pages/Namespaces/WithoutNamespace.php | 6 ++++++ .../features/tests/NamespacesTestCase.php | 17 +++++++++++++++++ 6 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.page create mode 100644 tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.php create mode 100644 tests/FunctionalTests/features/protected/pages/Namespaces/WithoutNamespace.page create mode 100644 tests/FunctionalTests/features/protected/pages/Namespaces/WithoutNamespace.php create mode 100644 tests/FunctionalTests/features/tests/NamespacesTestCase.php diff --git a/framework/Web/Services/TPageService.php b/framework/Web/Services/TPageService.php index be865d89..a52efd13 100644 --- a/framework/Web/Services/TPageService.php +++ b/framework/Web/Services/TPageService.php @@ -101,6 +101,10 @@ class TPageService extends \Prado\TService * Page template file extension */ const PAGE_FILE_EXT='.page'; + /** + * Prefix of Pages used for instantiating new pages + */ + const PAGE_NAMESPACE_PREFIX = 'Application\\Pages\\'; /** * @var string root path of pages */ @@ -490,21 +494,22 @@ class TPageService extends \Prado\TService if($hasClassFile) { $className=basename($path); - if(!class_exists($className,false)) + $namespacedClassName = static::PAGE_NAMESPACE_PREFIX .str_replace('.', '\\', $pagePath); + if(!class_exists($className,false) && !class_exists($namespacedClassName, false)) include_once($path.Prado::CLASS_FILE_EXT); + + if(!class_exists($className,false)) + $className = $namespacedClassName; } else { $className=$this->getBasePageClass(); - Prado::using($className); - if(($pos=strrpos($className,'.'))!==false) - $className=substr($className,$pos+1); } if(!class_exists($className,false) || ($className!=='TPage' && !is_subclass_of($className,'TPage'))) throw new THttpException(404,'pageservice_page_unknown',$pagePath); - $page=new $className; + $page=Prado::createComponent($className); $page->setPagePath($pagePath); if($hasTemplateFile) diff --git a/tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.page b/tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.page new file mode 100644 index 00000000..842868ba --- /dev/null +++ b/tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.page @@ -0,0 +1,3 @@ + + With Namespaces loaded + \ No newline at end of file diff --git a/tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.php b/tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.php new file mode 100644 index 00000000..5a49f6f6 --- /dev/null +++ b/tests/FunctionalTests/features/protected/pages/Namespaces/WithNamespace.php @@ -0,0 +1,14 @@ + + Without Namespaces loaded + \ No newline at end of file diff --git a/tests/FunctionalTests/features/protected/pages/Namespaces/WithoutNamespace.php b/tests/FunctionalTests/features/protected/pages/Namespaces/WithoutNamespace.php new file mode 100644 index 00000000..ad671e7f --- /dev/null +++ b/tests/FunctionalTests/features/protected/pages/Namespaces/WithoutNamespace.php @@ -0,0 +1,6 @@ +url("features/index.php?page=Namespaces.WithoutNamespace"); + $this->pause(50); + $this->assertContains('Without Namespaces loaded', $this->source()); + + $this->url("features/index.php?page=Namespaces.WithNamespace"); + $this->pause(50); + $this->assertContains('With Namespaces loaded', $this->source()); + } + +} \ No newline at end of file -- cgit v1.2.3