Skip to content

Refactor classes. #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
---
# Copyright (c) 2022 PHPER Framework Team
# PHPER is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
---

Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
...
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ header:
- '**/.gitignore'
- '**/.gitmodules'
- '.cargo'
- '.clang-format'
- '.idea'
- '.vscode'
- 'LICENSE'
Expand Down
10 changes: 5 additions & 5 deletions examples/complex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

use phper::{
arrays::ZArray,
classes::{StatefulClass, Visibility},
classes::{ClassEntity, Visibility},
functions::Argument,
ini::{ini_get, Policy},
modules::{Module, ModuleContext},
objects::StatefulObj,
objects::StateObj,
php_get_module,
values::ZVal,
};
Expand Down Expand Up @@ -73,12 +73,12 @@ pub fn get_module() -> Module {
});

// register classes
let mut foo_class = StatefulClass::new("FooClass");
let mut foo_class = ClassEntity::new("FooClass");
foo_class.add_property("foo", Visibility::Private, 100);
foo_class.add_method(
"getFoo",
Visibility::Public,
|this: &mut StatefulObj<()>, _: &mut [ZVal]| {
|this: &mut StateObj<()>, _: &mut [ZVal]| {
let prop = this.get_property("foo");
Ok::<_, phper::Error>(prop.clone())
},
Expand All @@ -87,7 +87,7 @@ pub fn get_module() -> Module {
.add_method(
"setFoo",
Visibility::Public,
|this: &mut StatefulObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
|this: &mut StateObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
this.set_property("foo", arguments[0].clone());
Ok(())
},
Expand Down
15 changes: 8 additions & 7 deletions examples/http-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use crate::{errors::HttpClientError, request::REQUEST_BUILDER_CLASS_NAME};
use phper::{
alloc::ToRefOwned,
classes::{ClassEntry, StatefulClass, Visibility},
classes::{ClassEntity, ClassEntry, Visibility},
functions::Argument,
};
use reqwest::blocking::{Client, ClientBuilder};
Expand All @@ -20,10 +20,10 @@ use std::{mem::take, time::Duration};
const HTTP_CLIENT_BUILDER_CLASS_NAME: &str = "HttpClient\\HttpClientBuilder";
const HTTP_CLIENT_CLASS_NAME: &str = "HttpClient\\HttpClient";

pub fn make_client_builder_class() -> StatefulClass<ClientBuilder> {
// `new_with_default_state` means initialize the state of `ClientBuilder` as
// `Default::default`.
let mut class = StatefulClass::new_with_default_state(HTTP_CLIENT_BUILDER_CLASS_NAME);
pub fn make_client_builder_class() -> ClassEntity<ClientBuilder> {
// `new_with_default_state_constructor` means initialize the state of
// `ClientBuilder` as `Default::default`.
let mut class = ClassEntity::new_with_default_state_constructor(HTTP_CLIENT_BUILDER_CLASS_NAME);

// Inner call the `ClientBuilder::timeout`.
class
Expand Down Expand Up @@ -62,8 +62,9 @@ pub fn make_client_builder_class() -> StatefulClass<ClientBuilder> {
class
}

pub fn make_client_class() -> StatefulClass<Option<Client>> {
let mut class = StatefulClass::<Option<Client>>::new_with_default_state(HTTP_CLIENT_CLASS_NAME);
pub fn make_client_class() -> ClassEntity<Option<Client>> {
let mut class =
ClassEntity::<Option<Client>>::new_with_default_state_constructor(HTTP_CLIENT_CLASS_NAME);

class.add_method("__construct", Visibility::Private, |_, _| {});

Expand Down
10 changes: 5 additions & 5 deletions examples/http-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
// See the Mulan PSL v2 for more details.

use phper::{
classes::{ClassEntry, StatefulClass},
classes::{ClassEntity, ClassEntry},
errors::{exception_class, Throwable},
};

/// The exception class name of extension.
const EXCEPTION_CLASS_NAME: &str = "HttpClient\\HttpClientException";

pub fn make_exception_class() -> StatefulClass<()> {
let mut exception_class = StatefulClass::new(EXCEPTION_CLASS_NAME);
pub fn make_exception_class() -> ClassEntity<()> {
let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
// The `extends` is same as the PHP class `extends`.
exception_class.extends("Exception");
exception_class
class.extends(exception_class);
class
}

#[derive(Debug, thiserror::Error)]
Expand Down
9 changes: 5 additions & 4 deletions examples/http-client/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@

use crate::{errors::HttpClientError, response::RESPONSE_CLASS_NAME};
use phper::{
classes::{ClassEntry, StatefulClass, Visibility},
classes::{ClassEntity, ClassEntry, Visibility},
errors::ThrowObject,
};
use reqwest::blocking::RequestBuilder;
use std::mem::take;

pub const REQUEST_BUILDER_CLASS_NAME: &str = "HttpClient\\RequestBuilder";

pub fn make_request_builder_class() -> StatefulClass<Option<RequestBuilder>> {
let mut class =
StatefulClass::<Option<RequestBuilder>>::new_with_default_state(REQUEST_BUILDER_CLASS_NAME);
pub fn make_request_builder_class() -> ClassEntity<Option<RequestBuilder>> {
let mut class = ClassEntity::<Option<RequestBuilder>>::new_with_default_state_constructor(
REQUEST_BUILDER_CLASS_NAME,
);

class.add_method("__construct", Visibility::Private, |_, _| {});

Expand Down
7 changes: 4 additions & 3 deletions examples/http-client/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
use crate::errors::HttpClientError;
use phper::{
arrays::{InsertKey, ZArray},
classes::{StatefulClass, Visibility},
classes::{ClassEntity, Visibility},
values::ZVal,
};
use reqwest::blocking::Response;
use std::mem::take;

pub const RESPONSE_CLASS_NAME: &str = "HttpClient\\Response";

pub fn make_response_class() -> StatefulClass<Option<Response>> {
let mut class = StatefulClass::<Option<Response>>::new_with_default_state(RESPONSE_CLASS_NAME);
pub fn make_response_class() -> ClassEntity<Option<Response>> {
let mut class =
ClassEntity::<Option<Response>>::new_with_default_state_constructor(RESPONSE_CLASS_NAME);

class.add_method("body", Visibility::Public, |this, _arguments| {
let response = take(this.as_mut_state());
Expand Down
10 changes: 5 additions & 5 deletions examples/http-server/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// See the Mulan PSL v2 for more details.

use phper::{
classes::{ClassEntry, StatefulClass},
classes::{ClassEntity, ClassEntry},
errors::{exception_class, Throwable},
};
use std::error::Error;
Expand All @@ -32,8 +32,8 @@ impl From<HttpServerError> for phper::Error {
}
}

pub fn make_exception_class() -> StatefulClass<()> {
let mut exception_class = StatefulClass::new(EXCEPTION_CLASS_NAME);
exception_class.extends("Exception");
exception_class
pub fn make_exception_class() -> ClassEntity<()> {
let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
class.extends(exception_class);
class
}
6 changes: 3 additions & 3 deletions examples/http-server/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

use phper::classes::{StatefulClass, Visibility};
use phper::classes::{ClassEntity, Visibility};

pub const HTTP_REQUEST_CLASS_NAME: &str = "HttpServer\\HttpRequest";

pub fn make_request_class() -> StatefulClass<()> {
let mut class = StatefulClass::new(HTTP_REQUEST_CLASS_NAME);
pub fn make_request_class() -> ClassEntity<()> {
let mut class = ClassEntity::new(HTTP_REQUEST_CLASS_NAME);

class.add_property("header", Visibility::Public, ());
class.add_property("server", Visibility::Public, ());
Expand Down
6 changes: 3 additions & 3 deletions examples/http-server/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use crate::errors::HttpServerError;
use hyper::{header::HeaderName, http::HeaderValue, Body, Response};
use phper::{
classes::{StatefulClass, Visibility},
classes::{ClassEntity, Visibility},
functions::Argument,
};

pub const HTTP_RESPONSE_CLASS_NAME: &str = "HttpServer\\HttpResponse";

pub fn make_response_class() -> StatefulClass<Response<Body>> {
let mut class = StatefulClass::new_with_default_state(HTTP_RESPONSE_CLASS_NAME);
pub fn make_response_class() -> ClassEntity<Response<Body>> {
let mut class = ClassEntity::new_with_default_state_constructor(HTTP_RESPONSE_CLASS_NAME);

class
.add_method("header", Visibility::Public, |this, arguments| {
Expand Down
11 changes: 6 additions & 5 deletions examples/http-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use hyper::{
};
use phper::{
alloc::{EBox, RefClone, ToRefOwned},
classes::{ClassEntry, StatefulClass, Visibility},
classes::{ClassEntity, ClassEntry, Visibility},
functions::Argument,
values::ZVal,
};
Expand All @@ -34,10 +34,11 @@ use tokio::runtime::Handle;

const HTTP_SERVER_CLASS_NAME: &str = "HttpServer\\HttpServer";

pub fn make_server_class() -> StatefulClass<Option<Builder<AddrIncoming>>> {
let mut class = StatefulClass::<Option<Builder<AddrIncoming>>>::new_with_default_state(
HTTP_SERVER_CLASS_NAME,
);
pub fn make_server_class() -> ClassEntity<Option<Builder<AddrIncoming>>> {
let mut class =
ClassEntity::<Option<Builder<AddrIncoming>>>::new_with_default_state_constructor(
HTTP_SERVER_CLASS_NAME,
);

class.add_property("host", Visibility::Private, "127.0.0.1");
class.add_property("port", Visibility::Private, 8080);
Expand Down
Loading